| 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 #include "webrtc/base/testclient.h" | 11 #include "webrtc/base/testclient.h" |
| 12 |
| 13 #include "webrtc/base/gunit.h" |
| 12 #include "webrtc/base/ptr_util.h" | 14 #include "webrtc/base/ptr_util.h" |
| 13 #include "webrtc/base/thread.h" | 15 #include "webrtc/base/thread.h" |
| 14 #include "webrtc/base/timeutils.h" | 16 #include "webrtc/base/timeutils.h" |
| 15 | 17 |
| 16 namespace rtc { | 18 namespace rtc { |
| 17 | 19 |
| 18 // DESIGN: Each packet received is put it into a list of packets. | 20 // DESIGN: Each packet received is put it into a list of packets. |
| 19 // Callers can retrieve received packets from any thread by calling | 21 // Callers can retrieve received packets from any thread by calling |
| 20 // NextPacket. | 22 // NextPacket. |
| 21 | 23 |
| 22 TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket) | 24 TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket) |
| 23 : socket_(std::move(socket)), prev_packet_timestamp_(-1) { | 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) { |
| 24 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); | 32 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); |
| 25 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); | 33 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); |
| 26 } | 34 } |
| 27 | 35 |
| 28 TestClient::~TestClient() {} | 36 TestClient::~TestClient() {} |
| 29 | 37 |
| 30 bool TestClient::CheckConnState(AsyncPacketSocket::State state) { | 38 bool TestClient::CheckConnState(AsyncPacketSocket::State state) { |
| 31 // Wait for our timeout value until the socket reaches the desired state. | 39 // Wait for our timeout value until the socket reaches the desired state. |
| 32 int64_t end = TimeAfter(kTimeoutMs); | 40 int64_t end = TimeAfter(kTimeoutMs); |
| 33 while (socket_->GetState() != state && TimeUntil(end) > 0) { | 41 while (socket_->GetState() != state && TimeUntil(end) > 0) { |
| 34 Thread::Current()->ProcessMessages(1); | 42 AdvanceTime(1); |
| 35 } | 43 } |
| 36 return (socket_->GetState() == state); | 44 return (socket_->GetState() == state); |
| 37 } | 45 } |
| 38 | 46 |
| 39 int TestClient::Send(const char* buf, size_t size) { | 47 int TestClient::Send(const char* buf, size_t size) { |
| 40 rtc::PacketOptions options; | 48 rtc::PacketOptions options; |
| 41 return socket_->Send(buf, size, options); | 49 return socket_->Send(buf, size, options); |
| 42 } | 50 } |
| 43 | 51 |
| 44 int TestClient::SendTo(const char* buf, size_t size, | 52 int TestClient::SendTo(const char* buf, size_t size, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 60 // the wrong thread to non-thread-safe objects. | 68 // the wrong thread to non-thread-safe objects. |
| 61 | 69 |
| 62 int64_t end = TimeAfter(timeout_ms); | 70 int64_t end = TimeAfter(timeout_ms); |
| 63 while (TimeUntil(end) > 0) { | 71 while (TimeUntil(end) > 0) { |
| 64 { | 72 { |
| 65 CritScope cs(&crit_); | 73 CritScope cs(&crit_); |
| 66 if (packets_.size() != 0) { | 74 if (packets_.size() != 0) { |
| 67 break; | 75 break; |
| 68 } | 76 } |
| 69 } | 77 } |
| 70 Thread::Current()->ProcessMessages(1); | 78 AdvanceTime(1); |
| 71 } | 79 } |
| 72 | 80 |
| 73 // Return the first packet placed in the queue. | 81 // Return the first packet placed in the queue. |
| 74 std::unique_ptr<Packet> packet; | 82 std::unique_ptr<Packet> packet; |
| 75 CritScope cs(&crit_); | 83 CritScope cs(&crit_); |
| 76 if (packets_.size() > 0) { | 84 if (packets_.size() > 0) { |
| 77 packet = std::move(packets_.front()); | 85 packet = std::move(packets_.front()); |
| 78 packets_.erase(packets_.begin()); | 86 packets_.erase(packets_.begin()); |
| 79 } | 87 } |
| 80 | 88 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 101 } | 109 } |
| 102 if (prev_packet_timestamp_ != -1) { | 110 if (prev_packet_timestamp_ != -1) { |
| 103 if (packet_timestamp < prev_packet_timestamp_) { | 111 if (packet_timestamp < prev_packet_timestamp_) { |
| 104 res = false; | 112 res = false; |
| 105 } | 113 } |
| 106 } | 114 } |
| 107 prev_packet_timestamp_ = packet_timestamp; | 115 prev_packet_timestamp_ = packet_timestamp; |
| 108 return res; | 116 return res; |
| 109 } | 117 } |
| 110 | 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 |
| 111 bool TestClient::CheckNoPacket() { | 129 bool TestClient::CheckNoPacket() { |
| 112 return NextPacket(kNoPacketTimeoutMs) == nullptr; | 130 return NextPacket(kNoPacketTimeoutMs) == nullptr; |
| 113 } | 131 } |
| 114 | 132 |
| 115 int TestClient::GetError() { | 133 int TestClient::GetError() { |
| 116 return socket_->GetError(); | 134 return socket_->GetError(); |
| 117 } | 135 } |
| 118 | 136 |
| 119 int TestClient::SetOption(Socket::Option opt, int value) { | 137 int TestClient::SetOption(Socket::Option opt, int value) { |
| 120 return socket_->SetOption(opt, value); | 138 return socket_->SetOption(opt, value); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 144 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) { | 162 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) { |
| 145 buf = new char[size]; | 163 buf = new char[size]; |
| 146 memcpy(buf, p.buf, size); | 164 memcpy(buf, p.buf, size); |
| 147 } | 165 } |
| 148 | 166 |
| 149 TestClient::Packet::~Packet() { | 167 TestClient::Packet::~Packet() { |
| 150 delete[] buf; | 168 delete[] buf; |
| 151 } | 169 } |
| 152 | 170 |
| 153 } // namespace rtc | 171 } // namespace rtc |
| OLD | NEW |