Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
aae40e49
Commit
aae40e49
authored
Jan 19, 2016
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
io: io_time_override to insert fake times.
Signed-off-by:
Rusty Russell
<
rusty@rustcorp.com.au
>
parent
3022d16d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
1 deletion
+111
-1
ccan/io/io.h
ccan/io/io.h
+10
-0
ccan/io/poll.c
ccan/io/poll.c
+9
-1
ccan/io/test/run-20-io_time_override.c
ccan/io/test/run-20-io_time_override.c
+92
-0
No files found.
ccan/io/io.h
View file @
aae40e49
...
@@ -649,6 +649,16 @@ void *io_loop(struct timers *timers, struct timer **expired);
...
@@ -649,6 +649,16 @@ void *io_loop(struct timers *timers, struct timer **expired);
*/
*/
int
io_conn_fd
(
const
struct
io_conn
*
conn
);
int
io_conn_fd
(
const
struct
io_conn
*
conn
);
/**
* io_time_override - override the normal call for time.
* @nowfn: the function to call.
*
* io usually uses time_now() internally, but this forces it
* to use your function (eg. for debugging). Returns the old
* one.
*/
struct
timeabs
(
*
io_time_override
(
struct
timeabs
(
*
now
)(
void
)))(
void
);
/**
/**
* io_set_debug - set synchronous mode on a connection.
* io_set_debug - set synchronous mode on a connection.
* @conn: the connection.
* @conn: the connection.
...
...
ccan/io/poll.c
View file @
aae40e49
...
@@ -16,6 +16,14 @@ static struct pollfd *pollfds = NULL;
...
@@ -16,6 +16,14 @@ static struct pollfd *pollfds = NULL;
static
struct
fd
**
fds
=
NULL
;
static
struct
fd
**
fds
=
NULL
;
static
LIST_HEAD
(
closing
);
static
LIST_HEAD
(
closing
);
static
LIST_HEAD
(
always
);
static
LIST_HEAD
(
always
);
static
struct
timeabs
(
*
nowfn
)(
void
)
=
time_now
;
struct
timeabs
(
*
io_time_override
(
struct
timeabs
(
*
now
)(
void
)))(
void
)
{
struct
timeabs
(
*
old
)(
void
)
=
nowfn
;
nowfn
=
now
;
return
old
;
}
static
bool
add_fd
(
struct
fd
*
fd
,
short
events
)
static
bool
add_fd
(
struct
fd
*
fd
,
short
events
)
{
{
...
@@ -256,7 +264,7 @@ void *io_loop(struct timers *timers, struct timer **expired)
...
@@ -256,7 +264,7 @@ void *io_loop(struct timers *timers, struct timer **expired)
if
(
timers
)
{
if
(
timers
)
{
struct
timeabs
now
,
first
;
struct
timeabs
now
,
first
;
now
=
time_now
();
now
=
nowfn
();
/* Call functions for expired timers. */
/* Call functions for expired timers. */
*
expired
=
timers_expire
(
timers
,
now
);
*
expired
=
timers_expire
(
timers
,
now
);
...
...
ccan/io/test/run-20-io_time_override.c
0 → 100644
View file @
aae40e49
#include <ccan/io/io.h>
/* Include the C files directly. */
#include <ccan/io/poll.c>
#include <ccan/io/io.c>
#include <ccan/tap/tap.h>
#include <sys/wait.h>
#include <stdio.h>
#define PORT "65020"
static
struct
io_plan
*
init_conn
(
struct
io_conn
*
conn
,
void
*
unused
)
{
return
io_close
(
conn
);
}
static
int
make_listen_fd
(
const
char
*
port
,
struct
addrinfo
**
info
)
{
int
fd
,
on
=
1
;
struct
addrinfo
*
addrinfo
,
hints
;
memset
(
&
hints
,
0
,
sizeof
(
hints
));
hints
.
ai_family
=
AF_UNSPEC
;
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_flags
=
AI_PASSIVE
;
hints
.
ai_protocol
=
0
;
if
(
getaddrinfo
(
NULL
,
port
,
&
hints
,
&
addrinfo
)
!=
0
)
return
-
1
;
fd
=
socket
(
addrinfo
->
ai_family
,
addrinfo
->
ai_socktype
,
addrinfo
->
ai_protocol
);
if
(
fd
<
0
)
return
-
1
;
setsockopt
(
fd
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
on
,
sizeof
(
on
));
if
(
bind
(
fd
,
addrinfo
->
ai_addr
,
addrinfo
->
ai_addrlen
)
!=
0
)
{
close
(
fd
);
return
-
1
;
}
if
(
listen
(
fd
,
1
)
!=
0
)
{
close
(
fd
);
return
-
1
;
}
*
info
=
addrinfo
;
return
fd
;
}
static
struct
timeabs
fake_time
;
static
struct
timeabs
get_fake_time
(
void
)
{
return
fake_time
;
}
int
main
(
void
)
{
struct
io_listener
*
l
;
int
fd
;
struct
timers
timers
;
struct
timer
timer
,
*
expired
;
struct
addrinfo
*
addrinfo
;
/* This is how many tests you plan to run */
plan_tests
(
7
);
fake_time
=
time_now
();
timers_init
(
&
timers
,
fake_time
);
timer_init
(
&
timer
);
timer_add
(
&
timers
,
&
timer
,
timeabs_add
(
fake_time
,
time_from_sec
(
1000
)));
fd
=
make_listen_fd
(
PORT
,
&
addrinfo
);
freeaddrinfo
(
addrinfo
);
ok1
(
fd
>=
0
);
l
=
io_new_listener
(
NULL
,
fd
,
init_conn
,
NULL
);
ok1
(
l
);
fake_time
.
ts
.
tv_sec
+=
1000
;
ok1
(
io_time_override
(
get_fake_time
)
==
time_now
);
ok1
(
io_loop
(
&
timers
,
&
expired
)
==
NULL
);
ok1
(
expired
==
&
timer
);
ok1
(
!
timers_expire
(
&
timers
,
fake_time
));
ok1
(
io_time_override
(
time_now
)
==
get_fake_time
);
io_close_listener
(
l
);
timers_cleanup
(
&
timers
);
/* This exits depending on whether all tests passed */
return
exit_status
();
}
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