Skip to content

Commit

Permalink
Merge pull request #898 from jphickey/fix-857-doselect-calc
Browse files Browse the repository at this point in the history
Fix #857, correct interval calculation in DoSelect

When calculating the relative time interval for the select() call,
the increment should have been a decrement.

This also adds a UT case that specifically exercises the carryover
described.
  • Loading branch information
astrogeco committed Mar 19, 2021
2 parents cd7c3d1 + 4d7fc54 commit efc29be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/os/portable/os-impl-bsd-select.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static int32 OS_DoSelect(int maxfd, fd_set *rd_set, fd_set *wr_set, int32 msecs)
if (tv.tv_usec < 0)
{
tv.tv_usec += 1000000;
++tv.tv_sec;
--tv.tv_sec;
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/unit-test-coverage/portable/src/coveragetest-bsd-select.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "os-shared-idmap.h"

#include "OCS_sys_select.h"
#include "OCS_errno.h"

void Test_OS_SelectSingle_Impl(void)
{
Expand All @@ -39,6 +40,7 @@ void Test_OS_SelectSingle_Impl(void)
OS_object_token_t token;
struct OCS_timespec nowtime;
struct OCS_timespec latertime;
struct OCS_timespec latertime2;

memset(&token, 0, sizeof(token));

Expand All @@ -52,6 +54,23 @@ void Test_OS_SelectSingle_Impl(void)
SelectFlags = 0;
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 0), OS_SUCCESS);

/* try a case where select() needs to be repeated to achieve the desired wait time */
UT_SetDefaultReturnValue(UT_KEY(OCS_select), -1);
OCS_errno = OCS_EINTR;
UT_SetDeferredRetcode(UT_KEY(OCS_select), 2, 0);
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
nowtime.tv_sec = 1;
nowtime.tv_nsec = 0;
latertime.tv_sec = 1;
latertime.tv_nsec = 800000000;
latertime2.tv_sec = 2;
latertime2.tv_nsec = 200000000;
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &nowtime, sizeof(nowtime), false);
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &nowtime, sizeof(nowtime), false);
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &latertime, sizeof(latertime), false);
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &latertime2, sizeof(latertime2), false);
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 1200), OS_ERROR_TIMEOUT);

UT_SetDefaultReturnValue(UT_KEY(OCS_select), 0);
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
nowtime.tv_sec = 1;
Expand All @@ -63,6 +82,7 @@ void Test_OS_SelectSingle_Impl(void)
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 999), OS_ERROR_TIMEOUT);

UT_SetDefaultReturnValue(UT_KEY(OCS_select), -1);
OCS_errno = OCS_ETIMEDOUT;
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
nowtime.tv_sec = 1;
nowtime.tv_nsec = 0;
Expand Down

0 comments on commit efc29be

Please sign in to comment.