OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_TESTCLIENT_H_ | 11 #ifndef WEBRTC_BASE_TESTCLIENT_H_ |
12 #define WEBRTC_BASE_TESTCLIENT_H_ | 12 #define WEBRTC_BASE_TESTCLIENT_H_ |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 #include <vector> | 15 #include <vector> |
16 #include "webrtc/base/asyncudpsocket.h" | 16 #include "webrtc/base/asyncudpsocket.h" |
17 #include "webrtc/base/constructormagic.h" | 17 #include "webrtc/base/constructormagic.h" |
18 #include "webrtc/base/criticalsection.h" | 18 #include "webrtc/base/criticalsection.h" |
| 19 #include "webrtc/base/fakeclock.h" |
19 | 20 |
20 namespace rtc { | 21 namespace rtc { |
21 | 22 |
22 // A simple client that can send TCP or UDP data and check that it receives | 23 // A simple client that can send TCP or UDP data and check that it receives |
23 // what it expects to receive. Useful for testing server functionality. | 24 // what it expects to receive. Useful for testing server functionality. |
24 class TestClient : public sigslot::has_slots<> { | 25 class TestClient : public sigslot::has_slots<> { |
25 public: | 26 public: |
26 // Records the contents of a packet that was received. | 27 // Records the contents of a packet that was received. |
27 struct Packet { | 28 struct Packet { |
28 Packet(const SocketAddress& a, | 29 Packet(const SocketAddress& a, |
29 const char* b, | 30 const char* b, |
30 size_t s, | 31 size_t s, |
31 const PacketTime& packet_time); | 32 const PacketTime& packet_time); |
32 Packet(const Packet& p); | 33 Packet(const Packet& p); |
33 virtual ~Packet(); | 34 virtual ~Packet(); |
34 | 35 |
35 SocketAddress addr; | 36 SocketAddress addr; |
36 char* buf; | 37 char* buf; |
37 size_t size; | 38 size_t size; |
38 PacketTime packet_time; | 39 PacketTime packet_time; |
39 }; | 40 }; |
40 | 41 |
41 // Default timeout for NextPacket reads. | 42 // Default timeout for NextPacket reads. |
42 static const int kTimeoutMs = 5000; | 43 static const int kTimeoutMs = 5000; |
43 | 44 |
44 // Creates a client that will send and receive with the given socket and | 45 // Creates a client that will send and receive with the given socket and |
45 // will post itself messages with the given thread. | 46 // will post itself messages with the given thread. |
46 explicit TestClient(std::unique_ptr<AsyncPacketSocket> socket); | 47 explicit TestClient(std::unique_ptr<AsyncPacketSocket> socket); |
| 48 // Create a test client that will use a fake clock. NextPacket needs to wait |
| 49 // for a packet to be received, and thus it needs to advance the fake clock |
| 50 // if the test is using one, rather than just sleeping. |
| 51 TestClient(std::unique_ptr<AsyncPacketSocket> socket, FakeClock* fake_clock); |
47 ~TestClient() override; | 52 ~TestClient() override; |
48 | 53 |
49 SocketAddress address() const { return socket_->GetLocalAddress(); } | 54 SocketAddress address() const { return socket_->GetLocalAddress(); } |
50 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); } | 55 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); } |
51 | 56 |
52 // Checks that the socket moves to the specified connect state. | 57 // Checks that the socket moves to the specified connect state. |
53 bool CheckConnState(AsyncPacketSocket::State state); | 58 bool CheckConnState(AsyncPacketSocket::State state); |
54 | 59 |
55 // Checks that the socket is connected to the remote side. | 60 // Checks that the socket is connected to the remote side. |
56 bool CheckConnected() { | 61 bool CheckConnected() { |
(...skipping 29 matching lines...) Expand all Loading... |
86 // Timeout for reads when no packet is expected. | 91 // Timeout for reads when no packet is expected. |
87 static const int kNoPacketTimeoutMs = 1000; | 92 static const int kNoPacketTimeoutMs = 1000; |
88 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist. | 93 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist. |
89 Socket::ConnState GetState(); | 94 Socket::ConnState GetState(); |
90 // Slot for packets read on the socket. | 95 // Slot for packets read on the socket. |
91 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len, | 96 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len, |
92 const SocketAddress& remote_addr, | 97 const SocketAddress& remote_addr, |
93 const PacketTime& packet_time); | 98 const PacketTime& packet_time); |
94 void OnReadyToSend(AsyncPacketSocket* socket); | 99 void OnReadyToSend(AsyncPacketSocket* socket); |
95 bool CheckTimestamp(int64_t packet_timestamp); | 100 bool CheckTimestamp(int64_t packet_timestamp); |
| 101 void AdvanceTime(int ms); |
96 | 102 |
| 103 FakeClock* fake_clock_ = nullptr; |
97 CriticalSection crit_; | 104 CriticalSection crit_; |
98 std::unique_ptr<AsyncPacketSocket> socket_; | 105 std::unique_ptr<AsyncPacketSocket> socket_; |
99 std::vector<std::unique_ptr<Packet>> packets_; | 106 std::vector<std::unique_ptr<Packet>> packets_; |
100 int ready_to_send_count_ = 0; | 107 int ready_to_send_count_ = 0; |
101 int64_t prev_packet_timestamp_; | 108 int64_t prev_packet_timestamp_; |
102 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient); | 109 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient); |
103 }; | 110 }; |
104 | 111 |
105 } // namespace rtc | 112 } // namespace rtc |
106 | 113 |
107 #endif // WEBRTC_BASE_TESTCLIENT_H_ | 114 #endif // WEBRTC_BASE_TESTCLIENT_H_ |
OLD | NEW |