include/punk/punk_obs_selftest.h

filter: Statementstmt 94.8% 55 / 58 Branchbran 59.5% 25 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 80 / 100 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_dispatch.h ↑ Summary xs/stencil.xs »
1 /* punk_obs_selftest.h - a C consumer of Punk's own C ABI, for the tests.
2 *
3 * The point of testing an ABI through itself is that a static function called
4 * directly proves nothing about the TABLE: a mis-ordered initialiser, a
5 * signature that drifted from the header, a version that was not bumped, are
6 * all invisible until something goes through the function pointers. So this
7 * registers with the table and reads the request back through it, exactly as
8 * Punk::OpenTelemetry will, and t/0011-pk-abi.t reads what it recorded.
9 *
10 * Included by Punk.xs after pk_abi_impl.h (it needs the PK_ABI table).
11 * Private; nothing here is part of any interface.
12 */
13  
14 #ifndef PUNK_OBS_SELFTEST_H
15 #define PUNK_OBS_SELFTEST_H
16  
17 static AV *PK_SELFTEST_EVENTS = NULL;
18 static int PK_SELFTEST_ON = 0;
19 static IV PK_SELFTEST_SEQ = 0;
20  
21 87 static AV *pk_selftest_av(pTHX) {
22 87 ▶ if (!PK_SELFTEST_EVENTS) PK_SELFTEST_EVENTS = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PK_SELFTEST_EVENTS) PK_SELFTEST_EVENTS = newAV(); 1 86 87
23 87 return PK_SELFTEST_EVENTS;
24 }
25  
26 /* One recorded event. Everything is read back through the table's own
27 * pointers, including the accessors, so a broken entry shows up as a wrong
28 * value in the test rather than as a compile error nobody sees. */
29 48 static HV *pk_selftest_event(pTHX_ SV *c, const char *kind) {
30 48 const pk_abi *A = &PK_ABI;
31 48 HV *e = newHV();
32 48 SV *rp = A->route_pattern_of(aTHX_ c);
33 48 SV *op = A->operation_of(aTHX_ c);
34 48 SV *en = A->env_of(aTHX_ c);
35 48 SV *ap = A->app_of(aTHX_ c);
36 48 SV *mt = A->match_of(aTHX_ c);
37  
38 48 (void)hv_stores(e, "kind", newSVpv(kind, 0));
39 /* v5. Not "is it set" but "is it THIS ONE": an observer that is handed a
40 * context and a current_of that names a different one is the failure the
41 * member exists to make impossible, and only an identity check sees it. */
42 48 (void)hv_stores(e, "current_is_c",
43 newSViv(A->current_of(aTHX) == c ? 1 : 0));
44 48 ▶ (void)hv_stores(e, "route", rp ? newSVsv(rp) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(e, "route", rp ? newSVsv(rp) : newSV(0)); 16 32 48
45 48 ▶ (void)hv_stores(e, "operation", op ? newSVsv(op) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(e, "operation", op ? newSVsv(op) : newSV(0)); 2 46 48
46 48 (void)hv_stores(e, "has_env", newSViv(en ? 1 : 0));
47 48 (void)hv_stores(e, "has_app", newSViv(ap ? 1 : 0));
48 48 (void)hv_stores(e, "has_match", newSViv(mt ? 1 : 0));
49  
50 /* the request path, straight off the env, so the test can tell which
51 * request an event belongs to without depending on ordering */
52 96 ▶ if (en && SvROK(en) && SvTYPE(SvRV(en)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (en && SvROK(en) && SvTYPE(SvRV(en)) == SVt_PVHV) { 48 0 48
if (en && SvROK(en) && SvTYPE(SvRV(en)) == SVt_PVHV) { 48 0 48
if (en && SvROK(en) && SvTYPE(SvRV(en)) == SVt_PVHV) { 48 0 48
53 48 SV **p = hv_fetchs((HV *)SvRV(en), "PATH_INFO", 0);
54 48 ▶ (void)hv_stores(e, "path", (p && *p && SvOK(*p)) ? newSVsv(*p)
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(e, "path", (p && *p && SvOK(*p)) ? newSVsv(*p) 48 0 48
(void)hv_stores(e, "path", (p && *p && SvOK(*p)) ? newSVsv(*p) 48 0 48
(void)hv_stores(e, "path", (p && *p && SvOK(*p)) ? newSVsv(*p) 48 0 48
55 : newSV(0));
56 }
57 0 else (void)hv_stores(e, "path", newSV(0));
58  
59 48 return e;
60 }
61  
62 /* The stash hash through the table, or NULL. */
63 48 static HV *pk_selftest_stash(pTHX_ SV *c) {
64 48 SV *st = PK_ABI.stash_of(aTHX_ c);
65 48 ▶ if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 48 0 48
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 48 0 48
if (!(st && SvROK(st) && SvTYPE(SvRV(st)) == SVt_PVHV)) return NULL; 0 48 48
66 48 return (HV *)SvRV(st);
67 }
68  
69 24 static void pk_selftest_req(pTHX_ SV *c, void *ud) {
70 24 HV *e = pk_selftest_event(aTHX_ c, "request");
71 24 HV *st = pk_selftest_stash(aTHX_ c);
72 PERL_UNUSED_ARG(ud);
73 /* Leave a mark, and record that we could. The response side reads it back
74 * out: if the dispatcher ever built a second context, the mark would be
75 * missing there and the test says so. */
76 24 (void)hv_stores(e, "has_stash", newSViv(st ? 1 : 0));
77 24 ▶ if (st) (void)hv_stores(st, "pk.selftest", newSViv(++PK_SELFTEST_SEQ));
Branch: 1 branch
BranchTrueFalseTotal
if (st) (void)hv_stores(st, "pk.selftest", newSViv(++PK_SELFTEST_SEQ)); 24 0 24
78 24 (void)hv_stores(e, "mark", newSViv(PK_SELFTEST_SEQ));
79 24 av_push(pk_selftest_av(aTHX), newRV_noinc((SV *)e));
80 24 }
81  
82 24 static void pk_selftest_res(pTHX_ SV *c, SV *response, void *ud) {
83 24 HV *e = pk_selftest_event(aTHX_ c, "response");
84 24 HV *st = pk_selftest_stash(aTHX_ c);
85 24 ▶ SV **m = st ? hv_fetchs(st, "pk.selftest", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **m = st ? hv_fetchs(st, "pk.selftest", 0) : NULL; 24 0 24
86 PERL_UNUSED_ARG(ud);
87 24 (void)hv_stores(e, "has_stash", newSViv(st ? 1 : 0));
88 24 ▶ (void)hv_stores(e, "mark", (m && *m && SvOK(*m)) ? newSVsv(*m) : newSViv(0));
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(e, "mark", (m && *m && SvOK(*m)) ? newSVsv(*m) : newSViv(0)); 24 0 24
(void)hv_stores(e, "mark", (m && *m && SvOK(*m)) ? newSVsv(*m) : newSViv(0)); 24 0 24
(void)hv_stores(e, "mark", (m && *m && SvOK(*m)) ? newSVsv(*m) : newSViv(0)); 24 0 24
89 24 (void)hv_stores(e, "status", newSViv(PK_ABI.status_of(aTHX_ response)));
90 24 av_push(pk_selftest_av(aTHX), newRV_noinc((SV *)e));
91 24 }
92  
93 /* ---- v2 on_query, for t/0011-pk-abi.t ------------------------------------- */
94 static IV PK_SELFTEST_Q_STARTS = 0;
95 static IV PK_SELFTEST_Q_DONES = 0;
96 static IV PK_SELFTEST_Q_OK = 0;
97 static IV PK_SELFTEST_Q_BIND = -1;
98 static char PK_SELFTEST_Q_SQL[512];
99  
100 17 static void *pk_selftest_query(pTHX_ const char *sql, STRLEN sql_len,
101 int nbind, void *ud) {
102 PERL_UNUSED_ARG(ud);
103 PERL_UNUSED_CONTEXT;
104 17 PK_SELFTEST_Q_STARTS++;
105 17 PK_SELFTEST_Q_BIND = nbind;
106 17 ▶ if (sql_len >= sizeof(PK_SELFTEST_Q_SQL))
Branch: 1 branch
BranchTrueFalseTotal
if (sql_len >= sizeof(PK_SELFTEST_Q_SQL)) 0 17 17
107 0 sql_len = sizeof(PK_SELFTEST_Q_SQL) - 1;
108 17 memcpy(PK_SELFTEST_Q_SQL, sql, sql_len);
109 17 PK_SELFTEST_Q_SQL[sql_len] = '\0';
110 17 return INT2PTR(void *, PK_SELFTEST_Q_STARTS);
111 }
112  
113 17 static void pk_selftest_query_done(pTHX_ void *token, int ok, void *ud) {
114 PERL_UNUSED_ARG(ud);
115 PERL_UNUSED_CONTEXT;
116 17 PK_SELFTEST_Q_DONES++;
117 17 ▶ if (ok) PK_SELFTEST_Q_OK++;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) PK_SELFTEST_Q_OK++; 16 1 17
118 17 ▶ if (PTR2IV(token) < 1 || PTR2IV(token) > PK_SELFTEST_Q_STARTS)
Branch: 2 branches
BranchTrueFalseTotal
if (PTR2IV(token) < 1 || PTR2IV(token) > PK_SELFTEST_Q_STARTS) 17 0 17
if (PTR2IV(token) < 1 || PTR2IV(token) > PK_SELFTEST_Q_STARTS) 0 17 17
119 0 PK_SELFTEST_Q_OK = -1; /* correlation broke */
120 17 }
121  
122 #endif /* PUNK_OBS_SELFTEST_H */
« include/punk/punk_dispatch.h ↑ Summary xs/stencil.xs »