Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame^] | 1 | // 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 | |
| 10 | namespace openscreen { |
| 11 | namespace 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. |
| 17 | TEST(UdpSocketTest, TestDeletionWithoutCallbackSet) { |
| 18 | UdpSocket* socket = new MockUdpSocket(UdpSocket::Version::kV4); |
| 19 | delete socket; |
| 20 | } |
| 21 | |
| 22 | TEST(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 |