Skip to content

Commit

Permalink
[test] Added test with bonding first connecting socket failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Małecki authored and maxsharabayko committed Sep 9, 2024
1 parent d2706ea commit 5a88e13
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions test/test_bonding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,80 @@ TEST(Bonding, Options)
srt_close(grp);
}

inline SRT_SOCKGROUPCONFIG PrepareEndpoint(const std::string& host, int port)
{
srt::sockaddr_any sa = srt::CreateAddr(host, port, AF_INET);
return srt_prepare_endpoint(NULL, sa.get(), sa.size());
}

// This test will create a listener and then the group that should
// connect members, where the first one fail, and two next should
// succeed. Then sends a single packet over that link and makes sure
// it's properly received, then the second packet isn't read.
TEST(Bonding, InitialFailure)
{
using namespace std;
using namespace srt;

TestInit srtinit;
MAKE_UNIQUE_SOCK(lsn, "Listener", srt_create_socket());
MAKE_UNIQUE_SOCK(grp, "GrpCaller", srt_create_group(SRT_GTYPE_BROADCAST));

// Create the listener on port 5555.
int allow = 1;
ASSERT_NE(srt_setsockflag(lsn, SRTO_GROUPCONNECT, &allow, sizeof allow), SRT_ERROR);

sockaddr_any sa = CreateAddr("127.0.0.1", 5555, AF_INET);
ASSERT_NE(srt_bind(lsn, sa.get(), sa.size()), SRT_ERROR);
ASSERT_NE(srt_listen(lsn, 5), SRT_ERROR);

// Create a group
// Connect 3 members in the group.
std::vector<SRT_SOCKGROUPCONFIG> targets;
targets.push_back(PrepareEndpoint("127.0.0.1", 5556)); // NOTE: NONEXISTENT LISTENER
targets.push_back(PrepareEndpoint("127.0.0.1", 5555));
targets.push_back(PrepareEndpoint("127.0.0.1", 5555));

// This should block until the connection is established, but
// accepted socket should be spawned and just wait for extraction.
const SRTSOCKET conn = srt_connect_group(grp, targets.data(), (int)targets.size());
EXPECT_NE(conn, SRT_INVALID_SOCK);

// Now check if the accept is ready
sockaddr_any revsa;
const SRTSOCKET gs = srt_accept(lsn, revsa.get(), &revsa.len);
EXPECT_NE(gs, SRT_INVALID_SOCK);

// Make sure that it was the group accepted
EXPECT_EQ(gs & SRTGROUP_MASK, SRTGROUP_MASK);

// Set 1s reading timeout on the socket so that reading won't wait forever,
// as it should fail at the second reading.
int read_timeout = 500; // 0.5s
EXPECT_NE(srt_setsockflag(gs, SRTO_RCVTIMEO, &read_timeout, sizeof (read_timeout)), SRT_ERROR);

int lsn_isn = -1, lsn_isn_size = sizeof (int);
EXPECT_NE(srt_getsockflag(gs, SRTO_ISN, &lsn_isn, &lsn_isn_size), SRT_ERROR);

// Now send a packet

string packet_data = "PREDEFINED PACKET DATA";
EXPECT_NE(srt_send(grp, packet_data.data(), packet_data.size()), SRT_ERROR);

char outbuf[1316];
SRT_MSGCTRL mc = srt_msgctrl_default;
int recvlen = srt_recvmsg2(gs, outbuf, 1316, &mc);
EXPECT_EQ(recvlen, packet_data.size());
outbuf[recvlen] = 0;

EXPECT_EQ(outbuf, packet_data);
EXPECT_EQ(mc.pktseq, lsn_isn);

recvlen = srt_recv(gs, outbuf, 80);
EXPECT_EQ(recvlen, SRT_ERROR);

srt_close(gs);
srt_close(grp);
srt_close(lsn);
}

0 comments on commit 5a88e13

Please sign in to comment.