#!/usr/bin/perl

use strict;
use warnings;

use Apache::Test qw( -withtestmore );

plan tests => 19;

use lib qw( ./t ./lib );

use PS::IPP::IData::Client;
use PS::IPP::IData::Util qw( :standard );
use Test::IData;

my $hostport = Apache::Test->config->{ 'hostport' };

Test::IData->setup;

{
    # key, type
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    ok( $idata->lock( "foo", "read" ), "read lock" );
}

Test::IData->setup;

{
    # key, type, timeout
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    ok( $idata->lock( "foo", "read", 10 ), "read lock" );
}

Test::IData->setup;

{
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    ok( $idata->lock( "foo", "read" ), "read lock" );
    ok( $idata->lock( "foo", "read" ), "read lock" );
}

Test::IData->setup;

{
    # key, type
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    ok( $idata->lock( "foo", "write" ), "write lock" );
}

Test::IData->setup;

{
    # key, type, timeout
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    ok( $idata->lock( "foo", "write", 10 ), "write lock" );
}

Test::IData->setup;

{
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );

    is( $idata->lock( "foo", "read" ), undef, "storage object does not exist" );
}

Test::IData->setup;

{
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );

    is( $idata->lock( "foo", "write" ), undef, "storage object does not exist" );
}

Test::IData->setup;

diag "testing a spinlock, this will be a litle slow...";

eval {
    local $SIG{ALRM} = sub { alarm 0; die "timeout"; };
    alarm 8;

    # key, type, timeout
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->lock( "foo", "read" );
    $idata->lock( "foo", "write" );

    alarm 0;
};
like( $@, qr/timeout/, "default spinlock" );

Test::IData->setup;

diag "testing a spinlock, this will be a litle slow...";

eval {
    local $SIG{ALRM} = sub { alarm 0; die "timeout"; };
    alarm 12;

    # key, type, timeout
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->lock( "foo", "read" );
    unless ( $idata->lock( "foo", "write" ) ) {
        alarm 0;
        die "gaveup";
    }

    alarm 0;
};
like( $@, qr/gaveup/, "default spinlock" );

Test::IData->setup;

diag "testing a spinlock, this will be a litle slow...";

eval {
    local $SIG{ALRM} = sub { alarm 0; die "timeout"; };
    alarm 3;

    # key, type, timeout
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->lock( "foo", "read" );
    $idata->lock( "foo", "write", 5 );

    alarm 0;
};
like( $@, qr/timeout/, "specified spinlock" );

Test::IData->setup;

diag "testing a spinlock, this will be a litle slow...";

eval {
    local $SIG{ALRM} = sub { alarm 0; die "timeout"; };
    alarm 7;

    # key, type, timeout
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->lock( "foo", "read" );
    unless ( $idata->lock( "foo", "write", 5 ) ) {
        alarm 0;
        die "gaveup";
    }

    alarm 0;
};

like( $@, qr/gaveup/, "specified spinlock" );

Test::IData->setup;

{
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->lock( "foo", "write" );
    is( $idata->lock( "foo", "write", 1 ), undef, "can not write lock twice" );
}

Test::IData->setup;

{
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->lock( "foo", "read" );
    is( $idata->lock( "foo", "write", 1 ), undef, "can not write lock after read lock" );
}

Test::IData->setup;

{
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->lock( "foo", "write" );
    is( $idata->lock( "foo", "read", 1 ), undef, "can not read lock after write lock" );
}

Test::IData->setup;

eval {
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );

    $idata->lock();
};
like( $@, qr/2 - 3 were expected/, "no params" );

Test::IData->setup;

eval {
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );

    $idata->lock( "foo" );
};
like( $@, qr/2 - 3 were expected/, "not enough params" );

Test::IData->setup;

eval {
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );

    $idata->lock( "foo", "both" );
};
like( $@, qr/is read or write/, "not read or write" );

Test::IData->setup;

eval {
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );

    $idata->lock( "foo", 'read', 3, "bar" );
};
like( $@, qr/2 - 3 were expected/, "too many params" );

Test::IData->cleanup;
