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