chiark / gitweb /
@@@ tty commentary
[mLib] / test / tvec-timeout.c
1 /* -*-c-*-
2  *
3  * Timeout extension for test vector framework
4  *
5  * (c) 2024 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU Library General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <string.h>
33
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37
38 #include "tvec.h"
39 #include "tvec-timeout.h"
40 #include "tvec-types.h"
41
42 /*----- Main code ---------------------------------------------------------*/
43
44 static void reset(struct tvec_timeoutctx *tc)
45 {
46   const struct tvec_timeoutenv *te = tc->te;
47
48   tc->timer = te->timer; tc->t = te->t; tc->f &= ~TVTF_SETMASK;
49 }
50
51 /* --- @tvec_timeoutsetup@ --- *
52  *
53  * Arguments:   @struct tvec_state *tv@ = test vector state
54  *              @const struct tvec_env *env@ = environment description
55  *              @void *pctx@ = parent context (ignored)
56  *              @void *ctx@ = context pointer to initialize
57  *
58  * Returns:     ---
59  *
60  * Use:         Initialize a timeout environment context.
61  *
62  *              The environment description should be a @struct
63  *              tvec_timeoutenv@.
64  */
65
66 void tvec_timeoutsetup(struct tvec_state *tv, const struct tvec_env *env,
67                        void *pctx, void *ctx)
68 {
69   struct tvec_timeoutctx *tc = ctx;
70   const struct tvec_timeoutenv *te = (struct tvec_timeoutenv *)env;
71   const struct tvec_env *subenv = te->env;
72
73   tc->te = te;
74
75   reset(tc);
76
77   if (subenv && subenv->ctxsz)
78     tc->subctx = pool_alloc(tv->p_group, subenv->ctxsz);
79   else
80     tc->subctx = 0;
81   if (subenv && subenv->setup) subenv->setup(tv, subenv, tc, tc->subctx);
82 }
83
84 /* --- @tvec_timeoutfindvar@, @setvar@ --- *
85  *
86  * Arguments:   @struct tvec_state *tv@ = test vector state
87  *              @const char *var@ = variable name to set
88  *              @const union tvec_regval *rv@ = register value
89  *              @void **ctx_out@ = where to put the @setvar@ context
90  *              @void *ctx@ = context pointer
91  *
92  * Returns:     @tvec_timeoutfindvar@ returns a pointer to the variable
93  *              definition, or null; @setvar@ returns zero on success or
94  *              %$-1$% on error.
95  *
96  * Use:         Find a definition for a special variable.  The following
97  *              special variables are supported.
98  *
99  *                * %|@timeout|% is the duration to wait before killing the
100  *                  process.
101  *
102  *                * %|@timer|% is the timer to use to measure the duration.
103  *
104  *              Unrecognized variables are passed to the subordinate
105  *              environment, if there is one.
106  */
107
108 static int setvar(struct tvec_state *tv, const char *var,
109                   const union tvec_regval *rv, void *ctx)
110 {
111   struct tvec_timeoutctx *tc = ctx;
112
113   if (STRCMP(var, ==, "@timeout")) {
114     if (tc->f&TVTF_SETTMO) return (tvec_dupregerr(tv, var));
115     tc->t = rv->f; tc->f |= TVTF_SETTMO;
116   } else if (STRCMP(var, ==, "@timer")) {
117     if (tc->f&TVTF_SETTMR) return (tvec_dupregerr(tv, var));
118     tc->timer = rv->i; tc->f |= TVTF_SETTMR;
119   } else assert(!"unknown var");
120   return (0);
121 }
122
123 static const struct tvec_vardef timeout_var =
124   { sizeof(struct tvec_reg), setvar,
125     { "@timeout", &tvty_duration, -1, 0 } };
126
127 static const struct tvec_iassoc timer_assocs[] = {
128   { "REAL",     ITIMER_REAL },
129   { "VIRTUAL",  ITIMER_VIRTUAL },
130   { "PROF",     ITIMER_PROF },
131   TVEC_ENDENUM
132 };
133 static const struct tvec_ienuminfo timer_enum =
134   { "interval-timer", timer_assocs, &tvrange_int };
135 static const struct tvec_vardef timer_var =
136   { sizeof(struct tvec_reg), setvar,
137     { "@timer", &tvty_ienum, -1, 0, { &timer_enum } } };
138
139 const struct tvec_vardef *tvec_timeoutfindvar
140   (struct tvec_state *tv, const char *var, void **ctx_out, void *ctx)
141 {
142   struct tvec_timeoutctx *tc = ctx;
143   const struct tvec_timeoutenv *te = tc->te;
144   const struct tvec_env *subenv = te->env;
145
146   if (STRCMP(var, ==, "@timeout")) { *ctx_out = tc; return (&timeout_var); }
147   else if (STRCMP(var, ==, "@timer")) { *ctx_out = tc; return (&timer_var); }
148   else if (subenv && subenv->findvar)
149     return (subenv->findvar(tv, var, ctx_out, tc->subctx));
150   else return (0);
151 }
152
153 /* --- @tvec_timeoutbefore@ --- *
154  *
155  * Arguments:   @struct tvec_state *tv@ = test vector state
156  *              @void *ctx@ = context pointer
157  *
158  * Returns:     ---
159  *
160  * Use:         Invoke the subordinate environment's @before@ function to
161  *              prepare for the test.
162  */
163
164 void tvec_timeoutbefore(struct tvec_state *tv, void *ctx)
165 {
166   struct tvec_timeoutctx *tc = ctx;
167   const struct tvec_timeoutenv *te = tc->te;
168   const struct tvec_env *subenv = te->env;
169
170   if (subenv && subenv->before) subenv->before(tv, tc->subctx);
171 }
172
173 /* --- @tvec_timeoutrun@ --- *
174  *
175  * Arguments:   @struct tvec_state *tv@ = test vector state
176  *              @tvec_testfn *fn@ = test function to run
177  *              @void *ctx@ = context pointer for the test function
178  *
179  * Returns:     ---
180  *
181  * Use:         Runs a test with a timeout attached.
182  */
183
184 void tvec_timeoutrun(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
185 {
186   struct tvec_timeoutctx *tc = ctx;
187   const struct tvec_timeoutenv *te = tc->te;
188   const struct tvec_env *subenv = te->env;
189   struct itimerval itv;
190
191   itv.it_interval.tv_sec = 0; itv.it_interval.tv_usec = 0;
192   itv.it_value.tv_sec = tc->t;
193   itv.it_value.tv_usec = (tc->t - itv.it_value.tv_sec)*1e6;
194
195   if (setitimer(tc->timer, &itv, 0))
196     tvec_skip(tv, "failed to set interval timer: %s", strerror(errno));
197   else {
198     if (subenv && subenv->run) subenv->run(tv, fn, tc->subctx);
199     else fn(tv->in, tv->out, tc->subctx);
200
201     itv.it_interval.tv_sec = 0; itv.it_interval.tv_usec = 0;
202     itv.it_value.tv_sec = 0; itv.it_value.tv_usec = 0;
203     if (setitimer(tc->timer, &itv, 0))
204       tvec_error(tv, "failed to disarm interval timer: %s", strerror(errno));
205
206     if (!subenv || !subenv->run) tvec_check(tv, 0);
207   }
208 }
209
210 /* --- @tvec_timeoutafter@ --- *
211  *
212  * Arguments:   @struct tvec_state *tv@ = test vector state
213  *              @void *ctx@ = context pointer
214  *
215  * Returns:     ---
216  *
217  * Use:         Invoke the subordinate environment's @after@ function to
218  *              clean up after the test.
219  */
220
221 void tvec_timeoutafter(struct tvec_state *tv, void *ctx)
222 {
223   struct tvec_timeoutctx *tc = ctx;
224   const struct tvec_timeoutenv *te = tc->te;
225   const struct tvec_env *subenv = te->env;
226
227   /* Reset variables. */
228   reset(tc);
229
230   /* Pass the call on to the subsidiary environment. */
231   if (subenv && subenv->after) subenv->after(tv, tc->subctx);
232 }
233
234 /* --- @tvec_timeoutteardown@ --- *
235  *
236  * Arguments:   @struct tvec_state *tv@ = test vector state
237  *              @void *ctx@ = context pointer
238  *
239  * Returns:     ---
240  *
241  * Use:         Tear down the timeoutmark environment.
242  */
243
244 void tvec_timeoutteardown(struct tvec_state *tv, void *ctx)
245 {
246   struct tvec_timeoutctx *tc = ctx;
247   const struct tvec_timeoutenv *te = tc->te;
248   const struct tvec_env *subenv = te->env;
249
250   /* Just call the subsidiary environment. */
251   if (subenv && subenv->teardown) subenv->teardown(tv, tc->subctx);
252 }
253
254 /*----- That's all, folks -------------------------------------------------*/