Skip to content

Commit

Permalink
More robust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed Apr 11, 2024
1 parent dc9d500 commit baac1a9
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions tests/ecaludp_test/src/ecaludp_socket_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,16 @@ TEST(EcalUdpSocket, AsyncBigMessage)
// Send and Receive a small Hello World message using the sync API
TEST(EcalUdpSocket, SyncHelloWorldMessage)
{
atomic_signalable<int> received_messages(0);

asio::io_context io_context; // Will never be started, as we are using the sync API exclusively

// Create a send and recieve socket
ecaludp::Socket send_socket(io_context, {'E', 'C', 'A', 'L'});
ecaludp::Socket rev_socket (io_context, {'E', 'C', 'A', 'L'});

// Create a thread that will receive a message
std::thread recv_thread([&rev_socket]()
std::thread recv_thread([&rev_socket, &received_messages]()
{
// Open the socket
{
Expand All @@ -197,6 +199,8 @@ TEST(EcalUdpSocket, SyncHelloWorldMessage)
// Receive a message
asio::error_code ec;
auto received_buffer = rev_socket.receive_from(sender_endpoint, 0, ec);

received_messages++;

ASSERT_FALSE(ec);

Expand All @@ -205,8 +209,8 @@ TEST(EcalUdpSocket, SyncHelloWorldMessage)
ASSERT_EQ(received_string, std::string("Hello World!"));
});

// Wait a millisecond to make sure that the receiver is ready
std::this_thread::sleep_for(std::chrono::milliseconds(1));
// Wait 10 milliseconds to make sure that the receiver is ready
std::this_thread::sleep_for(std::chrono::milliseconds(10));

// Create destination endpoint
asio::ip::udp::endpoint destination(asio::ip::address_v4::loopback(), 14000);
Expand All @@ -223,44 +227,55 @@ TEST(EcalUdpSocket, SyncHelloWorldMessage)
ASSERT_FALSE(ec);
}

// Wait up to 1 second for the message to be received
received_messages.wait_for([](int v) { return v == 1; }, std::chrono::milliseconds(1000));

// Close the sockets
send_socket.close();
rev_socket.close();

recv_thread.join();
}

// Send and Receive a big message using the sync API
TEST(EcalUdpSocket, SyncBigMessage)
{
atomic_signalable<int> received_messages(0);

asio::io_context io_context; // Will never be started, as we are using the sync API exclusively

// Create a send and recieve socket
ecaludp::Socket send_socket(io_context, {'E', 'C', 'A', 'L'});
ecaludp::Socket rev_socket (io_context, {'E', 'C', 'A', 'L'});
ecaludp::Socket rcv_socket (io_context, {'E', 'C', 'A', 'L'});

// Create the message to send and fill it with random characters
std::string message_to_send(1024 * 1024, 'a');
std::generate(message_to_send.begin(), message_to_send.end(), []() { return static_cast<char>(std::rand()); });

// Create a thread that will receive a message
std::thread recv_thread([&rev_socket, &message_to_send]()
std::thread recv_thread([&rcv_socket, &message_to_send, &received_messages]()
{
// Open the socket
{
asio::error_code ec;
rev_socket.open(asio::ip::udp::v4(), ec);
rcv_socket.open(asio::ip::udp::v4(), ec);
ASSERT_FALSE(ec);
}

// Bind the socket
{
asio::error_code ec;
rev_socket.bind(asio::ip::udp::endpoint(asio::ip::address_v4::loopback(), 14000), ec);
rcv_socket.bind(asio::ip::udp::endpoint(asio::ip::address_v4::loopback(), 14000), ec);
ASSERT_FALSE(ec);
}

asio::ip::udp::endpoint sender_endpoint;

// Receive a message
asio::error_code ec;
auto received_buffer = rev_socket.receive_from(sender_endpoint, 0, ec);
auto received_buffer = rcv_socket.receive_from(sender_endpoint, 0, ec);

received_messages++;

ASSERT_FALSE(ec);

Expand All @@ -269,8 +284,8 @@ TEST(EcalUdpSocket, SyncBigMessage)
ASSERT_EQ(received_string, message_to_send);
});

// Wait a millisecond to make sure that the receiver is ready
std::this_thread::sleep_for(std::chrono::milliseconds(1));
// Wait 10 milliseconds to make sure that the receiver is ready
std::this_thread::sleep_for(std::chrono::milliseconds(10));

// Create destination endpoint
asio::ip::udp::endpoint destination(asio::ip::address_v4::loopback(), 14000);
Expand All @@ -285,5 +300,12 @@ TEST(EcalUdpSocket, SyncBigMessage)
ASSERT_FALSE(ec);
}

// Wait up to 1 second for the message to be received
received_messages.wait_for([](int v) { return v == 1; }, std::chrono::milliseconds(1000));

// Close the sockets
send_socket.close();
rcv_socket.close();

recv_thread.join();
}

0 comments on commit baac1a9

Please sign in to comment.