#! /usr/bin/perl -w

BEGIN
{
        push(@INC, $ENV{'GLOBUS_LOCATION'} . '/lib/perl');
}

use Globus::Core::Paths;
use Globus::Testing::Container;
use POSIX qw(getcwd);
use Test;
use strict;

my (@test_cases) = ();
my $wd = getcwd();
my $descriptor = "$wd/global_security_descriptor.xml";

my $containers = {
    c => new Globus::Testing::Container(
        {
            type => 'c',
            security => 0
        }),
    sec_c => new Globus::Testing::Container(
        {
            type => 'c',
            security => 1
        }),
    java => new Globus::Testing::Container(
        {
            type => 'java',
            security => 0
        }),
    sec_java => new Globus::Testing::Container(
        {
            type => 'java',
            security => 1,
            descriptor => $descriptor
        })
};
my %contacts = (
    'c-nosec' => $containers->{c}->contact(),
    'java-nosec' => $containers->{java}->contact(),
    'c-sec' => $containers->{sec_c}->contact(),
    'java-sec' => $containers->{sec_java}->contact(),
);

my $exec = './get-multiple-resource-pingpong';
my @persistence = ('nonpersistent', 'persistent');
    
foreach my $iterations (1, 10, 100) {
    foreach my $contact (keys %contacts) {
        foreach my $persistence (@persistence) {
            my $log = "$contact-$persistence-$iterations.log";
            my $case = [];

            if ($persistence eq 'persistent' && $contact =~ m/^c-/) {
                push(@test_cases,
                    [ 
                      $exec,
                      '-i', $iterations,
                      '-m', 1000,
                      '-l', $log,
                      '-s', "$contacts{$contact}/wsrf/services/WidgetService" ]);
            } elsif ($persistence eq 'nonpersistent') {
                push(@test_cases,
                    [ $exec,
                      '-i', $iterations,
                      '-m', 1000,
                      '-l', $log,
                      '-n',
                      '-s', "$contacts{$contact}/wsrf/services/WidgetService" ]);
            }
        }
    }
}

plan tests => scalar(@test_cases);

foreach my $test (@test_cases) {
    &pingpong_test($test);
}

sub pingpong_test {
    my $test_case = shift;
    my $contact = $test_case->[$#{$test_case}];
    my $no_contact = $contact eq '/wsrf/services/WidgetService';
    skip($no_contact, system({$test_case->[0]} (@{$test_case})), 0);
}
