1Test2::API::Instance(3)User Contributed Perl DocumentatioTnest2::API::Instance(3)
2
3
4
6 Test2::API::Instance - Object used by Test2::API under the hood
7
9 This object encapsulates the global shared state tracked by Test2. A
10 single global instance of this package is stored (and obscured) by the
11 Test2::API package.
12
13 There is no reason to directly use this package. This package is
14 documented for completeness. This package can change, or go away
15 completely at any time. Directly using, or monkeypatching this package
16 is not supported in any way shape or form.
17
19 use Test2::API::Instance;
20
21 my $obj = Test2::API::Instance->new;
22
23 $pid = $obj->pid
24 PID of this instance.
25
26 $obj->tid
27 Thread ID of this instance.
28
29 $obj->reset()
30 Reset the object to defaults.
31
32 $obj->load()
33 Set the internal state to loaded, and run and stored post-load
34 callbacks.
35
36 $bool = $obj->loaded
37 Check if the state is set to loaded.
38
39 $arrayref = $obj->post_load_callbacks
40 Get the post-load callbacks.
41
42 $obj->add_post_load_callback(sub { ... })
43 Add a post-load callback. If "load()" has already been called then
44 the callback will be immediately executed. If "load()" has not been
45 called then the callback will be stored and executed later when
46 "load()" is called.
47
48 $hashref = $obj->contexts()
49 Get a hashref of all active contexts keyed by hub id.
50
51 $arrayref = $obj->context_acquire_callbacks
52 Get all context acquire callbacks.
53
54 $arrayref = $obj->context_init_callbacks
55 Get all context init callbacks.
56
57 $arrayref = $obj->context_release_callbacks
58 Get all context release callbacks.
59
60 $arrayref = $obj->pre_subtest_callbacks
61 Get all pre-subtest callbacks.
62
63 $obj->add_context_init_callback(sub { ... })
64 Add a context init callback. Subs are called every time a context
65 is created. Subs get the newly created context as their only
66 argument.
67
68 $obj->add_context_release_callback(sub { ... })
69 Add a context release callback. Subs are called every time a
70 context is released. Subs get the released context as their only
71 argument. These callbacks should not call release on the context.
72
73 $obj->add_pre_subtest_callback(sub { ... })
74 Add a pre-subtest callback. Subs are called every time a subtest is
75 going to be run. Subs get the subtest name, coderef, and any
76 arguments.
77
78 $obj->set_exit()
79 This is intended to be called in an "END { ... }" block. This will
80 look at test state and set $?. This will also call any end
81 callbacks, and wait on child processes/threads.
82
83 $obj->ipc_enable_shm()
84 Turn on SHM for IPC (if possible)
85
86 $shm_id = $obj->ipc_shm_id()
87 If SHM is enabled for IPC this will be the shm_id for it.
88
89 $shm_size = $obj->ipc_shm_size()
90 If SHM is enabled for IPC this will be the size of it.
91
92 $shm_last_val = $obj->ipc_shm_last()
93 If SHM is enabled for IPC this will return the last SHM value seen.
94
95 $obj->set_ipc_pending($val)
96 use the IPC SHM to tell other processes and threads there is a
97 pending event. $val should be a unique value no other
98 thread/process will generate.
99
100 Note: This will also make the current process see a pending event.
101 It does not set "ipc_shm_last()", this is important because doing
102 so could hide a previous change.
103
104 $pending = $obj->get_ipc_pending()
105 This returns -1 if SHM is not enabled for IPC.
106
107 This returns 0 if the SHM value matches the last known value, which
108 means there are no pending events.
109
110 This returns 1 if the SHM value has changed, which means there are
111 probably pending events.
112
113 When 1 is returned this will set "$obj->ipc_shm_last()".
114
115 $timeout = $obj->ipc_timeout;
116 $obj->set_ipc_timeout($timeout);
117 How long to wait for child processes and threads before aborting.
118
119 $drivers = $obj->ipc_drivers
120 Get the list of IPC drivers.
121
122 $obj->add_ipc_driver($DRIVER_CLASS)
123 Add an IPC driver to the list. The most recently added IPC driver
124 will become the global one during initialization. If a driver is
125 added after initialization has occurred a warning will be
126 generated:
127
128 "IPC driver $driver loaded too late to be used as the global ipc driver"
129
130 $bool = $obj->ipc_polling
131 Check if polling is enabled.
132
133 $obj->enable_ipc_polling
134 Turn on polling. This will cull events from other processes and
135 threads every time a context is created.
136
137 $obj->disable_ipc_polling
138 Turn off IPC polling.
139
140 $bool = $obj->no_wait
141 $bool = $obj->set_no_wait($bool)
142 Get/Set no_wait. This option is used to turn off process/thread
143 waiting at exit.
144
145 $arrayref = $obj->exit_callbacks
146 Get the exit callbacks.
147
148 $obj->add_exit_callback(sub { ... })
149 Add an exit callback. This callback will be called by "set_exit()".
150
151 $bool = $obj->finalized
152 Check if the object is finalized. Finalization happens when either
153 "ipc()", "stack()", or "format()" are called on the object. Once
154 finalization happens these fields are considered unchangeable (not
155 enforced here, enforced by Test2).
156
157 $ipc = $obj->ipc
158 Get the one true IPC instance.
159
160 $obj->ipc_disable
161 Turn IPC off
162
163 $bool = $obj->ipc_disabled
164 Check if IPC is disabled
165
166 $stack = $obj->stack
167 Get the one true hub stack.
168
169 $formatter = $obj->formatter
170 Get the global formatter. By default this is the
171 'Test2::Formatter::TAP' package. This could be any package that
172 implements the "write()" method. This can also be an instantiated
173 object.
174
175 $bool = $obj->formatter_set()
176 Check if a formatter has been set.
177
178 $obj->add_formatter($class)
179 $obj->add_formatter($obj)
180 Add a formatter. The most recently added formatter will become the
181 global one during initialization. If a formatter is added after
182 initialization has occurred a warning will be generated:
183
184 "Formatter $formatter loaded too late to be used as the global formatter"
185
186 $obj->set_add_uuid_via(sub { ... })
187 $sub = $obj->add_uuid_via()
188 This allows you to provide a UUID generator. If provided UUIDs will
189 be attached to all events, hubs, and contexts. This is useful for
190 storing, tracking, and linking these objects.
191
192 The sub you provide should always return a unique identifier. Most
193 things will expect a proper UUID string, however nothing in
194 Test2::API enforces this.
195
196 The sub will receive exactly 1 argument, the type of thing being
197 tagged 'context', 'hub', or 'event'. In the future additional
198 things may be tagged, in which case new strings will be passed in.
199 These are purely informative, you can (and usually should) ignore
200 them.
201
203 The source code repository for Test2 can be found at
204 http://github.com/Test-More/test-more/.
205
207 Chad Granum <exodist@cpan.org>
208
210 Chad Granum <exodist@cpan.org>
211
213 Copyright 2018 Chad Granum <exodist@cpan.org>.
214
215 This program is free software; you can redistribute it and/or modify it
216 under the same terms as Perl itself.
217
218 See http://dev.perl.org/licenses/
219
220
221
222perl v5.28.0 2018-08-13 Test2::API::Instance(3)