Test2::IPC::Driver - Test2 IPC 驅動程式的基礎類別。
package Test2::IPC::Driver::MyDriver;
use base 'Test2::IPC::Driver';
...
如果 IPC 遇到致命錯誤,它應該使用這個方法。這會將訊息列印到 STDERR,並在前面加上 'IPC Fatal Error: '
,然後強制退出 255。IPC 錯誤可能發生在主執行緒或程序以外,這個方法提供讓測試架構注意到錯誤的最佳機會。
這與 $ipc->abort($msg)
相同,但它使用 Carp::longmess
將堆疊追蹤新增至訊息。
Test2::IPC::Driver 有 import()
方法。所有驅動程式都繼承此匯入方法。此匯入方法會註冊驅動程式。
在大部分情況下,您只需要載入所需的 IPC 驅動程式即可讓它運作。您應盡早載入此驅動程式。如果您載入得太晚而無法生效,系統會發出警告。
use Test2::IPC::Driver::MyDriver;
...
package Test2::IPC::Driver::MyDriver;
use strict;
use warnings;
use base 'Test2::IPC::Driver';
sub is_viable {
return 0 if $^O eq 'win32'; # Will not work on windows.
return 1;
}
sub add_hub {
my $self = shift;
my ($hid) = @_;
... # Make it possible to contact the hub
}
sub drop_hub {
my $self = shift;
my ($hid) = @_;
... # Nothing should try to reach the hub anymore.
}
sub send {
my $self = shift;
my ($hid, $e, $global) = @_;
... # Send the event to the proper hub.
# This may notify other procs/threads that there is a pending event.
Test2::API::test2_ipc_set_pending($uniq_val);
}
sub cull {
my $self = shift;
my ($hid) = @_;
my @events = ...; # Here is where you get the events for the hub
return @events;
}
sub waiting {
my $self = shift;
... # Notify all listening procs and threads that the main
... # process/thread is waiting for them to finish.
}
1;
如果驅動程式在目前的環境中運作,則應傳回 true。如果沒有,則應傳回 false。這是一個 CLASS 方法。
這用於提醒驅動程式有新的 hub 正在等候事件。驅動程式應追蹤處理程序和執行緒 ID,hub 只能由啟動它的處理程序 + 執行緒捨棄。
sub add_hub {
my $self = shift;
my ($hid) = @_;
... # Make it possible to contact the hub
}
這用於提醒驅動程式有 hub 不再接受事件。驅動程式應追蹤處理程序和執行緒 ID,hub 只能由啟動它的處理程序 + 執行緒捨棄(這是驅動程式的責任,必須強制執行)。
sub drop_hub {
my $self = shift;
my ($hid) = @_;
... # Nothing should try to reach the hub anymore.
}
用於將事件從目前的處理程序/執行緒傳送至其處理程序 + 執行緒中的指定 hub。
sub send {
my $self = shift;
my ($hid, $e) = @_;
... # Send the event to the proper hub.
# This may notify other procs/threads that there is a pending event.
Test2::API::test2_ipc_set_pending($uniq_val);
}
如果 $global
為 true,則驅動程式應將事件傳送至所有處理程序和執行緒中的所有 hub。
用於收集已傳送至指定 hub 的事件。
sub cull {
my $self = shift;
my ($hid) = @_;
my @events = ...; # Here is where you get the events for the hub
return @events;
}
當父程序完成並等待所有子程序和執行緒完成時,會呼叫此函數。
sub waiting {
my $self = shift;
... # Notify all listening procs and threads that the main
... # process/thread is waiting for them to finish.
}
這是由 Test2::IPC::Driver->abort()
呼叫的掛鉤。這是您在發生中斷時進行清理的機會。您無法防止中斷,但可以優雅地接受它。
Test2 的原始碼存放庫位於 http://github.com/Test-More/test-more/。
版權所有 2020 Chad Granum <exodist@cpan.org>。
此程式為自由軟體;您可以在與 Perl 相同的條款下重新散布或修改它。
請參閱 http://dev.perl.org/licenses/