#!/usr/bin/perl
#
# connect to one js, it times out, try another, no retry count, it succeeds
#

use strict;
use FindBin qw($Bin);
require "$Bin/lib/testlib.pl";

use Test::More;

use constant PORT => 9000;

if (start_server(PORT)) {
    plan tests => 3;
} else {
    plan skip_all => "Can't find server to test with";
    exit 0;
}

start_worker(PORT, 1);

my $client = Gearman::Client::Async->new;
$client->set_job_servers('127.0.0.1:9001', '127.0.0.1:9000');
$client->t_set_offline_host('127.0.0.1:9001'); # treat it as off the net, unplugged.
$client->t_set_disable_random(1);

my $completed = 0;
my $failed = 0;
my $done   = 0;
my $did_timeout = 0;

{
    no warnings;
    $Gearman::Client::Async::Connection::T_ON_TIMEOUT = sub {
        $did_timeout++;
    };
}

my $n_loops = 3;
for (1..$n_loops) {
    $client->add_task( Gearman::Task->new( "sleep_for" => \ "0.5", {
        on_complete => sub {
            $completed++;
            $done = 1 if $completed == $n_loops;
        },
        on_fail => sub {
            $failed = 1;
        },
    }));
}

Danga::Socket->AddTimer(3.0, sub { $done = 1; });

Danga::Socket->SetPostLoopCallback(sub {
    return !$done;
});

Danga::Socket->EventLoop();

ok(!$failed, "insertion didn't fail");
is($did_timeout, 1, "and connect did timeout, once");
is($completed, $n_loops, "completed $n_loops times");




