From 2e222d9bfc4be57be546da9ef5ee469e7e84be30 Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Wed, 2 Oct 2019 15:50:49 +0200 Subject: [PATCH] test for InternetSocket::close blocking Close should not take place in case there is someone reading or writing to the socket. --- .../InternetSocket/test_InternetSocket.cpp | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp b/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp index 8a757ce925..c06f6b587c 100644 --- a/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp +++ b/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp @@ -18,6 +18,9 @@ #include "gtest/gtest.h" #include "features/netsocket/InternetSocket.h" #include "NetworkStack_stub.h" +#include +#include +#include extern std::list eventFlagsStubNextRetval; @@ -155,10 +158,19 @@ TEST_F(TestInternetSocket, close_during_read) { stack.return_value = NSAPI_ERROR_OK; socket->open((NetworkStack *)&stack); - // when c++11 is available use something like the code below to test the blocking behavior - // socket->add_reader(); - // std::async(c[](){std::this_thread::sleep_for(1ms); socket->rem_reader()}); + // Simulate the blocking behavior by adding a reader. + socket->add_reader(); + // The reader will be removed after we attempt to close the socket. + auto delay = std::chrono::milliseconds(2); + auto fut = std::async(std::launch::async, [&](){std::this_thread::sleep_for(delay); socket->rem_reader();}); + + // close() will block until the other thread calls rem_reader() + auto start = std::chrono::system_clock::now(); EXPECT_EQ(socket->close(), NSAPI_ERROR_OK); + auto end = std::chrono::system_clock::now(); + + auto diff = end - start; + EXPECT_LE(delay, end - start); } TEST_F(TestInternetSocket, modify_multicast_group) @@ -166,9 +178,6 @@ TEST_F(TestInternetSocket, modify_multicast_group) SocketAddress a("127.0.0.1", 1024); stack.return_value = NSAPI_ERROR_OK; socket->open((NetworkStack *)&stack); - // when c++11 is available use something like the code below to test the blocking behavior - // socket->add_reader(); - // std::async(c[](){std::this_thread::sleep_for(1ms); socket->rem_reader()}); EXPECT_EQ(socket->join_multicast_group(a), NSAPI_ERROR_UNSUPPORTED); EXPECT_EQ(socket->leave_multicast_group(a), NSAPI_ERROR_UNSUPPORTED); }