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 17, 2020
1 parent b1ae834 commit 91a0336
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions fvtest/porttest/omrsockTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,44 @@ 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 timeSend;
OMRTimeval timeRecv;
EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeSend, 2, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeRecv, 20, 0), 0);
// EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, clientSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeRecv), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, clientSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeSend), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeRecv), 0);
// EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeSend), 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 < 200; 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 < 200; i++) {
bytesRecv = OMRPORTLIB->sock_recvfrom(OMRPORTLIB, serverSocket, (uint8_t *)bufDgram, bytesLeft, 0, &clientSockAddr);
printf("iteration %d: bytesRecv is %d \n", i, bytesRecv);
if (bytesRecv == bytesLeft) {
printf("break encountered at iter %d \n", i);
break;
}
}

printf("sent: %s \n", msgDgram);
printf("received: %s \n", bufDgram);

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

EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &clientSocket), 0);
EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverSocket), 0);
}
Expand Down
2 changes: 1 addition & 1 deletion port/unix/omrsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ omrsock_recvfrom(struct OMRPortLibrary *portLibrary, omrsock_socket_t sock, uint
}
if (-1 == bytesRecv) {
portLibrary->error_set_last_error(portLibrary, errno, OMRPORT_ERROR_SOCK_RECVFROM_FAILED);
return OMRPORT_ERROR_SOCK_RECVFROM_FAILED;
return errno;
}

return bytesRecv;
Expand Down

0 comments on commit 91a0336

Please sign in to comment.