Release commit for 0.003006
[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 $ENV{OBJECT_REMOTE_TEST_LOGGER} = 1;
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 my @keep;
67
68 push @keep,
69   S1S->start::get_s2->then::get_s3->on_ready(sub { ($res) = $_[0]->get });
70
71 is($res, 'S3', 'Synchronous code ok');
72
73 undef($res);
74
75 push @keep,
76   S1F->start::get_s2->then::get_s3->on_ready(sub { ($res) = $_[0]->get });
77
78 ok(!$S2F::C, 'Second future not yet constructed');
79
80 $S1F::C->();
81
82 ok($S2F::C, 'Second future constructed after first future completed');
83
84 ok(!$res, 'Nothing happened yet');
85
86 $S2F::C->();
87
88 is($res, 'S3', 'Asynchronous code ok');
89
90 is(S1S->get_s2->get_s3, 'S3', 'Sync without start');
91
92 Object::Remote->current_loop->watch_time(
93   after => 0.1,
94   code => sub {
95     $S1F::C->();
96     Object::Remote->current_loop->watch_time(
97       after => 0.1,
98       code => sub { $S2F::C->() }
99     );
100   }
101 );
102
103 is(S1F->get_s2->get_s3, 'S3', 'Async without start');
104
105 done_testing;