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 18, 2020
1 parent b1ae834 commit b50c88d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
45 changes: 43 additions & 2 deletions fvtest/porttest/omrsockTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ connect_client_to_server(struct OMRPortLibrary *portLibrary, const char *addrStr
if(0 == OMRPORTLIB->sock_socket(OMRPORTLIB, clientSocket, resultFamily, resultSocktype, resultProtocol)) {
EXPECT_NE(clientSocket, (void *)NULL);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_address(OMRPORTLIB, &result, i, clientSockAddr), 0);
EXPECT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, *clientSocket, clientSockAddr), 0);
break;
}
}
Expand Down Expand Up @@ -578,8 +579,7 @@ TEST(PortSockTest, two_socket_datagram_communication)
uint8_t serverAddr[4];

/* To Create a Server Socket and Address */
uint32_t inaddrAny = OMRPORTLIB->sock_htonl(OMRPORTLIB, OMRSOCK_INADDR_ANY);
memcpy(serverAddr, &inaddrAny, 4);
EXPECT_EQ(OMRPORTLIB->sock_inet_pton(OMRPORTLIB, OMRSOCK_AF_INET, "127.0.0.1", serverAddr), 0);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &serverSockAddr, OMRSOCK_AF_INET, serverAddr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
/* Datagram server sockets does not need to listen and accept */
EXPECT_EQ(start_server(OMRPORTLIB, OMRSOCK_AF_INET, OMRSOCK_DGRAM, &serverSocket, &serverSockAddr), 0);
Expand All @@ -589,6 +589,47 @@ 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, 3, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeSend), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, clientSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeRecv), 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 bytesTotal = strlen(msgDgram) + 1;
int32_t bytesSent = 0;

for (int32_t i = 0; i < 100; i++) {
bytesSent = OMRPORTLIB->sock_sendto(OMRPORTLIB, serverSocket, (uint8_t *)msgDgram, bytesTotal, 0, &clientSockAddr);
if (bytesSent == bytesTotal) {
printf("message sent at iter %d \n", i);
break;
}
}

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

for (int32_t i = 0; i < 100; i++) {
bytesRecv = OMRPORTLIB->sock_recvfrom(OMRPORTLIB, clientSocket, (uint8_t *)bufDgram, bytesTotal, 0, &serverSockAddr);
printf("iteration %d: bytesRecv is %d \n", i, bytesRecv);
if (bytesRecv == bytesTotal) {
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 b50c88d

Please sign in to comment.