| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use v5.14; |
| 4 | use warnings; |
| 5 | |
| 6 | use Test2::V0; |
| 7 | |
| 8 | use Future; |
| 9 | use Future::Queue; |
| 10 | |
| 11 | # bulk-push before shift |
| 12 | { |
| 13 | my $queue = Future::Queue->new; |
| 14 | |
| 15 | $queue->push( qw( A B C ) ); |
| 16 | |
| 17 | is( $queue->shift->result, "A", 'shift 1' ); |
| 18 | is( $queue->shift->result, "B", 'shift 2' ); |
| 19 | is( $queue->shift->result, "C", 'shift 3' ); |
| 20 | } |
| 21 | |
| 22 | # push before bulk-shift |
| 23 | { |
| 24 | my $queue = Future::Queue->new; |
| 25 | |
| 26 | $queue->push( $_ ) for qw( A B C D ); |
| 27 | |
| 28 | is( [ $queue->shift_atmost( 3 )->result ], [qw( A B C )], |
| 29 | '->shift_atmost can yield multiple' ); |
| 30 | |
| 31 | is( [ $queue->shift_atmost( 3 )->result ], [qw( D )], |
| 32 | '->shift_atmost yields when non-empty' ); |
| 33 | |
| 34 | ok( !$queue->shift_atmost( 3 )->is_ready, |
| 35 | '->shift_atmost pending when empty' ); |
| 36 | } |
| 37 | |
| 38 | # shift before bulk-push |
| 39 | { |
| 40 | my $queue = Future::Queue->new; |
| 41 | |
| 42 | my $f1 = $queue->shift; |
| 43 | my $f2 = $queue->shift; |
| 44 | my $f3 = $queue->shift; |
| 45 | |
| 46 | $queue->push( qw( A B C ) ); |
| 47 | |
| 48 | is( $f1->result, "A", 'shift 1' ); |
| 49 | is( $f2->result, "B", 'shift 2' ); |
| 50 | is( $f3->result, "C", 'shift 3' ); |
| 51 | } |
| 52 | |
| 53 | # bulk-shift before push |
| 54 | { |
| 55 | my $queue = Future::Queue->new; |
| 56 | |
| 57 | my $f1 = $queue->shift_atmost( 3 ); |
| 58 | my $f2 = $queue->shift_atmost( 3 ); |
| 59 | |
| 60 | $queue->push( $_ ) for qw( A B C ); |
| 61 | |
| 62 | is( [ $f1->result ], [qw( A )], |
| 63 | 'shift_atmost yielded first pushed item' ); |
| 64 | |
| 65 | is( [ $f2->result ], [qw( B )], |
| 66 | 'shift_atmost again yielded second pushed item' ); |
| 67 | } |
| 68 | |
| 69 | # bulk-shift before bulk-push |
| 70 | { |
| 71 | my $queue = Future::Queue->new; |
| 72 | |
| 73 | my $f1 = $queue->shift_atmost( 3 ); |
| 74 | my $f2 = $queue->shift_atmost( 3 ); |
| 75 | |
| 76 | $queue->push( qw( A B C D ) ); |
| 77 | |
| 78 | is( [ $f1->result ], [qw( A B C )], |
| 79 | 'shift_atmost yielded first three pushed items' ); |
| 80 | |
| 81 | is( [ $f2->result ], [qw( D )], |
| 82 | 'shift_atmost again yielded remaining pushed item' ); |
| 83 | } |
| 84 | |
| 85 | # bulk-push with max_items |
| 86 | { |
| 87 | my $queue = Future::Queue->new( max_items => 3 ); |
| 88 | |
| 89 | my $push_f = $queue->push( qw( A B C D E ) ); |
| 90 | |
| 91 | ok( !$push_f->is_done, 'bulk push not yet ready while over-size' ); |
| 92 | |
| 93 | $queue->shift->result; |
| 94 | ok( !$push_f->is_done, 'bulk push still not ready after first shift' ); |
| 95 | |
| 96 | $queue->shift->result; |
| 97 | |
| 98 | ok( $push_f->is_done, 'bulk push ready after second shift' ); |
| 99 | } |
| 100 | |
| 101 | done_testing; |