Acme-Parataxis

 view release on metacpan or  search on metacpan

t/014_http_pool.t  view on Meta::CPAN

                }
            }
        }
        return $self->SUPER::_do_timeout( $type, 0 );
    }
}
Acme::Parataxis::run(
    sub {
        # Testing reentrancy: Use a SINGLE HTTP::Tiny object across multiple concurrent fibers
        my $http  = Acme::Parataxis::Test::PoolHTTP->new( timeout => 10, verify_SSL => 0 );
        my @queue = qw[
            http://example.com
            https://www.google.com/
            https://www.perl.org/
            https://metacpan.org/
            https://www.cpan.org/
            https://github.com/
        ];
        my %results;
        my $worker_count = 3;
        my @workers;
        diag "Main: Starting worker pool with $worker_count fibers to process " . scalar(@queue) . " URLs...";

        for my $w_id ( 1 .. $worker_count ) {
            push @workers, Acme::Parataxis->spawn(
                sub {
                    my $fid = Acme::Parataxis->current_fid;
                    while (1) {
                        my $url = shift @queue;
                        last unless $url;
                        my $res = $http->get($url);
                        $results{$url} = $res;
                    }
                    return "Worker $w_id finished.";
                }
            );
        }

        # Wait for all workers to complete
        diag 'Main: Waiting for pool to drain...';
        $_->await for @workers;

        # Verify results
        my @urls_to_check = qw[
            http://example.com
            https://www.google.com/
            https://www.perl.org/
            https://metacpan.org/
            https://www.cpan.org/
            https://github.com/
        ];

        for my $url (@urls_to_check) {
            my $res = $results{$url};
            todo "Pooled network fetch for $url might fail" => sub {
                ok( $res, "Result exists for $url" );
                if ($res) {
                    is( $res->{status}, 200, "Fetched $url successfully" ) or
                        diag "Failed to fetch $url: $res->{status} " . ( $res->{reason} // 'No Reason' ) . "\nContent: " . ( $res->{content} // '' );
                    if ( $res->{status} == 200 ) {
                        like( $res->{content}, qr/<html/i, "$url content looks like HTML" );
                    }
                }
            };
        }
        Acme::Parataxis::stop();
    }
);
done_testing();



( run in 0.902 second using v1.01-cache-2.11-cpan-5a3173703d6 )