test without start
[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     future {
26       my $f = shift;
27       $C = sub { $f->done(S2F->new); undef($f); undef($C); };
28       $f;
29     }
30   }
31 }
32
33 {
34   package S2S;
35
36   use Moo;
37
38   sub get_s3 { 'S3' }
39 }
40
41 {
42   package S2F;
43
44   use Object::Remote::Future;
45   use Moo;
46
47   our $C;
48
49   sub get_s3 {
50     future {
51       my $f = shift;
52       $C = sub { $f->done('S3'); undef($f); undef($C); };
53       $f;
54     }
55   }
56 }
57
58 my $res;
59
60 S1S->start::get_s2->then::get_s3->on_ready(sub { ($res) = $_[0]->get });
61
62 is($res, 'S3', 'Synchronous code ok');
63
64 undef($res);
65
66 S1F->start::get_s2->then::get_s3->on_ready(sub { ($res) = $_[0]->get });
67
68 ok(!$S2F::C, 'Second future not yet constructed');
69
70 $S1F::C->();
71
72 ok($S2F::C, 'Second future constructed after first future completed');
73
74 ok(!$res, 'Nothing happened yet');
75
76 $S2F::C->();
77
78 is($res, 'S3', 'Asynchronous code ok');
79
80 is(S1S->get_s2->get_s3, 'S3', 'Sync without start');
81
82 open my $fh, '<', File::Spec->devnull;
83
84 Object::Remote->current_loop->watch_io(
85   handle => $fh,
86   on_read_ready => sub {
87     $S1F::C->() if defined $S1F::C;
88     $S2F::C->() if defined $S2F::C;
89   }
90 );
91
92 is(S1F->get_s2->get_s3, 'S3', 'Async without start');
93
94 done_testing;