1 package Test2::IPC::Driver;
5 our $VERSION = '1.302170';
9 use Test2::Util::HashBase qw{no_fatal no_bail};
11 use Test2::API qw/test2_ipc_add_driver/;
16 return if $class eq __PACKAGE__;
17 return if $ADDED{$class}++;
18 test2_ipc_add_driver($class);
22 sub set_pending { -1 }
24 for my $meth (qw/send cull add_hub drop_hub waiting is_viable/) {
28 confess "'$thing' did not define the required method '$meth'."
32 # Print the error and call exit. We are not using 'die' cause this is a
33 # catastrophic error that should never be caught. If we get here it
34 # means some serious shit has happened in a child process, the only way
35 # to inform the parent may be to exit false.
39 chomp(my ($msg) = @_);
41 $self->driver_abort($msg) if $self->can('driver_abort');
43 print STDERR "IPC Fatal Error: $msg\n";
44 print STDOUT "Bail out! IPC Fatal Error: $msg\n" unless $self->no_bail;
46 CORE::exit(255) unless $self->no_fatal;
52 # Older versions of Carp do not export longmess() function, so it needs to be called with package name
53 $self->abort(Carp::longmess($msg));
66 Test2::IPC::Driver - Base class for Test2 IPC drivers.
70 package Test2::IPC::Driver::MyDriver;
72 use base 'Test2::IPC::Driver';
80 =item $self->abort($msg)
82 If an IPC encounters a fatal error it should use this. This will print the
83 message to STDERR with C<'IPC Fatal Error: '> prefixed to it, then it will
84 forcefully exit 255. IPC errors may occur in threads or processes other than
85 the main one, this method provides the best chance of the harness noticing the
88 =item $self->abort_trace($msg)
90 This is the same as C<< $ipc->abort($msg) >> except that it uses
91 C<Carp::longmess> to add a stack trace to the message.
95 =head1 LOADING DRIVERS
97 Test2::IPC::Driver has an C<import()> method. All drivers inherit this import
98 method. This import method registers the driver.
100 In most cases you just need to load the desired IPC driver to make it work. You
101 should load this driver as early as possible. A warning will be issued if you
102 load it too late for it to be effective.
104 use Test2::IPC::Driver::MyDriver;
107 =head1 WRITING DRIVERS
109 package Test2::IPC::Driver::MyDriver;
113 use base 'Test2::IPC::Driver';
116 return 0 if $^O eq 'win32'; # Will not work on windows.
124 ... # Make it possible to contact the hub
131 ... # Nothing should try to reach the hub anymore.
136 my ($hid, $e, $global) = @_;
138 ... # Send the event to the proper hub.
140 # This may notify other procs/threads that there is a pending event.
141 Test2::API::test2_ipc_set_pending($uniq_val);
148 my @events = ...; # Here is where you get the events for the hub
156 ... # Notify all listening procs and threads that the main
157 ... # process/thread is waiting for them to finish.
162 =head2 METHODS SUBCLASSES MUST IMPLEMENT
166 =item $ipc->is_viable
168 This should return true if the driver works in the current environment. This
169 should return false if it does not. This is a CLASS method.
171 =item $ipc->add_hub($hid)
173 This is used to alert the driver that a new hub is expecting events. The driver
174 should keep track of the process and thread ids, the hub should only be dropped
175 by the proc+thread that started it.
181 ... # Make it possible to contact the hub
184 =item $ipc->drop_hub($hid)
186 This is used to alert the driver that a hub is no longer accepting events. The
187 driver should keep track of the process and thread ids, the hub should only be
188 dropped by the proc+thread that started it (This is the drivers responsibility
195 ... # Nothing should try to reach the hub anymore.
198 =item $ipc->send($hid, $event);
200 =item $ipc->send($hid, $event, $global);
202 Used to send events from the current process/thread to the specified hub in its
209 ... # Send the event to the proper hub.
211 # This may notify other procs/threads that there is a pending event.
212 Test2::API::test2_ipc_set_pending($uniq_val);
215 If C<$global> is true then the driver should send the event to all hubs in all
216 processes and threads.
218 =item @events = $ipc->cull($hid)
220 Used to collect events that have been sent to the specified hub.
226 my @events = ...; # Here is where you get the events for the hub
231 =item $ipc->waiting()
233 This is called in the parent process when it is complete and waiting for all
234 child processes and threads to complete.
239 ... # Notify all listening procs and threads that the main
240 ... # process/thread is waiting for them to finish.
245 =head2 METHODS SUBCLASSES MAY IMPLEMENT OR OVERRIDE
249 =item $ipc->driver_abort($msg)
251 This is a hook called by C<< Test2::IPC::Driver->abort() >>. This is your
252 chance to cleanup when an abort happens. You cannot prevent the abort, but you
253 can gracefully except it.
259 The source code repository for Test2 can be found at
260 F<http://github.com/Test-More/test-more/>.
266 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
274 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
280 Copyright 2019 Chad Granum E<lt>exodist@cpan.orgE<gt>.
282 This program is free software; you can redistribute it and/or
283 modify it under the same terms as Perl itself.
285 See F<http://dev.perl.org/licenses/>