-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Kilemonn/concurrent-bind-tests
Prebind callback on UDP Socket + Concurrent bind tests
- Loading branch information
Showing
7 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# docker build -f .\Environment-Test-Dockerfile . | ||
|
||
FROM alpine:3.20.0 AS alpine | ||
|
||
WORKDIR /alpine | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "../../src/socket/UDPSocket.h" | ||
#include "../../src/serversocket/ServerSocket.h" | ||
|
||
namespace kt | ||
{ | ||
/** | ||
* Ensure that we can bind to the same port using TCP and UDP sockets. | ||
* | ||
* In this case, when UDP binds first. | ||
*/ | ||
TEST(ScenarioTest, UDPThenTCPBindSamePort) | ||
{ | ||
kt::UDPSocket socket; | ||
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(); | ||
ASSERT_TRUE(bindResult.first); | ||
ASSERT_NE(std::nullopt, socket.getListeningPort()); | ||
|
||
kt::ServerSocket server(SocketType::Wifi, std::nullopt, socket.getListeningPort().value()); | ||
|
||
ASSERT_EQ(server.getPort(), socket.getListeningPort().value()); | ||
|
||
server.close(); | ||
socket.close(); | ||
} | ||
|
||
/** | ||
* Ensure that we can bind to the same port using TCP and UDP sockets. | ||
* | ||
* In this case, when TCP binds first. | ||
*/ | ||
TEST(ScenarioTest, TCPThenUDPBindSamePort) | ||
{ | ||
kt::ServerSocket server(SocketType::Wifi); | ||
|
||
kt::UDPSocket socket; | ||
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(std::nullopt, server.getPort()); | ||
ASSERT_TRUE(bindResult.first); | ||
ASSERT_NE(std::nullopt, socket.getListeningPort()); | ||
|
||
ASSERT_EQ(server.getPort(), socket.getListeningPort().value()); | ||
|
||
server.close(); | ||
socket.close(); | ||
} | ||
|
||
/** | ||
* Ensure that we can bind two UDP sockets to the same address by setting SO_REUSEADDR in the pre-bind handler. | ||
*/ | ||
TEST(ScenarioTest, TwoUDPSocketsBindingToSamePort) | ||
{ | ||
std::function setReuseAddrOption = [](SOCKET& s) { | ||
const int enableOption = 1; | ||
ASSERT_EQ(0, setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char*)&enableOption, sizeof(enableOption))); | ||
}; | ||
|
||
kt::UDPSocket socket; | ||
socket.setPreBindSocketOperation(setReuseAddrOption); | ||
std::pair<bool, kt::SocketAddress> bindResult = socket.bind(); | ||
ASSERT_TRUE(bindResult.first); | ||
|
||
kt::UDPSocket socket2; | ||
socket2.setPreBindSocketOperation(setReuseAddrOption); | ||
bindResult = socket2.bind(std::nullopt, socket.getListeningPort().value()); | ||
ASSERT_TRUE(bindResult.first); | ||
|
||
ASSERT_EQ(socket.getListeningPort().value(), socket2.getListeningPort().value()); | ||
|
||
kt::UDPSocket sendSocket; | ||
const std::string data = "TwoUDPSocketsBindingToSamePort"; | ||
std::pair<std::pair<bool, int>, kt::SocketAddress> sendResult = sendSocket.sendTo("localhost", socket.getListeningPort().value(), data); | ||
ASSERT_TRUE(sendResult.first.first); | ||
|
||
// Make sure only one of the sockets is ready to read, not both | ||
ASSERT_FALSE(socket.ready() && socket2.ready()); | ||
ASSERT_TRUE(socket2.ready() || socket.ready()); | ||
|
||
socket.close(); | ||
socket2.close(); | ||
|
||
sendSocket.close(); | ||
} | ||
} |