xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/test_tools/server_thread.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_QUIC_TEST_TOOLS_SERVER_THREAD_H_
6 #define QUICHE_QUIC_TEST_TOOLS_SERVER_THREAD_H_
7 
8 #include <memory>
9 
10 #include "quiche/quic/core/quic_config.h"
11 #include "quiche/quic/platform/api/quic_mutex.h"
12 #include "quiche/quic/platform/api/quic_socket_address.h"
13 #include "quiche/quic/platform/api/quic_thread.h"
14 #include "quiche/quic/tools/quic_server.h"
15 #include "quiche/common/quiche_callbacks.h"
16 
17 namespace quic {
18 namespace test {
19 
20 // Simple wrapper class to run QuicServer in a dedicated thread.
21 class ServerThread : public QuicThread {
22  public:
23   ServerThread(std::unique_ptr<QuicServer> server,
24                const QuicSocketAddress& address);
25   ServerThread(const ServerThread&) = delete;
26   ServerThread& operator=(const ServerThread&) = delete;
27 
28   ~ServerThread() override;
29 
30   // Prepares the server, but does not start accepting connections. Useful for
31   // injecting mocks.
32   void Initialize();
33 
34   // Runs the event loop. Will initialize if necessary.
35   void Run() override;
36 
37   // Schedules the given action for execution in the event loop.
38   void Schedule(quiche::SingleUseCallback<void()> action);
39 
40   // Like |Schedule|, but wait for |action| to complete before function returns.
41   void ScheduleAndWaitForCompletion(quiche::SingleUseCallback<void()> action);
42 
43   // Waits for the handshake to be confirmed for the first session created.
44   void WaitForCryptoHandshakeConfirmed();
45 
46   // Wait until |termination_predicate| returns true in server thread, or
47   // reached |timeout|. Must be called from an external thread.
48   // Return whether the function returned after |termination_predicate| become
49   // true.
50   bool WaitUntil(quiche::UnretainedCallback<bool()> termination_predicate,
51                  QuicTime::Delta timeout);
52 
53   // Pauses execution of the server until Resume() is called.  May only be
54   // called once.
55   void Pause();
56 
57   // Resumes execution of the server after Pause() has been called.  May only
58   // be called once.
59   void Resume();
60 
61   // Stops the server from executing and shuts it down, destroying all
62   // server objects.
63   void Quit();
64 
65   // Returns the underlying server.  Care must be taken to avoid data races
66   // when accessing the server.  It is always safe to access the server
67   // after calling Pause() and before calling Resume().
server()68   QuicServer* server() { return server_.get(); }
69 
70   // Returns the port that the server is listening on.
71   int GetPort();
72 
73  private:
74   void MaybeNotifyOfHandshakeConfirmation();
75   void ExecuteScheduledActions();
76 
77   QuicNotification
78       confirmed_;            // Notified when the first handshake is confirmed.
79   QuicNotification pause_;   // Notified when the server should pause.
80   QuicNotification paused_;  // Notitied when the server has paused
81   QuicNotification resume_;  // Notified when the server should resume.
82   QuicNotification quit_;    // Notified when the server should quit.
83 
84   std::unique_ptr<QuicServer> server_;
85   QuicClock* clock_;
86   QuicSocketAddress address_;
87   mutable QuicMutex port_lock_;
88   int port_ QUIC_GUARDED_BY(port_lock_);
89 
90   bool initialized_;
91 
92   QuicMutex scheduled_actions_lock_;
93   quiche::QuicheCircularDeque<quiche::SingleUseCallback<void()>>
94       scheduled_actions_ QUIC_GUARDED_BY(scheduled_actions_lock_);
95 };
96 
97 }  // namespace test
98 }  // namespace quic
99 
100 #endif  // QUICHE_QUIC_TEST_TOOLS_SERVER_THREAD_H_
101