include/punk/punk_obs_selftest.h

filter: Statementstmt 94.8% 55 / 58 Branchbran 61.9% 25 / 42 (5 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.0% 80 / 100 (5 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_dbi.h ↑ Summary include/punk/punk_cget.h »
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 /*
53 # uncoverable branch false
54 # uncoverable condition right count:1 class:ignore_covered_err
55 # uncoverable condition right count:2 class:ignore_covered_err
56 # uncoverable condition right count:3 class:ignore_covered_err */
57 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
58 48 SV **p = hv_fetchs((HV *)SvRV(en), "PATH_INFO", 0);
59 /*
60 # uncoverable condition right count:1 class:ignore_covered_err
61 # uncoverable condition right count:2 class:ignore_covered_err
62 # uncoverable condition right count:3 class:ignore_covered_err */
63 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
64 : newSV(0));
65 }
66 0 else (void)hv_stores(e, "path", newSV(0)); /* # uncoverable statement */
67  
68 48 return e;
69 }
70  
71 /* The stash hash through the table, or NULL. */
72 48 static HV *pk_selftest_stash(pTHX_ SV *c) {
73 48 SV *st = PK_ABI.stash_of(aTHX_ c);
74 /*
75 # uncoverable branch true
76 # uncoverable condition left count:1 class:ignore_covered_err
77 # uncoverable condition left count:2 class:ignore_covered_err
78 # uncoverable condition left count:3 class:ignore_covered_err */
79 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
80 48 return (HV *)SvRV(st);
81 }
82  
83 24 static void pk_selftest_req(pTHX_ SV *c, void *ud) {
84 24 HV *e = pk_selftest_event(aTHX_ c, "request");
85 24 HV *st = pk_selftest_stash(aTHX_ c);
86 PERL_UNUSED_ARG(ud);
87 /* Leave a mark, and record that we could. The response side reads it back
88 * out: if the dispatcher ever built a second context, the mark would be
89 * missing there and the test says so. */
90 24 (void)hv_stores(e, "has_stash", newSViv(st ? 1 : 0));
91 /*
92 # uncoverable branch false
93 # uncoverable condition right count:1 class:ignore_covered_err */
94 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
95 24 (void)hv_stores(e, "mark", newSViv(PK_SELFTEST_SEQ));
96 24 av_push(pk_selftest_av(aTHX), newRV_noinc((SV *)e));
97 24 }
98  
99 24 static void pk_selftest_res(pTHX_ SV *c, SV *response, void *ud) {
100 24 HV *e = pk_selftest_event(aTHX_ c, "response");
101 24 HV *st = pk_selftest_stash(aTHX_ c);
102 24 ▶ SV **m = st ? hv_fetchs(st, "pk.selftest", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **m = st ? hv_fetchs(st, "pk.selftest", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 24 0 24
103 PERL_UNUSED_ARG(ud);
104 24 (void)hv_stores(e, "has_stash", newSViv(st ? 1 : 0));
105 /*
106 # uncoverable condition right count:1 class:ignore_covered_err
107 # uncoverable condition right count:2 class:ignore_covered_err
108 # uncoverable condition right count:3 class:ignore_covered_err */
109 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
110 24 (void)hv_stores(e, "status", newSViv(PK_ABI.status_of(aTHX_ response)));
111 24 av_push(pk_selftest_av(aTHX), newRV_noinc((SV *)e));
112 24 }
113  
114 /* ---- v2 on_query, for t/0011-pk-abi.t ------------------------------------- */
115 static IV PK_SELFTEST_Q_STARTS = 0;
116 static IV PK_SELFTEST_Q_DONES = 0;
117 static IV PK_SELFTEST_Q_OK = 0;
118 static IV PK_SELFTEST_Q_BIND = -1;
119 static char PK_SELFTEST_Q_SQL[512];
120  
121 17 static void *pk_selftest_query(pTHX_ const char *sql, STRLEN sql_len,
122 int nbind, void *ud) {
123 PERL_UNUSED_ARG(ud);
124 PERL_UNUSED_CONTEXT;
125 17 PK_SELFTEST_Q_STARTS++;
126 17 PK_SELFTEST_Q_BIND = nbind;
127 /*
128 # uncoverable branch true
129 # uncoverable condition left count:1 class:ignore_covered_err */
130 17 ▶ if (sql_len >= sizeof(PK_SELFTEST_Q_SQL))
Branch: 1 branch
BranchTrueFalseTotal
if (sql_len >= sizeof(PK_SELFTEST_Q_SQL)) 0 17 17
131 0 sql_len = sizeof(PK_SELFTEST_Q_SQL) - 1; /* # uncoverable statement */
132 17 memcpy(PK_SELFTEST_Q_SQL, sql, sql_len);
133 17 PK_SELFTEST_Q_SQL[sql_len] = '\0';
134 17 return INT2PTR(void *, PK_SELFTEST_Q_STARTS);
135 }
136  
137 17 static void pk_selftest_query_done(pTHX_ void *token, int ok, void *ud) {
138 PERL_UNUSED_ARG(ud);
139 PERL_UNUSED_CONTEXT;
140 17 PK_SELFTEST_Q_DONES++;
141 17 ▶ if (ok) PK_SELFTEST_Q_OK++;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) PK_SELFTEST_Q_OK++; 16 1 17
142 /*
143 # uncoverable branch true
144 # uncoverable condition left count:1 class:ignore_covered_err
145 # uncoverable condition left count:2 class:ignore_covered_err */
146 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
147 0 PK_SELFTEST_Q_OK = -1; /* correlation broke */ /* # uncoverable statement */
148 17 }
149  
150 #endif /* PUNK_OBS_SELFTEST_H */
« include/punk/punk_dbi.h ↑ Summary include/punk/punk_cget.h »