| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2020-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Test::Future::AsyncAwait::Awaitable 0.71; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Test2::V0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 15 | test_awaitable | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | C<Test::Future::AsyncAwait::Awaitable> - conformance tests for awaitable role API | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =for highlighter language=perl | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use Test::More; | ||||||||||||||||||||||||||||
| 27 | use Test::Future::AsyncAwait::Awaitable; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | use My::Future::Subclass; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | test_awaitable "My subclass of Future", | ||||||||||||||||||||||||||||
| 32 | class => "My::Future::Subclass"; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | done_testing; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | This module provides a single test function, which runs a suite of subtests to | ||||||||||||||||||||||||||||
| 39 | check that a given class provides a useable implementation of the | ||||||||||||||||||||||||||||
| 40 | L<Future::AsyncAwait::Awaitable> role. It runs tests that simulate various | ||||||||||||||||||||||||||||
| 41 | ways in which L<Future::AsyncAwait> will try to use an instance of this class, | ||||||||||||||||||||||||||||
| 42 | to check that the implementation is valid. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 test_awaitable | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | test_awaitable( $title, %args ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Runs the API conformance tests. C<$title> is printed in the test description | ||||||||||||||||||||||||||||
| 55 | output so should be some human-friendly string. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Takes the following named arguments: | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =over 4 | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =item class => STRING | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Gives the name of the class. This is the class on which the C<AWAIT_NEW_DONE> | ||||||||||||||||||||||||||||
| 64 | and C<AWAIT_NEW_FAIL> methods will be invoked. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =item new => CODE | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Optional. Gives a callback function to invoke to construct a new pending | ||||||||||||||||||||||||||||
| 69 | instance; used by the tests to create pending instances that would be passed | ||||||||||||||||||||||||||||
| 70 | into the C<await> keyword. As this is not part of the API as such, the test | ||||||||||||||||||||||||||||
| 71 | code does not rely on being able to directly perform it via the API. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | This argument is optional; if not provided the tests will simply try to invoke | ||||||||||||||||||||||||||||
| 74 | the regular C<new> constructor on the given class name. For most | ||||||||||||||||||||||||||||
| 75 | implementations this should be sufficient. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | $f = $new->(); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =item cancel => CODE | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Optional. Gives a callback function to invoke to cancel a pending instance, if | ||||||||||||||||||||||||||||
| 82 | the implementation provides cancellation semantics. If this callback is | ||||||||||||||||||||||||||||
| 83 | provided then an extra subtest suite is run to check the API around | ||||||||||||||||||||||||||||
| 84 | cancellation. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | $cancel->( $f ); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item force => CODE | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Optional. Gives a callback function to invoke to wait for a promise to invoke | ||||||||||||||||||||||||||||
| 91 | its on-ready callbacks. Some future-like implementations will run these | ||||||||||||||||||||||||||||
| 92 | immediately when the future is marked as done or failed, and so this callback | ||||||||||||||||||||||||||||
| 93 | will not be required. Other implementations will defer these invocations, | ||||||||||||||||||||||||||||
| 94 | perhaps until the next tick of an event loop or similar. In the latter case, | ||||||||||||||||||||||||||||
| 95 | these implementations should provide a way for the test to wait for this to | ||||||||||||||||||||||||||||
| 96 | happen. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | $force->( $f ); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =back | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | my $FILE = __FILE__; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | my %FIXED_MODULE_VERSIONS = ( | |||||||||||||||||||||||||||
| 107 | 'Future::PP' => '0.50', | ||||||||||||||||||||||||||||
| 108 | 'Future::XS' => '0.09', | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub _complain_package_version | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_complain_package_version: not called CC=5 SCAR=28.6
| |||||||||||||||||||||||||||||
| 113 | 0 | my ( $pkg ) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Drill down to the most base class that isn't Future::_base | ||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | $pkg = ${"${pkg}::ISA"}[0] while @{"${pkg}::ISA"} and ${"${pkg}::ISA"}[0] ne "Future::_base"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@{"${pkg}::ISA";} and do {
"${pkg}::ISA"
}->[0] ne "Future::_base"
MC/DC0%0/2
@{"${pkg}::ISA";} and do {
"${pkg}::ISA"
}->[0] ne "Future::_base"
@{"${pkg}::ISA";}
do {
"${pkg}::ISA"
}->[0] ne "Future::_base"
Truth table0%0/3
A and B
A= @{"${pkg}::ISA";}B= do {
"${pkg}::ISA"
}->[0] ne "Future::_base"
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $pkgver = do { no strict 'refs'; ${"${pkg}::VERSION"} }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 122 | 0 | my $wantver = $FIXED_MODULE_VERSIONS{$pkg}; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if( defined $wantver && $pkgver < $wantver ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $wantver and $pkgver < $wantver
MC/DC0%0/2
defined $wantver and $pkgver < $wantver
defined $wantver
$pkgver < $wantver
Truth table0%0/3
A and B
A= defined $wantverB= $pkgver < $wantver
| |||||||||||||||||||||||||||||
| 125 | 0 | diag( "$pkg VERSION is only $pkgver; this might be fixed by updating to version $wantver" ); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||
| 128 | 0 | diag( "$pkg VERSION is $pkgver; maybe a later version fixes it?" ); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub test_awaitable | ||||||||||||||||||||||||||||
| 133 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
test_awaitable: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 1 | my ( $title, %args ) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | my $class = $args{class}; | |||||||||||||||||||||||||||
| 137 | 1 | ▶ | my $new = $args{new} || sub { return $class->new() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$args{'new'} || sub {
return $class->new;
}
MC/DC0%0/2
$args{'new'} || sub {
return $class->new;
}
$args{'new'}
sub {
return $class->new;
}
Truth table33%1/3
A || B
A= $args{'new'}B= sub {
return $class->new;
}
| |||||||||||||||||||||||||||||
| 138 | 1 | my $cancel = $args{cancel}; | |||||||||||||||||||||||||||
| 139 | 1 | my $force = $args{force}; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | ▶ | subtest "$title immediate done" => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | 1 | ok( my $f = $class->AWAIT_NEW_DONE( "result" ), "AWAIT_NEW_DONE yields object" ); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | ok( $f->AWAIT_IS_READY, 'AWAIT_IS_READY true' ); | |||||||||||||||||||||||||||
| 145 | 1 | ok( !$f->AWAIT_IS_CANCELLED, 'AWAIT_IS_CANCELLED false' ); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | is( [ $f->AWAIT_GET ], [ "result" ], 'AWAIT_GET in list context' ); | |||||||||||||||||||||||||||
| 148 | 1 | is( scalar $f->AWAIT_GET, "result", 'AWAIT_GET in scalar context' ); | |||||||||||||||||||||||||||
| 149 | 1 | ok( defined eval { $f->AWAIT_GET; 1 }, 'AWAIT_GET in void context' ); | |||||||||||||||||||||||||||
| 150 | 1 | }; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 1 | ▶ | subtest "$title immediate fail" => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 153 | 1 | ok( my $f = $class->AWAIT_NEW_FAIL( "Oopsie" ), "AWAIT_NEW_FAIL yields object" ); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | ok( $f->AWAIT_IS_READY, 'AWAIT_IS_READY true' ); | |||||||||||||||||||||||||||
| 156 | 1 | ok( !$f->AWAIT_IS_CANCELLED, 'AWAIT_IS_CANCELLED false' ); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | my $LINE = __LINE__+1; | |||||||||||||||||||||||||||
| 159 | 0 | ok( !defined eval { $f->AWAIT_GET; 1 }, 'AWAIT_GET in void context' ); | |||||||||||||||||||||||||||
| 160 | 1 | ▶ | is( $@, "Oopsie at $FILE line $LINE.\n", 'AWAIT_GET throws exception' ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | _complain_package_version( ref $f ); | ||||||||||||||||||||||||||||
| 162 | 1 | }; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 1 | ▶ | my $fproto = $new->() or BAIL_OUT( "new did not yield an instance" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | ▶ | subtest "$title deferred done" => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | 1 | ok( my $f = $fproto->AWAIT_CLONE, 'AWAIT_CLONE yields object' ); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 1 | ok( !$f->AWAIT_IS_READY, 'AWAIT_IS_READY false' ); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1 | $f->AWAIT_DONE( "Late result" ); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 1 | ok( $f->AWAIT_IS_READY, 'AWAIT_IS_READY true' ); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | is( scalar $f->AWAIT_GET, "Late result", 'AWAIT_GET in scalar context' ); | |||||||||||||||||||||||||||
| 176 | 1 | }; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | ▶ | subtest "$title deferred fail" => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 179 | 1 | ok( my $f = $fproto->AWAIT_CLONE, 'AWAIT_CLONE yields object' ); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1 | ok( !$f->AWAIT_IS_READY, 'AWAIT_IS_READY false' ); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 1 | $f->AWAIT_FAIL( "Late oopsie" ); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | ok( $f->AWAIT_IS_READY, 'AWAIT_IS_READY true' ); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 1 | my $LINE = __LINE__+1; | |||||||||||||||||||||||||||
| 188 | 0 | ok( !defined eval { $f->AWAIT_GET; 1 }, 'AWAIT_GET in void context' ); | |||||||||||||||||||||||||||
| 189 | 1 | ▶ | is( $@, "Late oopsie at $FILE line $LINE.\n", 'AWAIT_GET throws exception' ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | _complain_package_version( ref $f ); | ||||||||||||||||||||||||||||
| 191 | 1 | }; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | ▶ | subtest "$title on-ready" => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 194 | 1 | ▶ | my $f = $new->() or BAIL_OUT( "new did not yield an instance" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 1 | my $called; | |||||||||||||||||||||||||||
| 197 | 1 | ▶ | $f->AWAIT_ON_READY( sub { $called++ } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 198 | 1 | ok( !$called, 'AWAIT_ON_READY CB not yet invoked' ); | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 1 | $f->AWAIT_DONE( "ping" ); | |||||||||||||||||||||||||||
| 201 | 1 | ▶ | $force->( $f ) if $force; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 1 | ok( $called, 'AWAIT_ON_READY CB now invoked' ); | |||||||||||||||||||||||||||
| 203 | 1 | }; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 1 | ▶ | $cancel and subtest "$title cancellation" => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 206 | 1 | ▶ | my $f1 = $new->() or BAIL_OUT( "new did not yield an instance" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1 | my $f2 = $f1->AWAIT_CLONE; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1 | $f1->AWAIT_CHAIN_CANCEL( $f2 ); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 1 | ok( !$f2->AWAIT_IS_CANCELLED, 'AWAIT_IS_CANCELLED false before cancellation' ); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 1 | $cancel->( $f1 ); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1 | ok( $f2->AWAIT_IS_CANCELLED, 'AWAIT_IS_CANCELLED true after AWAIT_ON_CANCEL propagation' ); | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 1 | ▶ | my $f3 = $new->() or BAIL_OUT( "new did not yield an instance" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 1 | my $cancelled; | |||||||||||||||||||||||||||
| 221 | 1 | ▶ | $f3->AWAIT_ON_CANCEL( sub { $cancelled++ } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 1 | $cancel->( $f3 ); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | ok( $cancelled, 'AWAIT_ON_CANCEL invoked callback' ); | |||||||||||||||||||||||||||
| 226 | 1 | ▶ | }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 1 | 0x55AA; | |||||||||||||||||||||||||||