Skip to content

Commit

Permalink
Added Stream Communication Test with Send and Recv
Browse files Browse the repository at this point in the history
- Contains a short test for stream communications between
  server and client.
- Server sends a message to client and client receives the
  message and then a comparison is done to see if the messages
  match.
- There is a while loop which loops if the message for some reason
  was not completely sent. In that case, it is the users'
  responsibilities to resend the remaining message.

Issue: eclipse-omr#5069

Signed-off-by: Haley Cao <haleycao88@hotmail.com>
  • Loading branch information
Haley Cao committed Jun 4, 2020
1 parent aa69e5b commit 96bbd2c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions fvtest/porttest/omrsockTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,41 @@ TEST(PortSockTest, two_socket_stream_communication)
omrsock_socket_t connectedClientStreamSocket = NULL;
EXPECT_EQ(OMRPORTLIB->sock_accept(OMRPORTLIB, serverStreamSocket, &connectedClientStreamSockAddr, &connectedClientStreamSocket), 0);

char msg[100] = "This is an omrsock test for 2 socket stream communications.";
int32_t bytesLeft = strlen(msg) + 1;
uint8_t *cursor = (uint8_t *)msg;
int32_t bytesSent = 0;
while(1) {
bytesSent = OMRPORTLIB->sock_send(OMRPORTLIB, connectedClientStreamSocket, cursor, bytesLeft, 0);
if (bytesSent < 0) {
FAIL();
break;
}
bytesLeft -= bytesSent;
cursor += bytesSent;
if (0 == bytesLeft) {
break;
}
}

char buf[100] = "";
bytesLeft = strlen(msg) + 1;
cursor = (uint8_t *)buf;
int32_t bytesRecv = 0;
while(1) {
bytesRecv = OMRPORTLIB->sock_recv(OMRPORTLIB, clientStreamSocket, cursor, bytesLeft, 0);
if (bytesRecv < 0) {
FAIL();
break;
}
bytesLeft -= bytesRecv;
cursor += bytesRecv;
if (0 == bytesLeft) {
break;
}
}
EXPECT_EQ(strcmp(msg, buf), 0);

EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &connectedClientStreamSocket), 0);
EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &clientStreamSocket), 0);
EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverStreamSocket), 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 @@ -475,7 +475,7 @@ omrsock_bind(struct OMRPortLibrary *portLibrary, omrsock_socket_t sock, omrsock_

if (bind(sock->data, (struct sockaddr *)&addr->data, addrlength) < 0) {
portLibrary->error_set_last_error(portLibrary, errno, OMRPORT_ERROR_SOCK_BIND_FAILED);
return OMRPORT_ERROR_SOCK_BIND_FAILED;
return errno;
}

return 0;
Expand Down

0 comments on commit 96bbd2c

Please sign in to comment.