summaryrefslogtreecommitdiff
path: root/test/regress_watch.c
blob: 9e340618d6b46b219b9a57abf998c8385568c993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <time.h>

#include "event2/event.h"
#include "event2/watch.h"
#include "regress.h"

static int iteration = 0;
static int prepare_callback_1_count = 0;
static int prepare_callback_2_count = 0;
static int check_callback_1_count = 0;
static int check_callback_2_count = 0;
static struct timeval start_time = { 0, 0 };
static struct timeval end_time = { 0, 0 };
static int user_arg = 8675309;

static void
prepare_callback_1(struct evwatch *watcher, const struct evwatch_prepare_cb_info *info, void *arg)
{
	struct timeval timeout;
	int timeout_msec;

	/* user argument should be passed properly */
	tt_ptr_op(arg, ==, &user_arg);

	++prepare_callback_1_count;

	/* prepare_callback_1 should always fire before prepare_callback_2, and
	 * before both check callbacks */
	tt_int_op(prepare_callback_1_count, >, prepare_callback_2_count);
	tt_int_op(prepare_callback_1_count, >, check_callback_1_count);
	tt_int_op(prepare_callback_1_count, >, check_callback_2_count);

	/* if we've just scheduled the timeout event at the beginning of the
	 * iteration, save the current time and assert that the timeout is
	 * roughly what we set (this won't be exact on some platforms) */
	if (start_time.tv_sec == 0) {
		event_base_gettimeofday_cached(evwatch_base(watcher), &start_time);
		tt_int_op(evwatch_prepare_get_timeout(info, &timeout), ==, 1);

		timeout_msec = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000);
		tt_int_op(timeout_msec, >=, 995);
		tt_int_op(timeout_msec, <=, 1005);
	}
end:
	;
}

static void
prepare_callback_2(struct evwatch *watcher, const struct evwatch_prepare_cb_info *info, void *arg)
{
	/* user argument should be passed properly */
	tt_ptr_op(arg, ==, &user_arg);

	++prepare_callback_2_count;

	/* prepare_callback_2 should only fire on the first iteration, and
	* should fire before both check callbacks */
	tt_int_op(iteration, ==, 0);
	tt_int_op(prepare_callback_2_count, >, check_callback_1_count);
	tt_int_op(prepare_callback_2_count, >, check_callback_2_count);
end:
	;
}

static void
check_callback_1(struct evwatch *watcher, const struct evwatch_check_cb_info *info, void *arg)
{
	/* user argument should be passed properly */
	tt_ptr_op(arg, ==, &user_arg);

	++check_callback_1_count;

	/* check_callback_1 should always fire before check_callback_2 */
	tt_int_op(check_callback_1_count, >, check_callback_2_count);

	/* save the end time, in case the timeout fires this time through the
	 * event loop */
	event_base_gettimeofday_cached(evwatch_base(watcher), &end_time);
end:
	;
}

static void
check_callback_2(struct evwatch *watcher, const struct evwatch_check_cb_info *info, void *arg)
{
	/* user argument should be passed properly */
	tt_ptr_op(arg, ==, &user_arg);

	++check_callback_2_count;

	/* check_callback_2 should only fire on the first iteration */
	tt_int_op(iteration, ==, 0);
end:
	;
}

static void
timeout_callback(evutil_socket_t fd, short events, void *arg)
{
	/* the duration between the start and end times should be at least 1
	 * second */
	tt_int_op(end_time.tv_sec, >=, start_time.tv_sec + 1);
end:
	;
}

/**
  This tests a few important properties of "prepare" and "check" watchers:
    - Watchers should be called in the order they were registered.
    - Prepare watchers should be called before check watchers.
    - Freeing a watcher will stop callbacks to it, but not to other watchers.
    - Reported durations should align with the registered timeouts.
    - It should be possible to call back into libevent from a callback without a
      recursive lock.
    - If this test is compiled with ASAN or similar, this test also illustrates
      that event_base_free will free any watchers not previously freed by
      evwatch_free.
 */
static void
test_callback_ordering(void *ptr)
{
	struct basic_test_data *data = ptr;
	struct event_base *base = data->base;
	struct evwatch *prepare_callback_2_watcher;
	struct evwatch *check_callback_2_watcher;
	struct timeval timeout;

	/* install prepare and check watchers */
	evwatch_prepare_new(base, &prepare_callback_1, &user_arg);
	evwatch_check_new(base, &check_callback_1, &user_arg);
	prepare_callback_2_watcher = evwatch_prepare_new(base, &prepare_callback_2, &user_arg);
	check_callback_2_watcher = evwatch_check_new(base, &check_callback_2, &user_arg);

	/* schedule an 1 second timeout event, and run the event loop until the
	 * timeout fires */
	timeout.tv_sec = 1;
	timeout.tv_usec = 0;
	event_base_once(base, -1, EV_TIMEOUT, &timeout_callback, 0, &timeout);
	event_base_dispatch(base);

	/* second iteration: free two of the watchers, schedule a timeout and
	 * run the event loop again */
	iteration = 1;
	start_time.tv_sec = 0;
	evwatch_free(prepare_callback_2_watcher);
	evwatch_free(check_callback_2_watcher);
	event_base_once(base, -1, EV_TIMEOUT, &timeout_callback, 0, &timeout);
	event_base_dispatch(base);
}

static void
prepare_callback_3(struct evwatch *watcher, const struct evwatch_prepare_cb_info *info, void *arg)
{
	/* timeout should not be written to */
	struct timeval timeout = { 123, 456 };
	tt_int_op(evwatch_prepare_get_timeout(info, &timeout), ==, 0);
	tt_int_op(timeout.tv_sec, ==, 123);
	tt_int_op(timeout.tv_usec, ==, 456);
end:
	;
}

/**
  Test that evwatch_prepare_get_timeout behaves correctly when there is no
  timeout.
 */
static void
test_timeout_unavailable(void *ptr)
{
	struct basic_test_data *data = ptr;
	struct event_base *base = data->base;

	evwatch_prepare_new(base, &prepare_callback_3, NULL);
	event_base_dispatch(base);
}

#ifndef EVENT__DISABLE_MM_REPLACEMENT
static void *
bad_malloc(size_t sz)
{
	return NULL;
}

/**
  Test that creating prepare and check watchers fails gracefully if we can't
  allocate memory.
 */
static void
test_malloc_failure(void *ptr)
{
	struct basic_test_data *data = ptr;
	struct event_base *base = data->base;
	struct evwatch *bad_prepare, *bad_check;

	event_set_mem_functions(bad_malloc, realloc, free);
	bad_prepare = evwatch_prepare_new(base, &prepare_callback_1, NULL);
	tt_ptr_op(bad_prepare, ==, NULL);

	bad_check = evwatch_check_new(base, &check_callback_1, NULL);
	tt_ptr_op(bad_check, ==, NULL);

	event_set_mem_functions(malloc, realloc, free);
end:
	;
}
#endif

struct testcase_t watch_testcases[] = {
	BASIC(callback_ordering, TT_FORK|TT_NEED_BASE),
	BASIC(timeout_unavailable, TT_FORK|TT_NEED_BASE),
#ifndef EVENT__DISABLE_MM_REPLACEMENT
	BASIC(malloc_failure, TT_FORK|TT_NEED_BASE),
#endif
	END_OF_TESTCASES
};