Skip to content

Instantly share code, notes, and snippets.

@kgoess
Created June 28, 2019 00:10
Show Gist options
  • Save kgoess/275cc68b1bb7c5dd50e0a9463b4c2217 to your computer and use it in GitHub Desktop.
Save kgoess/275cc68b1bb7c5dd50e0a9463b4c2217 to your computer and use it in GitHub Desktop.
test case for nytprof with callbacks.
diff --git a/t/lib/ExampleApp.pm b/t/lib/ExampleApp.pm
index 8bfc57a..06c1aca 100644
--- a/t/lib/ExampleApp.pm
+++ b/t/lib/ExampleApp.pm
@@ -13,13 +13,16 @@ sub startup {
pre_hook => 'before_routes',
post_hook => 'around_dispatch',
},
});
$self->routes->any('/t1')->to('ExampleController#t1');
$self->routes->any('/t2')->to('ExampleController#t2');
$self->routes->any('/t3')->to('ExampleController#t3');
$self->routes->any('/t4')->to('ExampleController#t4');
$self->routes->any('/t5')->to('ExampleController#t5');
+
+ $self->routes->any('/nonblock')->to('ExampleController#nonblock');
+
}
1;
diff --git a/t/lib/ExampleApp/ExampleController.pm b/t/lib/ExampleApp/ExampleController.pm
index cdef580..a8bb58c 100644
--- a/t/lib/ExampleApp/ExampleController.pm
+++ b/t/lib/ExampleApp/ExampleController.pm
@@ -11,11 +11,51 @@ sub t2 {
}
sub t3 {
my $self=shift;
$self->render; # explicit render example/t3.html.ep
}
sub t4 {
my $self=shift;
$self->render(template=>'t3');
}
+sub nonblock {
+ my $self=shift;
+
+ my $delay = Mojo::IOLoop::Delay->new;
+
+ $self->app->log->info("starting request, first sleep");
+
+ sleeping_before_any_callbacks();
+
+ $delay->steps(
+ sub {
+ my $end = $delay->begin;
+ $self->app->log->info("in first callback");
+ sleeping_in_first_callback();
+ $end->();
+ },
+ sub {
+ my $end = $delay->begin;
+ $self->app->log->info("in second callback");
+ sleeping_in_second_callback();
+ $end->();
+ $self->render(text=>"Done with second callback, returning this response\n");
+ },
+ );
+ $self->app->log->info("about to wait");
+ $delay->wait unless Mojo::IOLoop->is_running;
+
+ $self->app->log->info("returning from controller");
+}
+
+sub sleeping_before_any_callbacks {
+ sleep 1;
+}
+sub sleeping_in_first_callback {
+ sleep 1;
+}
+sub sleeping_in_second_callback {
+ sleep 1;
+}
+
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment