|
18 | 18 | #include "gtest/gtest.h"
|
19 | 19 | #include "features/netsocket/InternetSocket.h"
|
20 | 20 | #include "NetworkStack_stub.h"
|
| 21 | +#include <future> |
| 22 | +#include <thread> |
| 23 | +#include <chrono> |
21 | 24 |
|
22 | 25 | extern std::list<uint32_t> eventFlagsStubNextRetval;
|
23 | 26 |
|
@@ -155,20 +158,26 @@ TEST_F(TestInternetSocket, close_during_read)
|
155 | 158 | {
|
156 | 159 | stack.return_value = NSAPI_ERROR_OK;
|
157 | 160 | socket->open((NetworkStack *)&stack);
|
158 |
| - // when c++11 is available use something like the code below to test the blocking behavior |
159 |
| - // socket->add_reader(); |
160 |
| - // std::async(c[](){std::this_thread::sleep_for(1ms); socket->rem_reader()}); |
| 161 | + // Simulate the blocking behavior by adding a reader. |
| 162 | + socket->add_reader(); |
| 163 | + // The reader will be removed after we attempt to close the socket. |
| 164 | + auto delay = std::chrono::milliseconds(2); |
| 165 | + auto fut = std::async(std::launch::async, [&](){std::this_thread::sleep_for(delay); socket->rem_reader();}); |
| 166 | + |
| 167 | + // close() will block until the other thread calls rem_reader() |
| 168 | + auto start = std::chrono::system_clock::now(); |
161 | 169 | EXPECT_EQ(socket->close(), NSAPI_ERROR_OK);
|
| 170 | + auto end = std::chrono::system_clock::now(); |
| 171 | + |
| 172 | + auto diff = end - start; |
| 173 | + EXPECT_LE(delay, end - start); |
162 | 174 | }
|
163 | 175 |
|
164 | 176 | TEST_F(TestInternetSocket, modify_multicast_group)
|
165 | 177 | {
|
166 | 178 | SocketAddress a("127.0.0.1", 1024);
|
167 | 179 | stack.return_value = NSAPI_ERROR_OK;
|
168 | 180 | socket->open((NetworkStack *)&stack);
|
169 |
| - // when c++11 is available use something like the code below to test the blocking behavior |
170 |
| - // socket->add_reader(); |
171 |
| - // std::async(c[](){std::this_thread::sleep_for(1ms); socket->rem_reader()}); |
172 | 181 | EXPECT_EQ(socket->join_multicast_group(a), NSAPI_ERROR_UNSUPPORTED);
|
173 | 182 | EXPECT_EQ(socket->leave_multicast_group(a), NSAPI_ERROR_UNSUPPORTED);
|
174 | 183 | }
|
|
0 commit comments