Debian Codegraph @

upstream/debian-unstable/man3/Test2::Hub.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Test2::Hub 3perl"
58 .TH Test2::Hub 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Test2::Hub \- The conduit through which all events flow.
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 1
68 \& use Test2::Hub;
69 \&
70 \& my $hub = Test2::Hub\->new();
71 \& $hub\->send(...);
72 .Ve
73 .SH DESCRIPTION
74 .IX Header "DESCRIPTION"
75 The hub is the place where all events get processed and handed off to the
76 formatter. The hub also tracks test state, and provides several hooks into the
77 event pipeline.
78 .SH "COMMON TASKS"
79 .IX Header "COMMON TASKS"
80 .SS "SENDING EVENTS"
81 .IX Subsection "SENDING EVENTS"
82 .Vb 1
83 \& $hub\->send($event)
84 .Ve
85 .PP
86 The \f(CWsend()\fR method is used to issue an event to the hub. This method will
87 handle thread/fork sync, filters, listeners, TAP output, etc.
88 .SS "ALTERING OR REMOVING EVENTS"
89 .IX Subsection "ALTERING OR REMOVING EVENTS"
90 You can use either \f(CWfilter()\fR or \f(CWpre_filter()\fR, depending on your
91 needs. Both have identical syntax, so only \f(CWfilter()\fR is shown here.
92 .PP
93 .Vb 2
94 \& $hub\->filter(sub {
95 \& my ($hub, $event) = @_;
96 \&
97 \& my $action = get_action($event);
98 \&
99 \& # No action should be taken
100 \& return $event if $action eq \*(Aqnone\*(Aq;
101 \&
102 \& # You want your filter to remove the event
103 \& return undef if $action eq \*(Aqdelete\*(Aq;
104 \&
105 \& if ($action eq \*(Aqdo_it\*(Aq) {
106 \& my $new_event = copy_event($event);
107 \& ... Change your copy of the event ...
108 \& return $new_event;
109 \& }
110 \&
111 \& die "Should not happen";
112 \& });
113 .Ve
114 .PP
115 By default, filters are not inherited by child hubs. That means if you start a
116 subtest, the subtest will not inherit the filter. You can change this behavior
117 with the \f(CW\*(C`inherit\*(C'\fR parameter:
118 .PP
119 .Vb 1
120 \& $hub\->filter(sub { ... }, inherit => 1);
121 .Ve
122 .SS "LISTENING FOR EVENTS"
123 .IX Subsection "LISTENING FOR EVENTS"
124 .Vb 2
125 \& $hub\->listen(sub {
126 \& my ($hub, $event, $number) = @_;
127 \&
128 \& ... do whatever you want with the event ...
129 \&
130 \& # return is ignored
131 \& });
132 .Ve
133 .PP
134 By default listeners are not inherited by child hubs. That means if you start a
135 subtest, the subtest will not inherit the listener. You can change this behavior
136 with the \f(CW\*(C`inherit\*(C'\fR parameter:
137 .PP
138 .Vb 1
139 \& $hub\->listen(sub { ... }, inherit => 1);
140 .Ve
141 .SS "POST-TEST BEHAVIORS"
142 .IX Subsection "POST-TEST BEHAVIORS"
143 .Vb 2
144 \& $hub\->follow_up(sub {
145 \& my ($trace, $hub) = @_;
146 \&
147 \& ... do whatever you need to ...
148 \&
149 \& # Return is ignored
150 \& });
151 .Ve
152 .PP
153 follow_up subs are called only once, either when done_testing is called, or in
154 an END block.
155 .SS "SETTING THE FORMATTER"
156 .IX Subsection "SETTING THE FORMATTER"
157 By default an instance of Test2::Formatter::TAP is created and used.
158 .PP
159 .Vb 1
160 \& my $old = $hub\->format(My::Formatter\->new);
161 .Ve
162 .PP
163 Setting the formatter will REPLACE any existing formatter. You may set the
164 formatter to undef to prevent output. The old formatter will be returned if one
165 was already set. Only one formatter is allowed at a time.
166 .SH METHODS
167 .IX Header "METHODS"
168 .ie n .IP $hub\->send($event) 4
169 .el .IP \f(CW$hub\fR\->send($event) 4
170 .IX Item "$hub->send($event)"
171 This is where all events enter the hub for processing.
172 .ie n .IP $hub\->process($event) 4
173 .el .IP \f(CW$hub\fR\->process($event) 4
174 .IX Item "$hub->process($event)"
175 This is called by send after it does any IPC handling. You can use this to
176 bypass the IPC process, but in general you should avoid using this.
177 .ie n .IP "$old = $hub\->format($formatter)" 4
178 .el .IP "\f(CW$old\fR = \f(CW$hub\fR\->format($formatter)" 4
179 .IX Item "$old = $hub->format($formatter)"
180 Replace the existing formatter instance with a new one. Formatters must be
181 objects that implement a \f(CW\*(C`$formatter\->write($event)\*(C'\fR method.
182 .ie n .IP "$sub = $hub\->listen(sub { ... }, %optional_params)" 4
183 .el .IP "\f(CW$sub\fR = \f(CW$hub\fR\->listen(sub { ... }, \f(CW%optional_params\fR)" 4
184 .IX Item "$sub = $hub->listen(sub { ... }, %optional_params)"
185 You can use this to record all events AFTER they have been sent to the
186 formatter. No changes made here will be meaningful, except possibly to other
187 listeners.
188 .Sp
189 .Vb 2
190 \& $hub\->listen(sub {
191 \& my ($hub, $event, $number) = @_;
192 \&
193 \& ... do whatever you want with the event ...
194 \&
195 \& # return is ignored
196 \& });
197 .Ve
198 .Sp
199 Normally listeners are not inherited by child hubs such as subtests. You can
200 add the \f(CW\*(C`inherit => 1\*(C'\fR parameter to allow a listener to be inherited.
201 .ie n .IP $hub\->unlisten($sub) 4
202 .el .IP \f(CW$hub\fR\->unlisten($sub) 4
203 .IX Item "$hub->unlisten($sub)"
204 You can use this to remove a listen callback. You must pass in the coderef
205 returned by the \f(CWlisten()\fR method.
206 .ie n .IP "$sub = $hub\->filter(sub { ... }, %optional_params)" 4
207 .el .IP "\f(CW$sub\fR = \f(CW$hub\fR\->filter(sub { ... }, \f(CW%optional_params\fR)" 4
208 .IX Item "$sub = $hub->filter(sub { ... }, %optional_params)"
209 .PD 0
210 .ie n .IP "$sub = $hub\->pre_filter(sub { ... }, %optional_params)" 4
211 .el .IP "\f(CW$sub\fR = \f(CW$hub\fR\->pre_filter(sub { ... }, \f(CW%optional_params\fR)" 4
212 .IX Item "$sub = $hub->pre_filter(sub { ... }, %optional_params)"
213 .PD
214 These can be used to add filters. Filters can modify, replace, or remove events
215 before anything else can see them.
216 .Sp
217 .Vb 3
218 \& $hub\->filter(
219 \& sub {
220 \& my ($hub, $event) = @_;
221 \&
222 \& return $event; # No Changes
223 \& return; # Remove the event
224 \&
225 \& # Or you can modify an event before returning it.
226 \& $event\->modify;
227 \& return $event;
228 \& }
229 \& );
230 .Ve
231 .Sp
232 If you are not using threads, forking, or IPC then the only difference between
233 a \f(CW\*(C`filter\*(C'\fR and a \f(CW\*(C`pre_filter\*(C'\fR is that \f(CW\*(C`pre_filter\*(C'\fR subs run first. When you
234 are using threads, forking, or IPC, pre_filters happen to events before they
235 are sent to their destination proc/thread, ordinary filters happen only in the
236 destination hub/thread.
237 .Sp
238 You cannot add a regular filter to a hub if the hub was created in another
239 process or thread. You can always add a pre_filter.
240 .ie n .IP $hub\->unfilter($sub) 4
241 .el .IP \f(CW$hub\fR\->unfilter($sub) 4
242 .IX Item "$hub->unfilter($sub)"
243 .PD 0
244 .ie n .IP $hub\->pre_unfilter($sub) 4
245 .el .IP \f(CW$hub\fR\->pre_unfilter($sub) 4
246 .IX Item "$hub->pre_unfilter($sub)"
247 .PD
248 These can be used to remove filters and pre_filters. The \f(CW$sub\fR argument is
249 the reference returned by \f(CWfilter()\fR or \f(CWpre_filter()\fR.
250 .ie n .IP "$hub\->follow_op(sub { ... })" 4
251 .el .IP "\f(CW$hub\fR\->follow_op(sub { ... })" 4
252 .IX Item "$hub->follow_op(sub { ... })"
253 Use this to add behaviors that are called just before the hub is finalized. The
254 only argument to your codeblock will be a Test2::EventFacet::Trace instance.
255 .Sp
256 .Vb 2
257 \& $hub\->follow_up(sub {
258 \& my ($trace, $hub) = @_;
259 \&
260 \& ... do whatever you need to ...
261 \&
262 \& # Return is ignored
263 \& });
264 .Ve
265 .Sp
266 follow_up subs are called only once, ether when done_testing is called, or in
267 an END block.
268 .ie n .IP "$sub = $hub\->add_context_acquire(sub { ... });" 4
269 .el .IP "\f(CW$sub\fR = \f(CW$hub\fR\->add_context_acquire(sub { ... });" 4
270 .IX Item "$sub = $hub->add_context_acquire(sub { ... });"
271 Add a callback that will be called every time someone tries to acquire a
272 context. It gets a single argument, a reference of the hash of parameters
273 being used the construct the context. This is your chance to change the
274 parameters by directly altering the hash.
275 .Sp
276 .Vb 4
277 \& test2_add_callback_context_acquire(sub {
278 \& my $params = shift;
279 \& $params\->{level}++;
280 \& });
281 .Ve
282 .Sp
283 This is a very scary API function. Please do not use this unless you need to.
284 This is here for Test::Builder and backwards compatibility. This has you
285 directly manipulate the hash instead of returning a new one for performance
286 reasons.
287 .Sp
288 \&\fBNote\fR Using this hook could have a huge performance impact.
289 .Sp
290 The coderef you provide is returned and can be used to remove the hook later.
291 .ie n .IP $hub\->remove_context_acquire($sub); 4
292 .el .IP \f(CW$hub\fR\->remove_context_acquire($sub); 4
293 .IX Item "$hub->remove_context_acquire($sub);"
294 This can be used to remove a context acquire hook.
295 .ie n .IP "$sub = $hub\->add_context_init(sub { ... });" 4
296 .el .IP "\f(CW$sub\fR = \f(CW$hub\fR\->add_context_init(sub { ... });" 4
297 .IX Item "$sub = $hub->add_context_init(sub { ... });"
298 This allows you to add callbacks that will trigger every time a new context is
299 created for the hub. The only argument to the sub will be the
300 Test2::API::Context instance that was created.
301 .Sp
302 \&\fBNote\fR Using this hook could have a huge performance impact.
303 .Sp
304 The coderef you provide is returned and can be used to remove the hook later.
305 .ie n .IP $hub\->remove_context_init($sub); 4
306 .el .IP \f(CW$hub\fR\->remove_context_init($sub); 4
307 .IX Item "$hub->remove_context_init($sub);"
308 This can be used to remove a context init hook.
309 .ie n .IP "$sub = $hub\->add_context_release(sub { ... });" 4
310 .el .IP "\f(CW$sub\fR = \f(CW$hub\fR\->add_context_release(sub { ... });" 4
311 .IX Item "$sub = $hub->add_context_release(sub { ... });"
312 This allows you to add callbacks that will trigger every time a context for
313 this hub is released. The only argument to the sub will be the
314 Test2::API::Context instance that was released. These will run in reverse
315 order.
316 .Sp
317 \&\fBNote\fR Using this hook could have a huge performance impact.
318 .Sp
319 The coderef you provide is returned and can be used to remove the hook later.
320 .ie n .IP $hub\->remove_context_release($sub); 4
321 .el .IP \f(CW$hub\fR\->remove_context_release($sub); 4
322 .IX Item "$hub->remove_context_release($sub);"
323 This can be used to remove a context release hook.
324 .ie n .IP $hub\->\fBcull()\fR 4
325 .el .IP \f(CW$hub\fR\->\fBcull()\fR 4
326 .IX Item "$hub->cull()"
327 Cull any IPC events (and process them).
328 .ie n .IP "$pid = $hub\->\fBpid()\fR" 4
329 .el .IP "\f(CW$pid\fR = \f(CW$hub\fR\->\fBpid()\fR" 4
330 .IX Item "$pid = $hub->pid()"
331 Get the process id under which the hub was created.
332 .ie n .IP "$tid = $hub\->\fBtid()\fR" 4
333 .el .IP "\f(CW$tid\fR = \f(CW$hub\fR\->\fBtid()\fR" 4
334 .IX Item "$tid = $hub->tid()"
335 Get the thread id under which the hub was created.
336 .ie n .IP "$hud = $hub\->\fBhid()\fR" 4
337 .el .IP "\f(CW$hud\fR = \f(CW$hub\fR\->\fBhid()\fR" 4
338 .IX Item "$hud = $hub->hid()"
339 Get the identifier string of the hub.
340 .ie n .IP "$uuid = $hub\->\fBuuid()\fR" 4
341 .el .IP "\f(CW$uuid\fR = \f(CW$hub\fR\->\fBuuid()\fR" 4
342 .IX Item "$uuid = $hub->uuid()"
343 If UUID tagging is enabled (see Test2::API) then the hub will have a UUID.
344 .ie n .IP "$ipc = $hub\->\fBipc()\fR" 4
345 .el .IP "\f(CW$ipc\fR = \f(CW$hub\fR\->\fBipc()\fR" 4
346 .IX Item "$ipc = $hub->ipc()"
347 Get the IPC object used by the hub.
348 .ie n .IP $hub\->set_no_ending($bool) 4
349 .el .IP \f(CW$hub\fR\->set_no_ending($bool) 4
350 .IX Item "$hub->set_no_ending($bool)"
351 .PD 0
352 .ie n .IP "$bool = $hub\->no_ending" 4
353 .el .IP "\f(CW$bool\fR = \f(CW$hub\fR\->no_ending" 4
354 .IX Item "$bool = $hub->no_ending"
355 .PD
356 This can be used to disable auto-ending behavior for a hub. The auto-ending
357 behavior is triggered by an end block and is used to cull IPC events, and
358 output the final plan if the plan was 'NO PLAN'.
359 .ie n .IP "$bool = $hub\->active" 4
360 .el .IP "\f(CW$bool\fR = \f(CW$hub\fR\->active" 4
361 .IX Item "$bool = $hub->active"
362 .PD 0
363 .ie n .IP $hub\->set_active($bool) 4
364 .el .IP \f(CW$hub\fR\->set_active($bool) 4
365 .IX Item "$hub->set_active($bool)"
366 .PD
367 These are used to get/set the 'active' attribute. When true this attribute will
368 force \f(CW\*(C`hub\->finalize()\*(C'\fR to take action even if there is no plan, and no
369 tests have been run. This flag is useful for plugins that add follow-up
370 behaviors that need to run even if no events are seen.
371 .SS "STATE METHODS"
372 .IX Subsection "STATE METHODS"
373 .ie n .IP $hub\->\fBreset_state()\fR 4
374 .el .IP \f(CW$hub\fR\->\fBreset_state()\fR 4
375 .IX Item "$hub->reset_state()"
376 Reset all state to the start. This sets the test count to 0, clears the plan,
377 removes the failures, etc.
378 .ie n .IP "$num = $hub\->count" 4
379 .el .IP "\f(CW$num\fR = \f(CW$hub\fR\->count" 4
380 .IX Item "$num = $hub->count"
381 Get the number of tests that have been run.
382 .ie n .IP "$num = $hub\->failed" 4
383 .el .IP "\f(CW$num\fR = \f(CW$hub\fR\->failed" 4
384 .IX Item "$num = $hub->failed"
385 Get the number of failures (Not all failures come from a test fail, so this
386 number can be larger than the count).
387 .ie n .IP "$bool = $hub\->ended" 4
388 .el .IP "\f(CW$bool\fR = \f(CW$hub\fR\->ended" 4
389 .IX Item "$bool = $hub->ended"
390 True if the testing has ended. This MAY return the stack frame of the tool that
391 ended the test, but that is not guaranteed.
392 .ie n .IP "$bool = $hub\->is_passing" 4
393 .el .IP "\f(CW$bool\fR = \f(CW$hub\fR\->is_passing" 4
394 .IX Item "$bool = $hub->is_passing"
395 .PD 0
396 .ie n .IP $hub\->is_passing($bool) 4
397 .el .IP \f(CW$hub\fR\->is_passing($bool) 4
398 .IX Item "$hub->is_passing($bool)"
399 .PD
400 Check if the overall test run is a failure. Can also be used to set the
401 pass/fail status.
402 .ie n .IP $hub\->plan($plan) 4
403 .el .IP \f(CW$hub\fR\->plan($plan) 4
404 .IX Item "$hub->plan($plan)"
405 .PD 0
406 .ie n .IP "$plan = $hub\->plan" 4
407 .el .IP "\f(CW$plan\fR = \f(CW$hub\fR\->plan" 4
408 .IX Item "$plan = $hub->plan"
409 .PD
410 Get or set the plan. The plan must be an integer larger than 0, the string
411 \&'NO PLAN', or the string 'SKIP'.
412 .ie n .IP "$bool = $hub\->check_plan" 4
413 .el .IP "\f(CW$bool\fR = \f(CW$hub\fR\->check_plan" 4
414 .IX Item "$bool = $hub->check_plan"
415 Check if the plan and counts match, but only if the tests have ended. If tests
416 have not ended this will return undef, otherwise it will be a true/false.
417 .SH "THIRD PARTY META-DATA"
418 .IX Header "THIRD PARTY META-DATA"
419 This object consumes Test2::Util::ExternalMeta which provides a consistent
420 way for you to attach meta-data to instances of this class. This is useful for
421 tools, plugins, and other extensions.
422 .SH SOURCE
423 .IX Header "SOURCE"
424 The source code repository for Test2 can be found at
425 <https://github.com/Test\-More/test\-more/>.
426 .SH MAINTAINERS
427 .IX Header "MAINTAINERS"
428 .IP "Chad Granum <[email protected]>" 4
429 .IX Item "Chad Granum <[email protected]>"
430 .SH AUTHORS
431 .IX Header "AUTHORS"
432 .PD 0
433 .IP "Chad Granum <[email protected]>" 4
434 .IX Item "Chad Granum <[email protected]>"
435 .PD
436 .SH COPYRIGHT
437 .IX Header "COPYRIGHT"
438 Copyright 2020 Chad Granum <[email protected]>.
439 .PP
440 This program is free software; you can redistribute it and/or
441 modify it under the same terms as Perl itself.
442 .PP
443 See <https://dev.perl.org/licenses/>