Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
945aae00
Commit
945aae00
authored
Jan 05, 2012
by
Pieter Noordhuis
Browse files
Test reading EOF
parent
0a66b7c0
Changes
3
Hide whitespace changes
Inline
Side-by-side
test/net-helper.c
View file @
945aae00
...
@@ -7,16 +7,26 @@
...
@@ -7,16 +7,26 @@
#include <netinet/tcp.h>
#include <netinet/tcp.h>
#include "net-helper.h"
#include "net-helper.h"
void
accept_and_ignore
(
void
*
ptr
)
{
void
run_server
(
void
*
ptr
)
{
accept_and_ignore_args
*
args
=
ptr
;
run_server_args
*
args
=
ptr
;
int
fd
,
rv
;
int
family
=
args
->
address
.
sa_family
;
int
fd
;
int
rv
;
fd
=
socket
(
args
->
address
.
sa_
family
,
SOCK_STREAM
,
0
);
fd
=
socket
(
family
,
SOCK_STREAM
,
0
);
if
(
fd
==
-
1
)
{
if
(
fd
==
-
1
)
{
fprintf
(
stderr
,
"socket: %s
\n
"
,
strerror
(
errno
));
fprintf
(
stderr
,
"socket: %s
\n
"
,
strerror
(
errno
));
exit
(
1
);
exit
(
1
);
}
}
if
(
family
==
AF_INET
||
family
==
AF_INET6
)
{
int
on
=
1
;
if
(
setsockopt
(
fd
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
on
,
sizeof
(
on
))
==
-
1
)
{
fprintf
(
stderr
,
"setsockopt: %s
\n
"
,
strerror
(
errno
));
exit
(
1
);
}
}
rv
=
bind
(
fd
,
&
args
->
address
.
sa_addr
.
addr
,
args
->
address
.
sa_addrlen
);
rv
=
bind
(
fd
,
&
args
->
address
.
sa_addr
.
addr
,
args
->
address
.
sa_addrlen
);
if
(
rv
==
-
1
)
{
if
(
rv
==
-
1
)
{
fprintf
(
stderr
,
"bind: %s
\n
"
,
strerror
(
errno
));
fprintf
(
stderr
,
"bind: %s
\n
"
,
strerror
(
errno
));
...
@@ -40,6 +50,9 @@ void accept_and_ignore(void *ptr) {
...
@@ -40,6 +50,9 @@ void accept_and_ignore(void *ptr) {
exit
(
1
);
exit
(
1
);
}
}
/* Ignore the new connection */
/* Execute specified function, if given */
if
(
args
->
fn
.
ptr
!=
NULL
)
{
args
->
fn
.
ptr
(
rv
,
args
->
fn
.
data
);
}
}
}
}
}
test/net-helper.h
View file @
945aae00
...
@@ -3,12 +3,16 @@
...
@@ -3,12 +3,16 @@
#include "../handle.h"
#include "../handle.h"
typedef
struct
accept_and_ignore_args_s
accept_and_ignore
_args
;
typedef
struct
run_server_args_s
run_server
_args
;
struct
accept_and_ignore
_args_s
{
struct
run_server
_args_s
{
redis_address
address
;
redis_address
address
;
struct
{
void
(
*
ptr
)(
int
fd
,
void
*
data
);
void
*
data
;
}
fn
;
};
};
void
accept_and_ignore
(
void
*
ptr
);
void
run_server
(
void
*
ptr
);
#endif
#endif
test/test-context.c
View file @
945aae00
...
@@ -15,6 +15,9 @@
...
@@ -15,6 +15,9 @@
#include <sys/socket.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netdb.h>
/* close */
#include <unistd.h>
/* local */
/* local */
#include "../context.h"
#include "../context.h"
#include "../object.h"
#include "../object.h"
...
@@ -257,10 +260,10 @@ TEST(flush_against_full_kernel_buffer) {
...
@@ -257,10 +260,10 @@ TEST(flush_against_full_kernel_buffer) {
redis_address
addr
=
redis_address_in
(
"127.0.0.1"
,
redis_port
()
+
1
);
redis_address
addr
=
redis_address_in
(
"127.0.0.1"
,
redis_port
()
+
1
);
accept_and_ignore
_args
args
;
run_server
_args
args
;
args
.
address
=
addr
;
args
.
address
=
addr
;
spawn
(
accept_and_ignore
,
&
args
);
spawn
(
run_server
,
&
args
);
rv
=
redis_context_connect_in
(
&
c
,
addr
.
sa_addr
.
in
);
rv
=
redis_context_connect_in
(
&
c
,
addr
.
sa_addr
.
in
);
assert_equal_return
(
rv
,
REDIS_OK
);
assert_equal_return
(
rv
,
REDIS_OK
);
...
@@ -282,6 +285,37 @@ TEST(flush_against_full_kernel_buffer) {
...
@@ -282,6 +285,37 @@ TEST(flush_against_full_kernel_buffer) {
assert_equal_int
(
errno
,
ETIMEDOUT
);
assert_equal_int
(
errno
,
ETIMEDOUT
);
}
}
void
close_after_accept
(
int
fd
,
void
*
data
)
{
close
(
fd
);
}
TEST
(
read_against_closed_connection
)
{
SETUP_CONNECT
();
redis_address
addr
=
redis_address_in
(
"127.0.0.1"
,
redis_port
()
+
1
);
run_server_args
args
;
args
.
address
=
addr
;
args
.
fn
.
ptr
=
close_after_accept
;
spawn
(
run_server
,
&
args
);
rv
=
redis_context_connect_in
(
&
c
,
addr
.
sa_addr
.
in
);
assert_equal_return
(
rv
,
REDIS_OK
);
redis_protocol
*
reply
=
NULL
;
/* Read should immediately EOF */
rv
=
redis_context_read
(
&
c
,
&
reply
);
assert_equal_return
(
rv
,
REDIS_EEOF
);
/* ... and should be idempotent... */
rv
=
redis_context_read
(
&
c
,
&
reply
);
assert_equal_return
(
rv
,
REDIS_EEOF
);
redis_context_destroy
(
&
c
);
}
int
main
(
void
)
{
int
main
(
void
)
{
test_connect_in_refused
();
test_connect_in_refused
();
test_connect_in6_refused
();
test_connect_in6_refused
();
...
@@ -297,4 +331,5 @@ int main(void) {
...
@@ -297,4 +331,5 @@ int main(void) {
test_call_command
();
test_call_command
();
test_call_command_argv
();
test_call_command_argv
();
test_flush_against_full_kernel_buffer
();
test_flush_against_full_kernel_buffer
();
test_read_against_closed_connection
();
}
}
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