Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added explicit closing the sockets. To prevent messing with UDP sockets keeping ports busy. #2807

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions test/test_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ class TestInit

};

class UniqueSocket
{
int32_t sock;

public:
UniqueSocket(int32_t s): sock(s)
{
if (s == -1)
throw std::invalid_argument("Invalid socket");
}

UniqueSocket(): sock(-1)
{
}

~UniqueSocket();

operator int32_t() const
{
return sock;
}

int32_t& ref() { return sock; }

/*
IF NEEDED, MOVE to test_main.cpp
UniqueSocket& operator=(int32_t s)
{
if (sock == s)
return;
srt_close(sock);
sock = s;
}
*/
};

class Test: public testing::Test
{
std::unique_ptr<TestInit> init_holder;
Expand Down
27 changes: 13 additions & 14 deletions test/test_epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ TEST(CEPoll, WaitEmptyCall)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
ASSERT_NE(client_sock, SRT_ERROR);

const int no = 0;
Expand All @@ -89,7 +89,7 @@ TEST(CEPoll, UWaitEmptyCall)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
ASSERT_NE(client_sock, SRT_ERROR);

const int no = 0;
Expand All @@ -112,7 +112,7 @@ TEST(CEPoll, WaitAllSocketsInEpollReleased)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
ASSERT_NE(client_sock, SRT_ERROR);

const int yes = 1;
Expand Down Expand Up @@ -146,7 +146,7 @@ TEST(CEPoll, WaitAllSocketsInEpollReleased2)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
ASSERT_NE(client_sock, SRT_ERROR);

const int yes = 1;
Expand All @@ -168,14 +168,13 @@ TEST(CEPoll, WaitAllSocketsInEpollReleased2)
ASSERT_EQ(srt_epoll_uwait(epoll_id, events, 2, -1), SRT_ERROR);

EXPECT_EQ(srt_epoll_release(epoll_id), 0);

}

TEST(CEPoll, WrongEpoll_idOnAddUSock)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
ASSERT_NE(client_sock, SRT_ERROR);

const int no = 0;
Expand All @@ -198,7 +197,7 @@ TEST(CEPoll, HandleEpollEvent)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
EXPECT_NE(client_sock, SRT_ERROR);

const int yes = 1;
Expand Down Expand Up @@ -259,7 +258,7 @@ TEST(CEPoll, NotifyConnectionBreak)
srt::TestInit srtinit;

// 1. Prepare client
SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
ASSERT_NE(client_sock, SRT_ERROR);

const int yes SRT_ATR_UNUSED = 1;
Expand All @@ -281,7 +280,7 @@ TEST(CEPoll, NotifyConnectionBreak)
ASSERT_EQ(inet_pton(AF_INET, "127.0.0.1", &sa_client.sin_addr), 1);

// 2. Prepare server
SRTSOCKET server_sock = srt_create_socket();
srt::UniqueSocket server_sock = srt_create_socket();
ASSERT_NE(server_sock, SRT_ERROR);

ASSERT_NE(srt_setsockopt(server_sock, 0, SRTO_RCVSYN, &no, sizeof no), SRT_ERROR); // for async connect
Expand Down Expand Up @@ -373,7 +372,7 @@ TEST(CEPoll, HandleEpollEvent2)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
EXPECT_NE(client_sock, SRT_ERROR);

const int yes = 1;
Expand Down Expand Up @@ -434,7 +433,7 @@ TEST(CEPoll, HandleEpollNoEvent)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
EXPECT_NE(client_sock, SRT_ERROR);

const int yes = 1;
Expand Down Expand Up @@ -484,7 +483,7 @@ TEST(CEPoll, ThreadedUpdate)
{
srt::TestInit srtinit;

SRTSOCKET client_sock = srt_create_socket();
srt::UniqueSocket client_sock = srt_create_socket();
EXPECT_NE(client_sock, SRT_ERROR);

const int no = 0;
Expand Down Expand Up @@ -754,8 +753,8 @@ class TestEPoll: public srt::Test

TEST_F(TestEPoll, SimpleAsync)
{
SRTSOCKET ss = SRT_INVALID_SOCK;
createServerSocket( (ss) );
srt::UniqueSocket ss;
createServerSocket( (ss.ref()) );
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

std::thread client([this] { clientSocket(); });

Expand Down
26 changes: 26 additions & 0 deletions test/test_fec_rebuilding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ TEST(TestFEC, ConfigExchange)
string exp_config = "fec,cols:10,rows:10,arq:never,layout:staircase";

EXPECT_TRUE(filterConfigSame(fec_configback, exp_config));
srt_close(sid1);
}

TEST(TestFEC, ConfigExchangeFaux)
Expand Down Expand Up @@ -273,6 +274,7 @@ TEST(TestFEC, ConfigExchangeFaux)
cout << "(NOTE: expecting a failure message)\n";
EXPECT_FALSE(m1.checkApplyFilterConfig("fec,cols:10,arq:never"));

srt_close(sid1);
}

TEST(TestFEC, Connection)
Expand Down Expand Up @@ -327,6 +329,9 @@ TEST(TestFEC, Connection)
EXPECT_TRUE(filterConfigSame(caller_config, fec_config_final));
EXPECT_TRUE(filterConfigSame(accept_config, fec_config_final));

srt_close(a);
srt_close(s);
srt_close(l);
}

TEST(TestFEC, ConnectionReorder)
Expand Down Expand Up @@ -379,6 +384,9 @@ TEST(TestFEC, ConnectionReorder)
EXPECT_TRUE(filterConfigSame(caller_config, fec_config_final));
EXPECT_TRUE(filterConfigSame(accept_config, fec_config_final));

srt_close(a);
srt_close(s);
srt_close(l);
}

TEST(TestFEC, ConnectionFull1)
Expand Down Expand Up @@ -431,6 +439,9 @@ TEST(TestFEC, ConnectionFull1)
EXPECT_TRUE(filterConfigSame(caller_config, fec_config_final));
EXPECT_TRUE(filterConfigSame(accept_config, fec_config_final));

srt_close(a);
srt_close(s);
srt_close(l);
}

TEST(TestFEC, ConnectionFull2)
Expand Down Expand Up @@ -483,6 +494,9 @@ TEST(TestFEC, ConnectionFull2)
EXPECT_TRUE(filterConfigSame(caller_config, fec_config_final));
EXPECT_TRUE(filterConfigSame(accept_config, fec_config_final));

srt_close(a);
srt_close(s);
srt_close(l);
}

TEST(TestFEC, ConnectionMess)
Expand Down Expand Up @@ -535,6 +549,9 @@ TEST(TestFEC, ConnectionMess)
EXPECT_TRUE(filterConfigSame(caller_config, fec_config_final));
EXPECT_TRUE(filterConfigSame(accept_config, fec_config_final));

srt_close(a);
srt_close(s);
srt_close(l);
}

TEST(TestFEC, ConnectionForced)
Expand Down Expand Up @@ -581,6 +598,9 @@ TEST(TestFEC, ConnectionForced)
EXPECT_TRUE(filterConfigSame(result_config1, fec_config_final));
EXPECT_TRUE(filterConfigSame(result_config2, fec_config_final));

srt_close(a);
srt_close(s);
srt_close(l);
}

TEST(TestFEC, RejectionConflict)
Expand Down Expand Up @@ -623,6 +643,8 @@ TEST(TestFEC, RejectionConflict)
int sclen = sizeof scl;
EXPECT_EQ(srt_accept(l, (sockaddr*)& scl, &sclen), SRT_ERROR);

srt_close(s);
srt_close(l);
}

TEST(TestFEC, RejectionIncompleteEmpty)
Expand Down Expand Up @@ -662,6 +684,8 @@ TEST(TestFEC, RejectionIncompleteEmpty)
int sclen = sizeof scl;
EXPECT_EQ(srt_accept(l, (sockaddr*)& scl, &sclen), SRT_ERROR);

srt_close(s);
srt_close(l);
}


Expand Down Expand Up @@ -705,6 +729,8 @@ TEST(TestFEC, RejectionIncomplete)
int sclen = sizeof scl;
EXPECT_EQ(srt_accept(l, (sockaddr*)& scl, &sclen), SRT_ERROR);

srt_close(s);
srt_close(l);
}

TEST_F(TestFECRebuilding, Prepare)
Expand Down
4 changes: 4 additions & 0 deletions test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,9 @@ sockaddr_any CreateAddr(const std::string& name, unsigned short port, int pref_f
return result;
}

UniqueSocket::~UniqueSocket()
{
srt_close(sock);
}

}
Loading