timer support in MiniLoop
[scpubgit/Object-Remote.git] / t / start_core.t
1 use strictures 1;
2 use Test::More;
3 use Object::Remote;
4 use File::Spec;
5
6 {
7   package S1S;
8
9   use Moo;
10
11   sub get_s2 {
12     S2S->new
13   }
14 }
15
16 {
17   package S1F;
18
19   use Object::Remote::Future;
20   use Moo;
21
22   our $C;
23
24   sub get_s2 {
25     shift->maybe::start::_real_get_s2;
26   }
27
28   sub _real_get_s2 {
29     future {
30       my $f = shift;
31       $C = sub { $f->done(S2F->new); undef($f); undef($C); };
32       $f;
33     }
34   }
35 }
36
37 {
38   package S2S;
39
40   use Moo;
41
42   sub get_s3 { 'S3' }
43 }
44
45 {
46   package S2F;
47
48   use Object::Remote::Future;
49   use Moo;
50
51   our $C;
52
53   sub get_s3 {
54     future {
55       my $f = shift;
56       $C = sub { $f->done('S3'); undef($f); undef($C); };
57       $f;
58     }
59   }
60 }
61
62 my $res;
63
64 S1S->start::get_s2->then::get_s3->on_ready(sub { ($res) = $_[0]->get });
65
66 is($res, 'S3', 'Synchronous code ok');
67
68 undef($res);
69
70 S1F->start::get_s2->then::get_s3->on_ready(sub { ($res) = $_[0]->get });
71
72 ok(!$S2F::C, 'Second future not yet constructed');
73
74 $S1F::C->();
75
76 ok($S2F::C, 'Second future constructed after first future completed');
77
78 ok(!$res, 'Nothing happened yet');
79
80 $S2F::C->();
81
82 is($res, 'S3', 'Asynchronous code ok');
83
84 is(S1S->get_s2->get_s3, 'S3', 'Sync without start');
85
86 Object::Remote->current_loop->watch_time(
87   after => 0.1,
88   code => sub {
89     $S1F::C->();
90     Object::Remote->current_loop->watch_time(
91       after => 0.1,
92       code => sub { $S2F::C->() }
93     );
94   }
95 );
96
97 is(S1F->get_s2->get_s3, 'S3', 'Async without start');
98
99 done_testing;