Skip to content

Commit

Permalink
Added new test case using sendto and recvfrom functions
Browse files Browse the repository at this point in the history
Added to two_socket_communication test in omrsockTest.cpp

- Added test case that involves datagram communication.
- Set timeout for sendto and recvfrom to be 2 seconds.
- However, because of the inconsistency in receiving the message
  for datagram communication, the message is sent 50 times and
  received using recvfrom for up to 50 times until it receives
  the same number of bytes as it sent.

Issue: eclipse-omr#5128

Signed-off-by: Haley Cao <haleycao88@hotmail.com>
  • Loading branch information
Haley Cao committed Jun 16, 2020
1 parent b1ae834 commit bd36531
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions fvtest/porttest/omrsockTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,37 @@ TEST(PortSockTest, two_socket_datagram_communication)
/* Connect is optional for datagram clients */
EXPECT_EQ(connect_client_to_server(OMRPORTLIB, (char *)"localhost", NULL, OMRSOCK_AF_INET, OMRSOCK_DGRAM, &clientSocket, &clientSockAddr, &serverSockAddr), 0);

/* Datagram Sendto and Recvfrom test. Sendto and Recvfrom are called multiple tests to ensure it has been
* sent and received.
*/
OMRTimeval timeVal;
EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeVal, 2, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, clientSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, clientSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeVal), 0);

/* Send 50 times. This should be enough for the message to be sent. */
char msgDgram[100] = "This is an omrsock test for datagram communications.";
int32_t bytesLeft = strlen(msgDgram) + 1;

for (int32_t i = 0; i < 50; i++) {
OMRPORTLIB->sock_sendto(OMRPORTLIB, clientSocket, (uint8_t *)msgDgram, bytesLeft, 0, &serverSockAddr);
}

/* Receive */
char bufDgram[100] = "";
int32_t bytesRecv = 0;

for (int32_t i = 0; i < 50; i++) {
bytesRecv = OMRPORTLIB->sock_recvfrom(OMRPORTLIB, serverSocket, (uint8_t *)bufDgram, bytesLeft, 0, &clientSockAddr);
if (bytesRecv == bytesLeft) {
break;
}
}

EXPECT_EQ(strcmp(msgDgram, bufDgram), 0);

EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &clientSocket), 0);
EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverSocket), 0);
}
Expand Down

0 comments on commit bd36531

Please sign in to comment.