Sun Dec 18 22:25:20 2011 UTC ()
test timer expiration.


(christos)
diff -r1.2 -r1.3 src/tests/lib/libc/sys/t_timer_create.c

cvs diff -r1.2 -r1.3 src/tests/lib/libc/sys/t_timer_create.c (expand / switch to unified diff)

--- src/tests/lib/libc/sys/t_timer_create.c 2011/09/17 18:52:21 1.2
+++ src/tests/lib/libc/sys/t_timer_create.c 2011/12/18 22:25:20 1.3
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: t_timer_create.c,v 1.2 2011/09/17 18:52:21 jruoho Exp $ */ 1/* $NetBSD: t_timer_create.c,v 1.3 2011/12/18 22:25:20 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,44 +27,41 @@ @@ -27,44 +27,41 @@
27 */ 27 */
28 28
29#include <atf-c.h> 29#include <atf-c.h>
30#include <errno.h> 30#include <errno.h>
31#include <stdio.h> 31#include <stdio.h>
32#include <signal.h> 32#include <signal.h>
33#include <string.h> 33#include <string.h>
34#include <time.h> 34#include <time.h>
35#include <unistd.h> 35#include <unistd.h>
36 36
37static timer_t t; 37static timer_t t;
38static bool fail = true; 38static bool fail = true;
39 39
40static void timer_signal_handler(int, siginfo_t *, void *); 
41static void timer_signal_create(clockid_t); 
42 
43static void 40static void
44timer_signal_handler(int signo, siginfo_t *si, void *osi) 41timer_signal_handler(int signo, siginfo_t *si, void *osi)
45{ 42{
46 timer_t *tp; 43 timer_t *tp;
47 44
48 tp = si->si_value.sival_ptr; 45 tp = si->si_value.sival_ptr;
49 46
50 if (*tp == t && signo == SIGALRM) 47 if (*tp == t && signo == SIGALRM)
51 fail = false; 48 fail = false;
52 49
53 (void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo)); 50 (void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo));
54} 51}
55 52
56static void 53static void
57timer_signal_create(clockid_t cid) 54timer_signal_create(clockid_t cid, bool expire)
58{ 55{
59 struct itimerspec tim; 56 struct itimerspec tim;
60 struct sigaction act; 57 struct sigaction act;
61 struct sigevent evt; 58 struct sigevent evt;
62 sigset_t set; 59 sigset_t set;
63 60
64 t = 0; 61 t = 0;
65 fail = true; 62 fail = true;
66 63
67 (void)memset(&evt, 0, sizeof(struct sigevent)); 64 (void)memset(&evt, 0, sizeof(struct sigevent));
68 (void)memset(&act, 0, sizeof(struct sigaction)); 65 (void)memset(&act, 0, sizeof(struct sigaction));
69 (void)memset(&tim, 0, sizeof(struct itimerspec)); 66 (void)memset(&tim, 0, sizeof(struct itimerspec));
70 67
@@ -86,36 +83,43 @@ timer_signal_create(clockid_t cid) @@ -86,36 +83,43 @@ timer_signal_create(clockid_t cid)
86 83
87 /* 84 /*
88 * Create the timer (SIGEV_SIGNAL). 85 * Create the timer (SIGEV_SIGNAL).
89 */ 86 */
90 evt.sigev_signo = SIGALRM; 87 evt.sigev_signo = SIGALRM;
91 evt.sigev_value.sival_ptr = &t; 88 evt.sigev_value.sival_ptr = &t;
92 evt.sigev_notify = SIGEV_SIGNAL; 89 evt.sigev_notify = SIGEV_SIGNAL;
93 90
94 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0); 91 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
95 92
96 /* 93 /*
97 * Start the timer. After this, unblock the signal. 94 * Start the timer. After this, unblock the signal.
98 */ 95 */
99 tim.it_value.tv_sec = 1; 96 tim.it_value.tv_sec = expire ? 5 : 1;
100 tim.it_value.tv_nsec = 0; 97 tim.it_value.tv_nsec = 0;
101 98
102 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0); 99 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
103 100
104 (void)sigprocmask(SIG_UNBLOCK, &set, NULL); 101 (void)sigprocmask(SIG_UNBLOCK, &set, NULL);
105 (void)sleep(2); 102 (void)sleep(2);
106 103
107 if (fail != false) 104 if (expire) {
108 atf_tc_fail("timer failed to fire"); 105 if (!fail)
 106 atf_tc_fail("timer fired too soon");
 107 } else {
 108 if (fail)
 109 atf_tc_fail("timer failed to fire");
 110 }
 111
 112 ATF_REQUIRE(timer_delete(t) == 0);
