Skip to content

Instantly share code, notes, and snippets.

@bigpresh
Last active February 27, 2021 04:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bigpresh/3882c22eece1a9221cf24679402579e8 to your computer and use it in GitHub Desktop.
Save bigpresh/3882c22eece1a9221cf24679402579e8 to your computer and use it in GitHub Desktop.
Dancer2 session stress testing
#!/usr/bin/env perl
use 5.012;
use LWP::UserAgent;
use Test::More qw(no_plan);
my $ua = LWP::UserAgent->new( cookie_jar => {} );
my $start_name = "Test$$";
my $name = $start_name;
my $last_value = $name;
for(1..100) {
# For each outer iteration, clear our cookie jar so we get a new session
$ua->cookie_jar({});
$ua->get("http://localhost:5000/its/$name");
for (1..100) {
my $response = $ua->get("http://localhost:5000/hi");
like($response->content, qr/Hi again, $name/, "Response greeted $name");
}
# Same cookie jar, but change session content, and expect change to have
# taken on next iteration of fetches
$name = $start_name . time();
$ua->get("http://localhost:5000/its/$name");
for (1..100) {
my $response = $ua->get("http://localhost:5000/hi");
like($response->content, qr/Hi again, $name/, "Response greeted $name");
}
}
package testsessions;
use Dancer2;
our $VERSION = '0.1';
get '/' => sub {
template 'index' => { 'title' => 'testsessions' };
};
get '/its/:name' => sub {
session name => route_parameters->{name};
warn "Set name to " . route_parameters->{name};
return "Hi there, " . session('name') . " (" . route_parameters->{name} . ")";
};
get '/hi' => sub {
my $name = session('name');
if (!$name) {
warn "***** FAILED TO FETCH NAME *****";
warn "session ID: " . session->id;
}
return "Hi again, " . session('name');
};
true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment