Apache-Session-Browseable

 view release on metacpan or  search on metacpan

t/Apache-Session-Browseable-Store-Patroni.t  view on Meta::CPAN

        my $result = $store->_useCachedLeader( $args1, $ds, "Test" );
        is( $result, 0, 'Custom TTL 30s: cache expired at 45s' );
    }

    # With 60s TTL, cache should be valid
    my $args2 = { DataSource => $ds, PatroniCacheTTL => 60 };
    $stderr = '';
    {
        local *STDERR;
        open STDERR, '>', \$stderr;
        my $result = $store->_useCachedLeader( $args2, $ds, "Test" );
        is( $result, 1, 'Custom TTL 60s: cache valid at 45s' );
    }

    # Clear cache
    %Apache::Session::Browseable::Store::Patroni::patroniCache = ();
}

# Test module can be loaded
use_ok('Apache::Session::Browseable::Patroni');

# Test JSON parsing and leader validation with mocked HTTP using LWP::Protocol::PSGI
SKIP: {
    skip 'LWP::Protocol::PSGI and JSON not available', 10
      unless eval { require LWP::Protocol::PSGI; require JSON; 1 };

    my $package = 'Apache::Session::Browseable::Store::Patroni';
    my $ds      = 'dbi:Pg:dbname=mocktest';

    # Helper to create PSGI app with given response
    my $make_app = sub {
        my ($json_data) = @_;
        return sub {
            my $env = shift;
            return [
                200,
                [ 'Content-Type' => 'application/json' ],
                [ JSON::to_json($json_data) ]
            ];
        };
    };

    # Test 1: Valid leader response
    {
        %Apache::Session::Browseable::Store::Patroni::patroniCache = ();

        my $guard = LWP::Protocol::PSGI->register(
            $make_app->(
                {
                    members => [
                        {
                            role  => 'leader',
                            host  => '10.0.0.100',
                            port  => 5432,
                            state => 'running'
                        },
                        {
                            role  => 'replica',
                            host  => '10.0.0.101',
                            port  => 5432,
                            state => 'streaming'
                        }
                    ]
                }
            )
        );

        my $store = $package->new;
        $store->{_originalDataSource} = $ds;
        my $args = {
            DataSource => $ds,
            PatroniUrl => 'http://mock:8008/cluster'
        };

        my $stderr = '';
        my $result;
        {
            local *STDERR;
            open STDERR, '>', \$stderr;
            $result = $store->checkMaster($args);
        }
        is( $result, 1, 'Valid leader: checkMaster returns 1' );
        like( $args->{DataSource}, qr/host=10\.0\.0\.100/,
            'Valid leader: correct host in DataSource' );
    }

    # Test 2: Split-brain detection (multiple leaders)
    {
        %Apache::Session::Browseable::Store::Patroni::patroniCache = ();

        my $guard = LWP::Protocol::PSGI->register(
            $make_app->(
                {
                    members => [
                        {
                            role  => 'leader',
                            host  => '10.0.0.100',
                            port  => 5432,
                            state => 'running'
                        },
                        {
                            role  => 'leader',
                            host  => '10.0.0.101',
                            port  => 5432,
                            state => 'running'
                        }
                    ]
                }
            )
        );

        my $store = $package->new;
        $store->{_originalDataSource} = $ds;
        my $args = {
            DataSource => $ds,
            PatroniUrl => 'http://mock:8008/cluster'
        };

        my $stderr = '';
        my $result;
        {

t/Apache-Session-Browseable-Store-Patroni.t  view on Meta::CPAN

        is( $result, 0, 'Leader starting: checkMaster returns 0' );
        like(
            $stderr,
            qr/not in running state/,
            'Leader starting: warning message'
        );
    }

    # Test 4: Leader missing host
    {
        %Apache::Session::Browseable::Store::Patroni::patroniCache = ();

        my $guard = LWP::Protocol::PSGI->register(
            $make_app->(
                {
                    members => [
                        {
                            role  => 'leader',
                            port  => 5432,
                            state => 'running'
                        }
                    ]
                }
            )
        );

        my $store = $package->new;
        $store->{_originalDataSource} = $ds;
        my $args = {
            DataSource => $ds,
            PatroniUrl => 'http://mock:8008/cluster'
        };

        my $stderr = '';
        my $result;
        {
            local *STDERR;
            open STDERR, '>', \$stderr;
            $result = $store->checkMaster($args);
        }
        is( $result, 0, 'Leader missing host: checkMaster returns 0' );
        like(
            $stderr,
            qr/missing host or port/,
            'Leader missing host: warning message'
        );
    }

    # Test 5: No leader found
    {
        %Apache::Session::Browseable::Store::Patroni::patroniCache = ();

        my $guard = LWP::Protocol::PSGI->register(
            $make_app->(
                {
                    members => [
                        {
                            role  => 'replica',
                            host  => '10.0.0.101',
                            port  => 5432,
                            state => 'streaming'
                        }
                    ]
                }
            )
        );

        my $store = $package->new;
        $store->{_originalDataSource} = $ds;
        my $args = {
            DataSource => $ds,
            PatroniUrl => 'http://mock:8008/cluster'
        };

        my $stderr = '';
        my $result;
        {
            local *STDERR;
            open STDERR, '>', \$stderr;
            $result = $store->checkMaster($args);
        }
        is( $result, 0, 'No leader: checkMaster returns 0' );
        like( $stderr, qr/No leader found/, 'No leader: warning message' );
    }

    # Clear cache
    %Apache::Session::Browseable::Store::Patroni::patroniCache = ();
}



( run in 1.208 second using v1.01-cache-2.11-cpan-140bd7fdf52 )