UdpSocket: Update constructor
This change adds the UdpSocket::Client interface (as discussed in previous CL
https://chromium-review.googlesource.com/c/openscreen/+/1754731) and updates
the UDP Socket constructor to make the following changes:
- Adds the Client to the constructor
- Adds the Task Runner to the constructor
The remaining changes are to support updating the constructor
Future changes will update the UdpSocket methods to use this Client and Task
Runner, but I want to do those as follow-up CLs to keep this one small
Change-Id: Icac05cf5825e4f84e4090d27849db41b6221a69a
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/1755645
Commit-Queue: Ryan Keane <rwkeane@google.com>
Reviewed-by: mark a. foltz <mfoltz@chromium.org>
Reviewed-by: Max Yakimakha <yakimakha@chromium.org>
diff --git a/platform/api/udp_socket_unittest.cc b/platform/api/udp_socket_unittest.cc
index f50ab72..5c73d14 100644
--- a/platform/api/udp_socket_unittest.cc
+++ b/platform/api/udp_socket_unittest.cc
@@ -5,6 +5,9 @@
#include "platform/api/udp_socket.h"
#include "gtest/gtest.h"
+#include "platform/api/time.h"
+#include "platform/test/fake_clock.h"
+#include "platform/test/fake_task_runner.h"
#include "platform/test/mock_udp_socket.h"
namespace openscreen {
@@ -15,12 +18,20 @@
// which will then crash the running code. This test ensures that deleting a
// new, unmodified UDP Socket object doesn't hit this edge case.
TEST(UdpSocketTest, TestDeletionWithoutCallbackSet) {
- UdpSocket* socket = new MockUdpSocket(UdpSocket::Version::kV4);
+ FakeClock clock(Clock::now());
+ FakeTaskRunner task_runner(&clock);
+ MockUdpSocket::MockClient client;
+ UdpSocket* socket =
+ new MockUdpSocket(&task_runner, &client, UdpSocket::Version::kV4);
delete socket;
}
TEST(UdpSocketTest, TestCallbackCalledOnDeletion) {
- UdpSocket* socket = new MockUdpSocket(UdpSocket::Version::kV4);
+ FakeClock clock(Clock::now());
+ FakeTaskRunner task_runner(&clock);
+ MockUdpSocket::MockClient client;
+ UdpSocket* socket =
+ new MockUdpSocket(&task_runner, &client, UdpSocket::Version::kV4);
int call_count = 0;
std::function<void(UdpSocket*)> callback = [&call_count](UdpSocket* socket) {
call_count++;
@@ -38,8 +49,11 @@
// auto-assigned socket name (i.e., the local endpoint's port will not be zero).
TEST(UdpSocketTest, ResolvesLocalEndpoint_IPv4) {
const uint8_t kIpV4AddrAny[4] = {};
- ErrorOr<UdpSocketUniquePtr> create_result =
- UdpSocket::Create(IPEndpoint{IPAddress(kIpV4AddrAny), 0});
+ FakeClock clock(Clock::now());
+ FakeTaskRunner task_runner(&clock);
+ MockUdpSocket::MockClient client;
+ ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create(
+ &task_runner, &client, IPEndpoint{IPAddress(kIpV4AddrAny), 0});
ASSERT_TRUE(create_result) << create_result.error();
const auto socket = create_result.MoveValue();
const Error bind_result = socket->Bind();
@@ -53,8 +67,11 @@
// auto-assigned socket name (i.e., the local endpoint's port will not be zero).
TEST(UdpSocketTest, ResolvesLocalEndpoint_IPv6) {
const uint8_t kIpV6AddrAny[16] = {};
- ErrorOr<UdpSocketUniquePtr> create_result =
- UdpSocket::Create(IPEndpoint{IPAddress(kIpV6AddrAny), 0});
+ FakeClock clock(Clock::now());
+ FakeTaskRunner task_runner(&clock);
+ MockUdpSocket::MockClient client;
+ ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create(
+ &task_runner, &client, IPEndpoint{IPAddress(kIpV6AddrAny), 0});
ASSERT_TRUE(create_result) << create_result.error();
const auto socket = create_result.MoveValue();
const Error bind_result = socket->Bind();