blob: 78140325f524878f4561bb9a586e8aa34d5333c2 [file] [log] [blame]
Ryan Keanedeb48b32019-06-28 16:24:40 -07001// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "platform/api/udp_socket.h"
6
7#include "gtest/gtest.h"
8#include "platform/test/mock_udp_socket.h"
9
10namespace openscreen {
11namespace platform {
12
13// Under some conditions, calling a callback can result in an exception
14// "terminate called after throwing an instance of 'std::bad_function_call'"
15// which will then crash the running code. This test ensures that deleting a
16// new, unmodified UDP Socket object doesn't hit this edge case.
17TEST(UdpSocketTest, TestDeletionWithoutCallbackSet) {
18 UdpSocket* socket = new MockUdpSocket(UdpSocket::Version::kV4);
19 delete socket;
20}
21
22TEST(UdpSocketTest, TestCallbackCalledOnDeletion) {
23 UdpSocket* socket = new MockUdpSocket(UdpSocket::Version::kV4);
24 int call_count = 0;
25 std::function<void(UdpSocket*)> callback = [&call_count](UdpSocket* socket) {
26 call_count++;
27 };
28 socket->SetDeletionCallback(callback);
29
30 EXPECT_EQ(call_count, 0);
31 delete socket;
32
33 EXPECT_EQ(call_count, 1);
34}
35
36} // namespace platform
37} // namespace openscreen