Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: webrtc/base/testclient.cc

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/testclient.h ('k') | webrtc/base/testclient_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/base/testclient.h"
12
13 #include "webrtc/base/gunit.h"
14 #include "webrtc/base/ptr_util.h"
15 #include "webrtc/base/thread.h"
16 #include "webrtc/base/timeutils.h"
17
18 namespace rtc {
19
20 // DESIGN: Each packet received is put it into a list of packets.
21 // Callers can retrieve received packets from any thread by calling
22 // NextPacket.
23
24 TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket)
25 : TestClient(std::move(socket), nullptr) {}
26
27 TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket,
28 FakeClock* fake_clock)
29 : fake_clock_(fake_clock),
30 socket_(std::move(socket)),
31 prev_packet_timestamp_(-1) {
32 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket);
33 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend);
34 }
35
36 TestClient::~TestClient() {}
37
38 bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
39 // Wait for our timeout value until the socket reaches the desired state.
40 int64_t end = TimeAfter(kTimeoutMs);
41 while (socket_->GetState() != state && TimeUntil(end) > 0) {
42 AdvanceTime(1);
43 }
44 return (socket_->GetState() == state);
45 }
46
47 int TestClient::Send(const char* buf, size_t size) {
48 rtc::PacketOptions options;
49 return socket_->Send(buf, size, options);
50 }
51
52 int TestClient::SendTo(const char* buf, size_t size,
53 const SocketAddress& dest) {
54 rtc::PacketOptions options;
55 return socket_->SendTo(buf, size, dest, options);
56 }
57
58 std::unique_ptr<TestClient::Packet> TestClient::NextPacket(int timeout_ms) {
59 // If no packets are currently available, we go into a get/dispatch loop for
60 // at most timeout_ms. If, during the loop, a packet arrives, then we can
61 // stop early and return it.
62
63 // Note that the case where no packet arrives is important. We often want to
64 // test that a packet does not arrive.
65
66 // Note also that we only try to pump our current thread's message queue.
67 // Pumping another thread's queue could lead to messages being dispatched from
68 // the wrong thread to non-thread-safe objects.
69
70 int64_t end = TimeAfter(timeout_ms);
71 while (TimeUntil(end) > 0) {
72 {
73 CritScope cs(&crit_);
74 if (packets_.size() != 0) {
75 break;
76 }
77 }
78 AdvanceTime(1);
79 }
80
81 // Return the first packet placed in the queue.
82 std::unique_ptr<Packet> packet;
83 CritScope cs(&crit_);
84 if (packets_.size() > 0) {
85 packet = std::move(packets_.front());
86 packets_.erase(packets_.begin());
87 }
88
89 return packet;
90 }
91
92 bool TestClient::CheckNextPacket(const char* buf, size_t size,
93 SocketAddress* addr) {
94 bool res = false;
95 std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs);
96 if (packet) {
97 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 &&
98 CheckTimestamp(packet->packet_time.timestamp));
99 if (addr)
100 *addr = packet->addr;
101 }
102 return res;
103 }
104
105 bool TestClient::CheckTimestamp(int64_t packet_timestamp) {
106 bool res = true;
107 if (packet_timestamp == -1) {
108 res = false;
109 }
110 if (prev_packet_timestamp_ != -1) {
111 if (packet_timestamp < prev_packet_timestamp_) {
112 res = false;
113 }
114 }
115 prev_packet_timestamp_ = packet_timestamp;
116 return res;
117 }
118
119 void TestClient::AdvanceTime(int ms) {
120 // If the test is using a fake clock, we must advance the fake clock to
121 // advance time. Otherwise, ProcessMessages will work.
122 if (fake_clock_) {
123 SIMULATED_WAIT(false, ms, *fake_clock_);
124 } else {
125 Thread::Current()->ProcessMessages(1);
126 }
127 }
128
129 bool TestClient::CheckNoPacket() {
130 return NextPacket(kNoPacketTimeoutMs) == nullptr;
131 }
132
133 int TestClient::GetError() {
134 return socket_->GetError();
135 }
136
137 int TestClient::SetOption(Socket::Option opt, int value) {
138 return socket_->SetOption(opt, value);
139 }
140
141 void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf,
142 size_t size, const SocketAddress& remote_addr,
143 const PacketTime& packet_time) {
144 CritScope cs(&crit_);
145 packets_.push_back(MakeUnique<Packet>(remote_addr, buf, size, packet_time));
146 }
147
148 void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
149 ++ready_to_send_count_;
150 }
151
152 TestClient::Packet::Packet(const SocketAddress& a,
153 const char* b,
154 size_t s,
155 const PacketTime& packet_time)
156 : addr(a), buf(0), size(s), packet_time(packet_time) {
157 buf = new char[size];
158 memcpy(buf, b, size);
159 }
160
161 TestClient::Packet::Packet(const Packet& p)
162 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) {
163 buf = new char[size];
164 memcpy(buf, p.buf, size);
165 }
166
167 TestClient::Packet::~Packet() {
168 delete[] buf;
169 }
170
171 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/testclient.h ('k') | webrtc/base/testclient_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698