109} 113}
110 114
111ATF_TC(timer_create_err); 115ATF_TC(timer_create_err);
112ATF_TC_HEAD(timer_create_err, tc) 116ATF_TC_HEAD(timer_create_err, tc)
113{ 117{
114 /* Cf. PR lib/42434. */ 118 /* Cf. PR lib/42434. */
115 atf_tc_set_md_var(tc, "descr", "Check errors from timer_create(2)"); 119 atf_tc_set_md_var(tc, "descr", "Check errors from timer_create(2)");
116} 120}
117 121
118ATF_TC_BODY(timer_create_err, tc) 122ATF_TC_BODY(timer_create_err, tc)
119{ 123{
120 struct sigevent ev; 124 struct sigevent ev;
121 125
@@ -135,39 +139,69 @@ ATF_TC_BODY(timer_create_err, tc) @@ -135,39 +139,69 @@ ATF_TC_BODY(timer_create_err, tc)
135} 139}
136 140
137ATF_TC(timer_create_real); 141ATF_TC(timer_create_real);
138ATF_TC_HEAD(timer_create_real, tc) 142ATF_TC_HEAD(timer_create_real, tc)
139{ 143{
140 144
141 atf_tc_set_md_var(tc, "descr", 145 atf_tc_set_md_var(tc, "descr",
142 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), " 146 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
143 "SIGEV_SIGNAL"); 147 "SIGEV_SIGNAL");
144} 148}
145 149
146ATF_TC_BODY(timer_create_real, tc) 150ATF_TC_BODY(timer_create_real, tc)
147{ 151{
148 timer_signal_create(CLOCK_REALTIME); 152 timer_signal_create(CLOCK_REALTIME, false);
149} 153}
150 154
151ATF_TC(timer_create_mono); 155ATF_TC(timer_create_mono);
152ATF_TC_HEAD(timer_create_mono, tc) 156ATF_TC_HEAD(timer_create_mono, tc)
153{ 157{
154 158
155 atf_tc_set_md_var(tc, "descr", 159 atf_tc_set_md_var(tc, "descr",
156 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), " 160 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
157 "SIGEV_SIGNAL"); 161 "SIGEV_SIGNAL");
158} 162}
159 163
160ATF_TC_BODY(timer_create_mono, tc) 164ATF_TC_BODY(timer_create_mono, tc)
161{ 165{
162 timer_signal_create(CLOCK_MONOTONIC); 166 timer_signal_create(CLOCK_MONOTONIC, false);
 167}
 168
 169ATF_TC(timer_create_real_expire);
 170ATF_TC_HEAD(timer_create_real_expire, tc)
 171{
 172
 173 atf_tc_set_md_var(tc, "descr",
 174 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
 175 "SIGEV_SIGNAL, with expiration");
 176}
 177
 178ATF_TC_BODY(timer_create_real_expire, tc)
 179{
 180 timer_signal_create(CLOCK_REALTIME, true);
 181}
 182
 183ATF_TC(timer_create_mono_expire);
 184ATF_TC_HEAD(timer_create_mono_expire, tc)
 185{
 186
 187 atf_tc_set_md_var(tc, "descr",
 188 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
 189 "SIGEV_SIGNAL, with expiration");
 190}
 191
 192ATF_TC_BODY(timer_create_mono_expire, tc)
 193{
 194 timer_signal_create(CLOCK_MONOTONIC, true);
163} 195}
164 196
165ATF_TP_ADD_TCS(tp) 197ATF_TP_ADD_TCS(tp)
166{ 198{
167 199
168 ATF_TP_ADD_TC(tp, timer_create_err); 200 ATF_TP_ADD_TC(tp, timer_create_err);
169 ATF_TP_ADD_TC(tp, timer_create_real); 201 ATF_TP_ADD_TC(tp, timer_create_real);
170 ATF_TP_ADD_TC(tp, timer_create_mono); 202 ATF_TP_ADD_TC(tp, timer_create_mono);
 203 ATF_TP_ADD_TC(tp, timer_create_real_expire);
 204 ATF_TP_ADD_TC(tp, timer_create_mono_expire);
171 205
172 return atf_no_error(); 206 return atf_no_error();
173} 207}