Mojo::IOLoop is singleton - how to do testing....

113 views
Skip to first unread message

Daniel Suen

unread,
Jan 4, 2016, 3:52:26 PM1/4/16
to Mojolicious
Hi, I have my webapp where I have a Mojo::IOLoop->recurring(10 => sub { # relaoding from db }) running (inside the app in the background, not really handling any web request) that reloads some configuration from the database.

When I want to do testing, I want to insert a testing entry in the database, and then run some testing code. But it does not work if I do,

#
# insert some test artifact to the database
#
Mojo::IOLoop->timer(15 => sub { print "done waiting for database reload" });

Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

#
# some cleanup code
#

done_testing;

I saw that the whole thing just keep on going without running the cleanup code (the reloading keeps running). So I think the Mojo::IOLoop is the singleton and since the recurring timer is still active, it just keeps on running. What is the proper way to make it working? Any ideas are appreciated.

Daniel.

s...@alexbyk.com

unread,
Jan 4, 2016, 4:50:57 PM1/4/16
to mojol...@googlegroups.com
# 1
# start only in production mode. Bad but quick
if ($app->mode eq 'production') {
  Mojo::IOLoop->recurring(10 => sub {...});
}

This doesn't look like a dirty testing hack, this is exactly a dirty testing hack... Singletons are untestable

# 2
# This is much better and more perlish
package App;

sub start_foo {
  Mojo::IOLoop->recurring(10 => sub {...});
}

package main;
local *App::start_foo = sub {'mock'};
#.... you test here


--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages