diff --git a/fvtest/porttest/omrsockTest.cpp b/fvtest/porttest/omrsockTest.cpp index 33df65e9d45..b8305fac537 100644 --- a/fvtest/porttest/omrsockTest.cpp +++ b/fvtest/porttest/omrsockTest.cpp @@ -589,6 +589,40 @@ 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 < 100; 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 < 100; i++) { + bytesRecv = OMRPORTLIB->sock_recvfrom(OMRPORTLIB, serverSocket, (uint8_t *)bufDgram, bytesLeft, 0, &clientSockAddr); + if (bytesRecv == bytesLeft) { + 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); }