2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/test_client.h"
|
Update VirtualSocketServerTest to use a fake clock.
Since this is a test for a fake network, it's only natural that it uses
a fake clock as well. This makes the tests much faster, less flaky, and
lets them be moved out of "webrtc_nonparallel_tests", since they no
longer have a dependency on any "real" thing (sockets, or time) and
can be run in parallel as easily as any other tests.
As part of this CL, added the fake clock as an argument to
VirtualSocketServer's and TestClient's constructors, since these classes
have methods that wait synchronously for something to occur, and if the
test is using a fake clock, they need to advance it in order to make
progress.
Lastly, added a DCHECK in Thread::ProcessMessages. If called with a
nonzero time while a fake clock is used, it will get stuck in an
infinite loop; a DCHECK is easier to notice than an infinite loop.
BUG=webrtc:7727, webrtc:2409
Review-Url: https://codereview.webrtc.org/2927413002
Cr-Commit-Position: refs/heads/master@{#18544}
2017-06-12 14:30:28 -07:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <string.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2025-01-17 13:19:45 +00:00
|
|
|
#include <cstdint>
|
2019-09-17 17:06:18 +02:00
|
|
|
#include <memory>
|
2025-01-17 13:19:45 +00:00
|
|
|
#include <optional>
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
2025-01-17 13:19:45 +00:00
|
|
|
#include "api/units/time_delta.h"
|
2023-11-17 10:18:25 +01:00
|
|
|
#include "api/units/timestamp.h"
|
2025-01-17 13:19:45 +00:00
|
|
|
#include "rtc_base/async_packet_socket.h"
|
|
|
|
|
#include "rtc_base/fake_clock.h"
|
2023-11-17 10:18:25 +01:00
|
|
|
#include "rtc_base/network/received_packet.h"
|
2025-01-17 13:19:45 +00:00
|
|
|
#include "rtc_base/socket.h"
|
|
|
|
|
#include "rtc_base/socket_address.h"
|
|
|
|
|
#include "rtc_base/synchronization/mutex.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/thread.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/time_utils.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
|
|
|
|
// DESIGN: Each packet received is put it into a list of packets.
|
|
|
|
|
// Callers can retrieve received packets from any thread by calling
|
|
|
|
|
// NextPacket.
|
|
|
|
|
|
2017-05-08 01:57:18 -07:00
|
|
|
TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket)
|
Update VirtualSocketServerTest to use a fake clock.
Since this is a test for a fake network, it's only natural that it uses
a fake clock as well. This makes the tests much faster, less flaky, and
lets them be moved out of "webrtc_nonparallel_tests", since they no
longer have a dependency on any "real" thing (sockets, or time) and
can be run in parallel as easily as any other tests.
As part of this CL, added the fake clock as an argument to
VirtualSocketServer's and TestClient's constructors, since these classes
have methods that wait synchronously for something to occur, and if the
test is using a fake clock, they need to advance it in order to make
progress.
Lastly, added a DCHECK in Thread::ProcessMessages. If called with a
nonzero time while a fake clock is used, it will get stuck in an
infinite loop; a DCHECK is easier to notice than an infinite loop.
BUG=webrtc:7727, webrtc:2409
Review-Url: https://codereview.webrtc.org/2927413002
Cr-Commit-Position: refs/heads/master@{#18544}
2017-06-12 14:30:28 -07:00
|
|
|
: TestClient(std::move(socket), nullptr) {}
|
|
|
|
|
|
|
|
|
|
TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket,
|
2019-04-17 10:36:03 +02:00
|
|
|
ThreadProcessingFakeClock* fake_clock)
|
2023-11-17 10:18:25 +01:00
|
|
|
: fake_clock_(fake_clock), socket_(std::move(socket)) {
|
|
|
|
|
socket_->RegisterReceivedPacketCallback(
|
|
|
|
|
[&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) {
|
|
|
|
|
OnPacket(socket, packet);
|
|
|
|
|
});
|
2014-05-13 18:00:26 +00:00
|
|
|
socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 01:57:18 -07:00
|
|
|
TestClient::~TestClient() {}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
|
|
|
|
|
// Wait for our timeout value until the socket reaches the desired state.
|
2016-05-19 14:57:31 -07:00
|
|
|
int64_t end = TimeAfter(kTimeoutMs);
|
|
|
|
|
while (socket_->GetState() != state && TimeUntil(end) > 0) {
|
Update VirtualSocketServerTest to use a fake clock.
Since this is a test for a fake network, it's only natural that it uses
a fake clock as well. This makes the tests much faster, less flaky, and
lets them be moved out of "webrtc_nonparallel_tests", since they no
longer have a dependency on any "real" thing (sockets, or time) and
can be run in parallel as easily as any other tests.
As part of this CL, added the fake clock as an argument to
VirtualSocketServer's and TestClient's constructors, since these classes
have methods that wait synchronously for something to occur, and if the
test is using a fake clock, they need to advance it in order to make
progress.
Lastly, added a DCHECK in Thread::ProcessMessages. If called with a
nonzero time while a fake clock is used, it will get stuck in an
infinite loop; a DCHECK is easier to notice than an infinite loop.
BUG=webrtc:7727, webrtc:2409
Review-Url: https://codereview.webrtc.org/2927413002
Cr-Commit-Position: refs/heads/master@{#18544}
2017-06-12 14:30:28 -07:00
|
|
|
AdvanceTime(1);
|
2016-05-19 14:57:31 -07:00
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
return (socket_->GetState() == state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TestClient::Send(const char* buf, size_t size) {
|
|
|
|
|
rtc::PacketOptions options;
|
|
|
|
|
return socket_->Send(buf, size, options);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
int TestClient::SendTo(const char* buf,
|
|
|
|
|
size_t size,
|
2014-05-13 18:00:26 +00:00
|
|
|
const SocketAddress& dest) {
|
|
|
|
|
rtc::PacketOptions options;
|
|
|
|
|
return socket_->SendTo(buf, size, dest, options);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 01:57:18 -07:00
|
|
|
std::unique_ptr<TestClient::Packet> TestClient::NextPacket(int timeout_ms) {
|
2014-05-13 18:00:26 +00:00
|
|
|
// If no packets are currently available, we go into a get/dispatch loop for
|
2015-02-07 22:37:59 +00:00
|
|
|
// at most timeout_ms. If, during the loop, a packet arrives, then we can
|
|
|
|
|
// stop early and return it.
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
// Note that the case where no packet arrives is important. We often want to
|
|
|
|
|
// test that a packet does not arrive.
|
|
|
|
|
|
|
|
|
|
// Note also that we only try to pump our current thread's message queue.
|
|
|
|
|
// Pumping another thread's queue could lead to messages being dispatched from
|
|
|
|
|
// the wrong thread to non-thread-safe objects.
|
|
|
|
|
|
2016-05-19 14:57:31 -07:00
|
|
|
int64_t end = TimeAfter(timeout_ms);
|
2014-05-13 18:00:26 +00:00
|
|
|
while (TimeUntil(end) > 0) {
|
|
|
|
|
{
|
2020-07-08 17:55:58 +02:00
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2025-01-17 13:19:45 +00:00
|
|
|
if (!packets_.empty()) {
|
2014-05-13 18:00:26 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
Update VirtualSocketServerTest to use a fake clock.
Since this is a test for a fake network, it's only natural that it uses
a fake clock as well. This makes the tests much faster, less flaky, and
lets them be moved out of "webrtc_nonparallel_tests", since they no
longer have a dependency on any "real" thing (sockets, or time) and
can be run in parallel as easily as any other tests.
As part of this CL, added the fake clock as an argument to
VirtualSocketServer's and TestClient's constructors, since these classes
have methods that wait synchronously for something to occur, and if the
test is using a fake clock, they need to advance it in order to make
progress.
Lastly, added a DCHECK in Thread::ProcessMessages. If called with a
nonzero time while a fake clock is used, it will get stuck in an
infinite loop; a DCHECK is easier to notice than an infinite loop.
BUG=webrtc:7727, webrtc:2409
Review-Url: https://codereview.webrtc.org/2927413002
Cr-Commit-Position: refs/heads/master@{#18544}
2017-06-12 14:30:28 -07:00
|
|
|
AdvanceTime(1);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the first packet placed in the queue.
|
2017-05-08 01:57:18 -07:00
|
|
|
std::unique_ptr<Packet> packet;
|
2020-07-08 17:55:58 +02:00
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2025-01-17 13:19:45 +00:00
|
|
|
if (!packets_.empty()) {
|
2017-05-08 01:57:18 -07:00
|
|
|
packet = std::move(packets_.front());
|
|
|
|
|
packets_.erase(packets_.begin());
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
bool TestClient::CheckNextPacket(const char* buf,
|
|
|
|
|
size_t size,
|
2014-05-13 18:00:26 +00:00
|
|
|
SocketAddress* addr) {
|
|
|
|
|
bool res = false;
|
2017-05-08 01:57:18 -07:00
|
|
|
std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs);
|
2014-05-13 18:00:26 +00:00
|
|
|
if (packet) {
|
2023-11-17 10:18:25 +01:00
|
|
|
res = (packet->buf.size() == size &&
|
|
|
|
|
memcmp(packet->buf.data(), buf, size) == 0 &&
|
|
|
|
|
CheckTimestamp(packet->packet_time));
|
2014-05-13 18:00:26 +00:00
|
|
|
if (addr)
|
|
|
|
|
*addr = packet->addr;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 10:18:25 +01:00
|
|
|
bool TestClient::CheckTimestamp(
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<webrtc::Timestamp> packet_timestamp) {
|
2016-05-23 18:19:26 +02:00
|
|
|
bool res = true;
|
2023-11-17 10:18:25 +01:00
|
|
|
if (!packet_timestamp) {
|
2016-05-23 18:19:26 +02:00
|
|
|
res = false;
|
|
|
|
|
}
|
2023-11-17 10:18:25 +01:00
|
|
|
if (prev_packet_timestamp_) {
|
2016-05-23 18:19:26 +02:00
|
|
|
if (packet_timestamp < prev_packet_timestamp_) {
|
|
|
|
|
res = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
prev_packet_timestamp_ = packet_timestamp;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
Update VirtualSocketServerTest to use a fake clock.
Since this is a test for a fake network, it's only natural that it uses
a fake clock as well. This makes the tests much faster, less flaky, and
lets them be moved out of "webrtc_nonparallel_tests", since they no
longer have a dependency on any "real" thing (sockets, or time) and
can be run in parallel as easily as any other tests.
As part of this CL, added the fake clock as an argument to
VirtualSocketServer's and TestClient's constructors, since these classes
have methods that wait synchronously for something to occur, and if the
test is using a fake clock, they need to advance it in order to make
progress.
Lastly, added a DCHECK in Thread::ProcessMessages. If called with a
nonzero time while a fake clock is used, it will get stuck in an
infinite loop; a DCHECK is easier to notice than an infinite loop.
BUG=webrtc:7727, webrtc:2409
Review-Url: https://codereview.webrtc.org/2927413002
Cr-Commit-Position: refs/heads/master@{#18544}
2017-06-12 14:30:28 -07:00
|
|
|
void TestClient::AdvanceTime(int ms) {
|
|
|
|
|
// If the test is using a fake clock, we must advance the fake clock to
|
|
|
|
|
// advance time. Otherwise, ProcessMessages will work.
|
|
|
|
|
if (fake_clock_) {
|
2025-01-17 13:19:45 +00:00
|
|
|
for (int64_t start = rtc ::TimeMillis(); rtc ::TimeMillis() < start + ms;) {
|
|
|
|
|
fake_clock_->AdvanceTime(webrtc ::TimeDelta ::Millis(1));
|
|
|
|
|
};
|
Update VirtualSocketServerTest to use a fake clock.
Since this is a test for a fake network, it's only natural that it uses
a fake clock as well. This makes the tests much faster, less flaky, and
lets them be moved out of "webrtc_nonparallel_tests", since they no
longer have a dependency on any "real" thing (sockets, or time) and
can be run in parallel as easily as any other tests.
As part of this CL, added the fake clock as an argument to
VirtualSocketServer's and TestClient's constructors, since these classes
have methods that wait synchronously for something to occur, and if the
test is using a fake clock, they need to advance it in order to make
progress.
Lastly, added a DCHECK in Thread::ProcessMessages. If called with a
nonzero time while a fake clock is used, it will get stuck in an
infinite loop; a DCHECK is easier to notice than an infinite loop.
BUG=webrtc:7727, webrtc:2409
Review-Url: https://codereview.webrtc.org/2927413002
Cr-Commit-Position: refs/heads/master@{#18544}
2017-06-12 14:30:28 -07:00
|
|
|
} else {
|
|
|
|
|
Thread::Current()->ProcessMessages(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
bool TestClient::CheckNoPacket() {
|
2017-05-08 01:57:18 -07:00
|
|
|
return NextPacket(kNoPacketTimeoutMs) == nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TestClient::GetError() {
|
|
|
|
|
return socket_->GetError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TestClient::SetOption(Socket::Option opt, int value) {
|
|
|
|
|
return socket_->SetOption(opt, value);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
void TestClient::OnPacket(AsyncPacketSocket* socket,
|
2023-11-17 10:18:25 +01:00
|
|
|
const rtc::ReceivedPacket& received_packet) {
|
2020-07-08 17:55:58 +02:00
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2023-11-17 10:18:25 +01:00
|
|
|
packets_.push_back(std::make_unique<Packet>(received_packet));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
|
2016-09-09 13:16:15 -07:00
|
|
|
++ready_to_send_count_;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 10:18:25 +01:00
|
|
|
TestClient::Packet::Packet(const rtc::ReceivedPacket& received_packet)
|
|
|
|
|
: addr(received_packet.source_address()),
|
|
|
|
|
// Copy received_packet payload to a buffer owned by Packet.
|
|
|
|
|
buf(received_packet.payload().data(), received_packet.payload().size()),
|
|
|
|
|
packet_time(received_packet.arrival_time()) {}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
TestClient::Packet::Packet(const Packet& p)
|
2023-11-17 10:18:25 +01:00
|
|
|
: addr(p.addr),
|
|
|
|
|
buf(p.buf.data(), p.buf.size()),
|
|
|
|
|
packet_time(p.packet_time) {}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
} // namespace rtc
|