Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
f9596db9
Commit
f9596db9
authored
Oct 18, 2010
by
Pieter Noordhuis
Browse files
Test callback sequence in non-blocking context
parent
e332a32b
Changes
2
Show whitespace changes
Inline
Side-by-side
hiredis.c
View file @
f9596db9
...
@@ -676,15 +676,7 @@ int redisBufferRead(redisContext *c) {
...
@@ -676,15 +676,7 @@ int redisBufferRead(redisContext *c) {
return
REDIS_OK
;
return
REDIS_OK
;
}
}
static
void
redisPopCallback
(
redisContext
*
c
)
{
if
(
c
->
cpos
>
1
)
{
memmove
(
&
c
->
callbacks
[
0
],
&
c
->
callbacks
[
1
],(
c
->
cpos
-
1
)
*
sizeof
(
redisCallback
));
}
c
->
cpos
--
;
}
int
redisGetReply
(
redisContext
*
c
,
void
**
reply
)
{
int
redisGetReply
(
redisContext
*
c
,
void
**
reply
)
{
redisPopCallback
(
c
);
if
(
redisReplyReaderGetReply
(
c
->
reader
,
reply
)
==
REDIS_ERR
)
{
if
(
redisReplyReaderGetReply
(
c
->
reader
,
reply
)
==
REDIS_ERR
)
{
/* Copy the (protocol) error from the reader to the context. */
/* Copy the (protocol) error from the reader to the context. */
c
->
error
=
sdsnew
(((
redisReader
*
)
c
->
reader
)
->
error
);
c
->
error
=
sdsnew
(((
redisReader
*
)
c
->
reader
)
->
error
);
...
@@ -693,24 +685,35 @@ int redisGetReply(redisContext *c, void **reply) {
...
@@ -693,24 +685,35 @@ int redisGetReply(redisContext *c, void **reply) {
return
REDIS_OK
;
return
REDIS_OK
;
}
}
static
void
redisPopCallback
(
redisContext
*
c
)
{
assert
(
c
->
cpos
>
0
);
if
(
c
->
cpos
>
1
)
memmove
(
&
c
->
callbacks
[
0
],
&
c
->
callbacks
[
1
],(
c
->
cpos
-
1
)
*
sizeof
(
redisCallback
));
c
->
cpos
--
;
}
int
redisProcessCallbacks
(
redisContext
*
c
)
{
int
redisProcessCallbacks
(
redisContext
*
c
)
{
void
*
reply
=
NULL
;
void
*
reply
=
NULL
;
redisCallback
cb
;
redisCallback
cb
;
do
{
/* Continue while there are callbacks */
while
(
c
->
cpos
>
0
)
{
cb
=
c
->
callbacks
[
0
];
cb
=
c
->
callbacks
[
0
];
if
(
redisGetReply
(
c
,
&
reply
)
==
REDIS_ERR
)
if
(
redisGetReply
(
c
,
&
reply
)
==
REDIS_ERR
)
return
REDIS_ERR
;
return
REDIS_ERR
;
/* Fire callback when there is a reply. */
if
(
reply
!=
NULL
)
{
if
(
reply
!=
NULL
)
{
redisPopCallback
(
c
);
if
(
cb
.
fn
!=
NULL
)
{
if
(
cb
.
fn
!=
NULL
)
{
cb
.
fn
(
c
,
reply
,
cb
.
privdata
);
cb
.
fn
(
c
,
reply
,
cb
.
privdata
);
}
else
{
}
else
{
c
->
fn
->
freeObject
(
reply
);
c
->
fn
->
freeObject
(
reply
);
}
}
}
else
{
/* Stop trying */
break
;
}
}
}
}
while
(
reply
!=
NULL
);
return
REDIS_OK
;
return
REDIS_OK
;
}
}
...
...
test.c
View file @
f9596db9
...
@@ -202,6 +202,15 @@ static void __test_callback(redisContext *c, const void *privdata) {
...
@@ -202,6 +202,15 @@ static void __test_callback(redisContext *c, const void *privdata) {
__test_callback_flags
|=
(
long
)
privdata
;
__test_callback_flags
|=
(
long
)
privdata
;
}
}
static
long
__test_reply_callback_flags
=
0
;
static
void
__test_reply_callback
(
redisContext
*
c
,
redisReply
*
reply
,
const
void
*
privdata
)
{
((
void
)
c
);
/* Shift to detect execution order */
__test_reply_callback_flags
<<=
8
;
__test_reply_callback_flags
|=
(
long
)
privdata
;
freeReplyObject
(
reply
);
}
static
void
test_nonblocking_connection
()
{
static
void
test_nonblocking_connection
()
{
redisContext
*
c
;
redisContext
*
c
;
int
wdone
=
0
;
int
wdone
=
0
;
...
@@ -249,6 +258,29 @@ static void test_nonblocking_connection() {
...
@@ -249,6 +258,29 @@ static void test_nonblocking_connection() {
test_cond
(
redisBufferWrite
(
c
,
NULL
)
==
REDIS_ERR
&&
test_cond
(
redisBufferWrite
(
c
,
NULL
)
==
REDIS_ERR
&&
strncmp
(
c
->
error
,
"write:"
,
6
)
==
0
);
strncmp
(
c
->
error
,
"write:"
,
6
)
==
0
);
redisFree
(
c
);
redisFree
(
c
);
wdone
=
__test_reply_callback_flags
=
0
;
test
(
"Process callbacks in the right sequence: "
);
c
=
redisConnectNonBlock
(
"127.0.0.1"
,
6379
,
NULL
);
redisCommandWithCallback
(
c
,
__test_reply_callback
,(
const
void
*
)
1
,
"PING"
);
redisCommandWithCallback
(
c
,
__test_reply_callback
,(
const
void
*
)
2
,
"PING"
);
redisCommandWithCallback
(
c
,
__test_reply_callback
,(
const
void
*
)
3
,
"PING"
);
/* Write output buffer */
while
(
!
wdone
)
{
usleep
(
500
);
redisBufferWrite
(
c
,
&
wdone
);
}
/* Read until at least one callback is executed (the 3 replies will
* arrive in a single packet, causing all callbacks to be executed in
* a single pass). */
while
(
__test_reply_callback_flags
==
0
)
{
assert
(
redisBufferRead
(
c
)
==
REDIS_OK
);
redisProcessCallbacks
(
c
);
}
test_cond
(
__test_reply_callback_flags
==
0x010203
);
redisFree
(
c
);
}
}
int
main
(
void
)
{
int
main
(
void
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment