• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 namespace quic {
17 namespace test {
18 
19 // Simple wrapper class to run QuicServer in a dedicated thread.
20 class ServerThread : public QuicThread {
21  public:
22   ServerThread(std::unique_ptr<QuicServer> server,
23                const QuicSocketAddress& address);
24   ServerThread(const ServerThread&) = delete;
25   ServerThread& operator=(const ServerThread&) = delete;
26 
27   ~ServerThread() override;
28 
29   // Prepares the server, but does not start accepting connections. Useful for
30   // injecting mocks.
31   void Initialize();
32 
33   // Runs the event loop. Will initialize if necessary.
34   void Run() override;
35 
36   // Schedules the given action for execution in the event loop.
37   void Schedule(std::function<void()> action);
38 
39   // Waits for the handshake to be confirmed for the first session created.
40   void WaitForCryptoHandshakeConfirmed();
41 
42   // Wait until |termination_predicate| returns true in server thread, or
43   // reached |timeout|. Must be called from an external thread.
44   // Return whether the function returned after |termination_predicate| become
45   // true.
46   bool WaitUntil(std::function<bool()> termination_predicate,
47                  QuicTime::Delta timeout);
48 
49   // Pauses execution of the server until Resume() is called.  May only be
50   // called once.
51   void Pause();
52 
53   // Resumes execution of the server after Pause() has been called.  May only
54   // be called once.
55   void Resume();
56 
57   // Stops the server from executing and shuts it down, destroying all
58   // server objects.
59   void Quit();
60 
61   // Returns the underlying server.  Care must be taken to avoid data races
62   // when accessing the server.  It is always safe to access the server
63   // after calling Pause() and before calling Resume().
server()64   QuicServer* server() { return server_.get(); }
65 
66   // Returns the port that the server is listening on.
67   int GetPort();
68 
69  private:
70   void MaybeNotifyOfHandshakeConfirmation();
71   void ExecuteScheduledActions();
72 
73   QuicNotification
74       confirmed_;            // Notified when the first handshake is confirmed.
75   QuicNotification pause_;   // Notified when the server should pause.
76   QuicNotification paused_;  // Notitied when the server has paused
77   QuicNotification resume_;  // Notified when the server should resume.
78   QuicNotification quit_;    // Notified when the server should quit.
79 
80   std::unique_ptr<QuicServer> server_;
81   QuicClock* clock_;
82   QuicSocketAddress address_;
83   mutable QuicMutex port_lock_;
84   int port_ QUIC_GUARDED_BY(port_lock_);
85 
86   bool initialized_;
87 
88   QuicMutex scheduled_actions_lock_;
89   quiche::QuicheCircularDeque<std::function<void()>> scheduled_actions_
90       QUIC_GUARDED_BY(scheduled_actions_lock_);
91 };
92 
93 }  // namespace test
94 }  // namespace quic
95 
96 #endif  // QUICHE_QUIC_TEST_TOOLS_SERVER_THREAD_H_
97