How can I use multiple user agents (with different options) under a single
event loop in controller?

Example:

$ cat x.t
#!/usr/bin/perl

use Mojolicious::Lite;
use Test::Mojo;

get '/' => sub {
  my $c = shift;
  $c->render_later;

  my @res;
  my $delay = Mojo::IOLoop->delay;
  $delay->on(finish => sub {
    $c->render(text => join "\n", @res);
  });

  for my $url ('http://cpan.org/', 'http://metacpan.org/') {
    my $ua = Mojo::UserAgent->new(max_redirects => 1 + int rand 5);
    my $end = $delay->begin;
    $ua->get($url => sub {
      my ($ua, $tx) = @_;

      if (my $err = $tx->error) {
        push @res, "$url => $err->{code} response: $err->{message}" if
$err->{code};
        push @res, "$url => Connection error: $err->{message}";
      } else {
        push @res, "$url => " . $tx->res->code;
      }
      $end->();

    });
  }
};

app->start;

my $t = Test::Mojo->new;
$t->get_ok('/')->status_is(200)->content_unlike(qr/error/);

$ prove x.t
x.t .. 1/?
#   Failed test 'content is not similar'
#   at x.t line 36.
#                   'http://cpan.org/ => Connection error: Premature
connection close
# http://metacpan.org/ => Connection error: Premature connection close'
#           matches '(?^:error)'
# Tests were run but no plan was declared and done_testing() was not seen.
x.t .. Failed 1/3 subtests

Test Summary Report
-------------------
x.t (Wstat: 0 Tests: 3 Failed: 1)
  Failed test:  3
  Parse errors: No plan found in TAP output
Files=1, Tests=3,  1 wallclock secs ( 0.04 usr  0.00 sys +  0.47 cusr
0.05 csys =  0.56 CPU)
Result: FAIL
$

-- 
Andrey Khozov

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to