Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do store command timeout in the context for redisSetTimeout (#593) #1093

Merged
merged 2 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
/* Defined in hiredis.c */
void __redisSetError(redisContext *c, int type, const char *str);

int redisContextUpdateCommandTimeout(redisContext *c, const struct timeval *timeout);

void redisNetClose(redisContext *c) {
if (c && c->fd != REDIS_INVALID_FD) {
close(c->fd);
Expand Down Expand Up @@ -333,6 +335,10 @@ int redisContextSetTimeout(redisContext *c, const struct timeval tv) {
const void *to_ptr = &tv;
size_t to_sz = sizeof(tv);

if (redisContextUpdateCommandTimeout(c, &tv) != REDIS_OK) {
__redisSetError(c, REDIS_ERR_OOM, "Out of memory");
return REDIS_ERR;
}
if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,to_ptr,to_sz) == -1) {
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
return REDIS_ERR;
Expand Down
29 changes: 19 additions & 10 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ static int tests = 0, fails = 0, skips = 0;
#define test_cond(_c) if(_c) printf("\033[0;32mPASSED\033[0;0m\n"); else {printf("\033[0;31mFAILED\033[0;0m\n"); fails++;}
#define test_skipped() { printf("\033[01;33mSKIPPED\033[0;0m\n"); skips++; }

static void millisleep(int ms)
{
#if _MSC_VER
Sleep(ms);
#else
usleep(ms*1000);
#endif
}

static long long usec(void) {
#ifndef _MSC_VER
struct timeval tv;
Expand Down Expand Up @@ -1156,7 +1165,13 @@ static void test_blocking_connection_timeouts(struct config config) {
test("Does not return a reply when the command times out: ");
if (detect_debug_sleep(c)) {
redisAppendFormattedCommand(c, sleep_cmd, strlen(sleep_cmd));

// flush connection buffer without waiting for the reply
s = c->funcs->write(c);
assert(s == (ssize_t)sdslen(c->obuf));
sdsfree(c->obuf);
c->obuf = sdsempty();

tv.tv_sec = 0;
tv.tv_usec = 10000;
redisSetTimeout(c, tv);
Expand All @@ -1169,6 +1184,9 @@ static void test_blocking_connection_timeouts(struct config config) {
strcmp(c->errstr, "recv timeout") == 0);
#endif
freeReplyObject(reply);

// wait for the DEBUG SLEEP to complete so that Redis server is unblocked for the following tests
millisleep(3000);
} else {
test_skipped();
}
Expand Down Expand Up @@ -1929,15 +1947,6 @@ struct _astest {
};
static struct _astest astest;

static void asSleep(int ms)
{
#if _MSC_VER
Sleep(ms);
#else
usleep(ms*1000);
#endif
}

/* async callbacks */
static void asCleanup(void* data)
{
Expand Down Expand Up @@ -2118,7 +2127,7 @@ static void test_async_polling(struct config config) {
while(astest.connected == 0)
redisPollTick(c, 0.1);
/* sleep 0.1 s, allowing old timeout to arrive */
asSleep(10);
millisleep(10);
status = redisAsyncCommand(c, commandCallback, NULL, "PING");
assert(status == REDIS_OK);
while(astest.ac)
Expand Down