Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 67 lines (53 sloc) 2.002 kb
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
1 #!/usr/bin/env perl
5b0127b @rcaputo Remove emit() from roles.
authored
2 # vim: ts=2 sw=2 noexpandtab
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
3
4854633 @rcaputo Convert this example into a test.
authored
4 # Reflex APIs are built in layers. This test exercises Reflex's
5 # low-level watcher API. There are much more concise and convenient
6 # APIs layered atop this one.
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
7 #
4854633 @rcaputo Convert this example into a test.
authored
8 # The test creates two objects: One to periodically emit events, and
9 # another to watch for those events. Receipt of those events is
10 # verified, as well as natural program exit when all events are done.
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
11 #
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored
12 # While verbose, the watch() syntax allows multiple objects to
c00d096 @rcaputo Revise discussion of tests 901 and 902.
authored
13 # consume events from a single emitter. Most other event systems only
14 # allow one event consumer.
15 #
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored
16 # In some cases, events can be lost if watch() is called after an
c00d096 @rcaputo Revise discussion of tests 901 and 902.
authored
17 # event emitter is created. However, this shouldn't happen if the
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored
18 # emitter and its watcher are created in the same basic block of code.
c00d096 @rcaputo Revise discussion of tests 901 and 902.
authored
19 # Under normal circumstances, events are not dispatched in the middle
20 # of a basic block of code, so events cannot be lost there.
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
21 #
22 # TODO - Another option is to create an object in a stopped state,
b11c4bb @rcaputo Add inheritance and I/O support. Emitted events can be handled by subcl...
authored
23 # then start it after watchers have been registered.
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
24
25 use warnings;
26 use strict;
15add25 @rcaputo Fix library paths to find common test helper libraries.
authored
27 use lib qw(t/lib);
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
28
1f5ed27 @rcaputo Rename Reflex::Role::Object to Reflex::Role::Reactive; Reflex::Object to...
authored
29 use Reflex::Base;
bbf149d @rcaputo Create Reflex::Role::Interval, and reify it as Reflex::Interval. Needed...
authored
30 use Reflex::Interval;
3af6b57 @rcaputo Initial Reflex::Callbacks integration into Reflex at large.
authored
31 use Reflex::Callbacks qw(cb_coderef);
4854633 @rcaputo Convert this example into a test.
authored
32
33 use Test::More tests => 6;
34
35 ### Create a timer. This timer will be watched for events.
36
bbf149d @rcaputo Create Reflex::Role::Interval, and reify it as Reflex::Interval. Needed...
authored
37 my $timer = Reflex::Interval->new( interval => 0.1, auto_repeat => 1 );
4854633 @rcaputo Convert this example into a test.
authored
38 ok( (defined $timer), "started timer object" );
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
39
4854633 @rcaputo Convert this example into a test.
authored
40 ### Create an object to watch the timer.
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
41
1f5ed27 @rcaputo Rename Reflex::Role::Object to Reflex::Role::Reactive; Reflex::Object to...
authored
42 my $watcher = Reflex::Base->new();
4854633 @rcaputo Convert this example into a test.
authored
43 ok( (defined $watcher), "started watcher object" );
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
44
4854633 @rcaputo Convert this example into a test.
authored
45 ### The watcher will now watch the timer for a little while.
8364e2e @rcaputo Clean up the tests a little more.
authored
46 #
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored
47 # The watcher only exists so that watch() may be called. A better
1f5ed27 @rcaputo Rename Reflex::Role::Object to Reflex::Role::Reactive; Reflex::Object to...
authored
48 # example would have "tick" handled by one of Reflex::Base's methods.
4854633 @rcaputo Convert this example into a test.
authored
49
50 my $countdown = 3;
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored
51 $watcher->watch(
3af6b57 @rcaputo Initial Reflex::Callbacks integration into Reflex at large.
authored
52 $timer,
4854633 @rcaputo Convert this example into a test.
authored
53 tick => cb_coderef(
54 sub {
8364e2e @rcaputo Clean up the tests a little more.
authored
55 pass("'tick' callback invoked ($countdown)");
4854633 @rcaputo Convert this example into a test.
authored
56 $timer = undef unless --$countdown;
57 }
58 ),
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
59 );
60
4854633 @rcaputo Convert this example into a test.
authored
61 ### Allow the timer and its watcher to run until they are done.
62
1f5ed27 @rcaputo Rename Reflex::Role::Object to Reflex::Role::Reactive; Reflex::Object to...
authored
63 Reflex->run_all();
4854633 @rcaputo Convert this example into a test.
authored
64 pass("run_all() returned");
65
faf205c @rcaputo Break the usecase.pl example into three discrete examples.
authored
66 exit;
Something went wrong with that request. Please try again.