1 package Test2::IPC::Driver;
5 our $VERSION = '1.302071';
8 use Carp qw/confess longmess/;
9 use Test2::Util::HashBase qw{no_fatal};
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);
23 for my $meth (qw/send cull add_hub drop_hub waiting is_viable/) {
27 confess "'$thing' did not define the required method '$meth'."
31 # Print the error and call exit. We are not using 'die' cause this is a
32 # catastrophic error that should never be caught. If we get here it
33 # means some serious shit has happened in a child process, the only way
34 # to inform the parent may be to exit false.
38 chomp(my ($msg) = @_);
39 print STDERR "IPC Fatal Error: $msg\n";
40 print STDOUT "not ok - IPC Fatal Error\n";
42 CORE::exit(255) unless $self->no_fatal;
48 $self->abort(longmess($msg));
61 Test2::IPC::Driver - Base class for Test2 IPC drivers.
65 package Test2::IPC::Driver::MyDriver;
67 use base 'Test2::IPC::Driver';
75 =item $self->abort($msg)
77 If an IPC encounters a fatal error it should use this. This will print the
78 message to STDERR with C<'IPC Fatal Error: '> prefixed to it, then it will
79 forcefully exit 255. IPC errors may occur in threads or processes other than
80 the main one, this method provides the best chance of the harness noticing the
83 =item $self->abort_trace($msg)
85 This is the same as C<< $ipc->abort($msg) >> except that it uses
86 C<Carp::longmess> to add a stack trace to the message.
88 =item $false = $self->use_shm
90 The base class always returns false for this method. You may override it if you
91 wish to use the SHM made available in L<Test2::API>/L<Test2::API::Instance>.
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 # If you are using the SHM you should notify other procs/threads that
141 # there is a pending event.
142 Test2::API::test2_ipc_set_pending($uniq_val);
149 my @events = ...; # Here is where you get the events for the hub
157 ... # Notify all listening procs and threads that the main
158 ... # process/thread is waiting for them to finish.
163 =head2 METHODS SUBCLASSES MUST IMPLEMENT
167 =item $ipc->is_viable
169 This should return true if the driver works in the current environment. This
170 should return false if it does not. This is a CLASS method.
172 =item $ipc->add_hub($hid)
174 This is used to alert the driver that a new hub is expecting events. The driver
175 should keep track of the process and thread ids, the hub should only be dropped
176 by the proc+thread that started it.
182 ... # Make it possible to contact the hub
185 =item $ipc->drop_hub($hid)
187 This is used to alert the driver that a hub is no longer accepting events. The
188 driver should keep track of the process and thread ids, the hub should only be
189 dropped by the proc+thread that started it (This is the drivers responsibility
196 ... # Nothing should try to reach the hub anymore.
199 =item $ipc->send($hid, $event);
201 =item $ipc->send($hid, $event, $global);
203 Used to send events from the current process/thread to the specified hub in its
210 ... # Send the event to the proper hub.
212 # If you are using the SHM you should notify other procs/threads that
213 # there is a pending event.
214 Test2::API::test2_ipc_set_pending($uniq_val);
217 If C<$global> is true then the driver should send the event to all hubs in all
218 processes and threads.
220 =item @events = $ipc->cull($hid)
222 Used to collect events that have been sent to the specified hub.
228 my @events = ...; # Here is where you get the events for the hub
233 =item $ipc->waiting()
235 This is called in the parent process when it is complete and waiting for all
236 child processes and threads to complete.
241 ... # Notify all listening procs and threads that the main
242 ... # process/thread is waiting for them to finish.
247 =head2 METHODS SUBCLASSES MAY IMPLEMENT OR OVERRIDE
251 =item $bool = $ipc->use_shm()
253 True if you want to make use of the L<Test2::API>/L<Test2::API::Instance> SHM.
255 =item $bites = $ipc->shm_size()
257 Use this to customize the size of the SHM space. There are no guarantees about
258 what the size will be if you do not implement this.
264 The source code repository for Test2 can be found at
265 F<http://github.com/Test-More/test-more/>.
271 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
279 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
285 Copyright 2016 Chad Granum E<lt>exodist@cpan.orgE<gt>.
287 This program is free software; you can redistribute it and/or
288 modify it under the same terms as Perl itself.
290 See F<http://dev.perl.org/licenses/>