3 * Timeout extension for test vector framework
5 * (c) 2024 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
35 #include <sys/types.h>
39 #include "tvec-timeout.h"
40 #include "tvec-types.h"
42 /*----- Main code ---------------------------------------------------------*/
44 static void reset(struct tvec_timeoutctx *tc)
46 const struct tvec_timeoutenv *te = tc->te;
48 tc->timer = te->timer; tc->t = te->t; tc->f &= ~TVTF_SETMASK;
51 /* --- @tvec_timeoutsetup@ --- *
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
60 * Use: Initialize a timeout environment context.
62 * The environment description should be a @struct
66 void tvec_timeoutsetup(struct tvec_state *tv, const struct tvec_env *env,
67 void *pctx, void *ctx)
69 struct tvec_timeoutctx *tc = ctx;
70 const struct tvec_timeoutenv *te = (struct tvec_timeoutenv *)env;
71 const struct tvec_env *subenv = te->env;
77 if (subenv && subenv->ctxsz)
78 tc->subctx = pool_alloc(tv->p_group, subenv->ctxsz);
81 if (subenv && subenv->setup) subenv->setup(tv, subenv, tc, tc->subctx);
84 /* --- @tvec_timeoutfindvar@, @setvar@ --- *
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
92 * Returns: @tvec_timeoutfindvar@ returns a pointer to the variable
93 * definition, or null; @setvar@ returns zero on success or
96 * Use: Find a definition for a special variable. The following
97 * special variables are supported.
99 * * %|@timeout|% is the duration to wait before killing the
102 * * %|@timer|% is the timer to use to measure the duration.
104 * Unrecognized variables are passed to the subordinate
105 * environment, if there is one.
108 static int setvar(struct tvec_state *tv, const char *var,
109 const union tvec_regval *rv, void *ctx)
111 struct tvec_timeoutctx *tc = ctx;
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");
123 static const struct tvec_vardef timeout_var =
124 { sizeof(struct tvec_reg), setvar,
125 { "@timeout", &tvty_duration, -1, 0 } };
127 static const struct tvec_iassoc timer_assocs[] = {
128 { "REAL", ITIMER_REAL },
129 { "VIRTUAL", ITIMER_VIRTUAL },
130 { "PROF", ITIMER_PROF },
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 } } };
139 const struct tvec_vardef *tvec_timeoutfindvar
140 (struct tvec_state *tv, const char *var, void **ctx_out, void *ctx)
142 struct tvec_timeoutctx *tc = ctx;
143 const struct tvec_timeoutenv *te = tc->te;
144 const struct tvec_env *subenv = te->env;
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));
153 /* --- @tvec_timeoutbefore@ --- *
155 * Arguments: @struct tvec_state *tv@ = test vector state
156 * @void *ctx@ = context pointer
160 * Use: Invoke the subordinate environment's @before@ function to
161 * prepare for the test.
164 void tvec_timeoutbefore(struct tvec_state *tv, void *ctx)
166 struct tvec_timeoutctx *tc = ctx;
167 const struct tvec_timeoutenv *te = tc->te;
168 const struct tvec_env *subenv = te->env;
170 if (subenv && subenv->before) subenv->before(tv, tc->subctx);
173 /* --- @tvec_timeoutrun@ --- *
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
181 * Use: Runs a test with a timeout attached.
184 void tvec_timeoutrun(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
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;
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;
195 if (setitimer(tc->timer, &itv, 0))
196 tvec_skip(tv, "failed to set interval timer: %s", strerror(errno));
198 if (subenv && subenv->run) subenv->run(tv, fn, tc->subctx);
199 else fn(tv->in, tv->out, tc->subctx);
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));
206 if (!subenv || !subenv->run) tvec_check(tv, 0);
210 /* --- @tvec_timeoutafter@ --- *
212 * Arguments: @struct tvec_state *tv@ = test vector state
213 * @void *ctx@ = context pointer
217 * Use: Invoke the subordinate environment's @after@ function to
218 * clean up after the test.
221 void tvec_timeoutafter(struct tvec_state *tv, void *ctx)
223 struct tvec_timeoutctx *tc = ctx;
224 const struct tvec_timeoutenv *te = tc->te;
225 const struct tvec_env *subenv = te->env;
227 /* Reset variables. */
230 /* Pass the call on to the subsidiary environment. */
231 if (subenv && subenv->after) subenv->after(tv, tc->subctx);
234 /* --- @tvec_timeoutteardown@ --- *
236 * Arguments: @struct tvec_state *tv@ = test vector state
237 * @void *ctx@ = context pointer
241 * Use: Tear down the timeoutmark environment.
244 void tvec_timeoutteardown(struct tvec_state *tv, void *ctx)
246 struct tvec_timeoutctx *tc = ctx;
247 const struct tvec_timeoutenv *te = tc->te;
248 const struct tvec_env *subenv = te->env;
250 /* Just call the subsidiary environment. */
251 if (subenv && subenv->teardown) subenv->teardown(tv, tc->subctx);
254 /*----- That's all, folks -------------------------------------------------*/