Debian Codegraph @

t/unit/App/Yath/Command/test.t

raw
1 use Test2::V0 -target => 'App::Yath::Command::test';
2 # HARNESS-DURATION-SHORT
3
4 use ok $CLASS;
5
6 use Errno();
7 use Test2::Harness::Settings();
8 use Test2::Harness::Util::JSON qw/encode_json/;
9
10 use constant SETTINGS => App::Yath::Command::test::SETTINGS();
11 use constant RENDERERS => App::Yath::Command::test::RENDERERS();
12 use constant RENDERER_READER => App::Yath::Command::test::RENDERER_READER();
13 use constant ASSERTS_SEEN => App::Yath::Command::test::ASSERTS_SEEN();
14
15 # render() reads a pipe and drives renderers; it needs no run behind it. Build
16 # the object directly. Note ipc() is lazy and self-building, so render() always
17 # gets a real Test2::Harness::IPC. That is harmless here: it installs no signal
18 # handlers until start(), and wait() returns immediately with no processes.
19 sub build_cmd {
20 my ($reader, @renderers) = @_;
21
22 my $settings = Test2::Harness::Settings->new(
23 harness => {plugins => []},
24 logging => {log => 0},
25 );
26
27 return bless(
28 {
29 SETTINGS() => $settings,
30 RENDERERS() => \@renderers,
31 RENDERER_READER() => $reader,
32 },
33 $CLASS,
34 );
35 }
36
37 sub event_line {
38 my ($fd) = @_;
39 return encode_json({facet_data => $fd}) . "\n";
40 }
41
42 # Runs $cmd->render() under an alarm. Returns 1 if it returned on its own,
43 # 0 if the alarm had to break it out.
44 sub render_under_alarm {
45 my ($cmd, $timeout) = @_;
46
47 local $SIG{ALRM} = sub { die "render-still-running\n" };
48
49 alarm $timeout;
50 my $ok = eval { $cmd->render(); 1 };
51 my $err = $@;
52 alarm 0;
53
54 return 1 if $ok;
55
56 die $err unless $err eq "render-still-running\n";
57
58 return 0;
59 }
60
61 subtest eof_ends_the_loop => sub {
62 pipe(my $r, my $w) or die "Could not create pipe: $!";
63 $w->autoflush(1);
64
65 print $w event_line({assert => {pass => 1}});
66 close($w);
67
68 my $cmd = build_cmd($r);
69
70 ok(render_under_alarm($cmd, 10), "loop ended on EOF");
71 is($cmd->{+ASSERTS_SEEN}, 1, "processed the event before EOF");
72 };
73
74 subtest live_writer_does_not_end_the_loop => sub {
75 pipe(my $r, my $w) or die "Could not create pipe: $!";
76
77 # Without autoflush nothing is ever readable and the test proves nothing.
78 $w->autoflush(1);
79
80 print $w event_line({assert => {pass => 1}});
81
82 my $cmd = build_cmd($r);
83
84 # The writer stays open and the pipe goes empty. render() must still be
85 # looping when the alarm fires; returning would mean it mistook "nothing
86 # yet" for EOF and truncated a healthy run.
87 ok(!render_under_alarm($cmd, 2), "render() did not return while the writer was alive");
88 is($cmd->{+ASSERTS_SEEN}, 1, "processed the event that was available");
89
90 close($w);
91 };
92
93 subtest null_sentinel_ends_the_loop => sub {
94 pipe(my $r, my $w) or die "Could not create pipe: $!";
95 $w->autoflush(1);
96
97 # The collector and auditor end the stream with a literal JSON null. That
98 # path must keep working, with the writer still open.
99 print $w "null\n";
100
101 my $cmd = build_cmd($r);
102
103 ok(render_under_alarm($cmd, 10), "render() returned on the null sentinel");
104
105 close($w);
106 };
107
108 subtest partial_line_at_eof_is_reported => sub {
109 pipe(my $r, my $w) or die "Could not create pipe: $!";
110 $w->autoflush(1);
111
112 print $w event_line({assert => {pass => 1}});
113 print $w '{"facet_data":{"assert"'; # no newline, writer then goes away
114 close($w);
115
116 my $cmd = build_cmd($r);
117
118 my $stderr = "";
119 my $returned;
120 {
121 local *STDERR;
122 open(STDERR, '>', \$stderr) or die "Could not redirect STDERR: $!";
123 $returned = render_under_alarm($cmd, 10);
124 }
125
126 ok($returned, "render() returned instead of dying on the fragment");
127 like($stderr, qr/Incomplete event discarded/, "reported the incomplete event");
128 like($stderr, qr/\Q{"facet_data":{"assert"\E/, "included the fragment");
129 };
130
131 subtest oversized_fragment_is_truncated => sub {
132 pipe(my $r, my $w) or die "Could not create pipe: $!";
133 $w->autoflush(1);
134
135 print $w '{"facet_data":' . ('x' x 5000); # no newline
136 close($w);
137
138 my $cmd = build_cmd($r);
139
140 my $stderr = "";
141 my $returned;
142 {
143 local *STDERR;
144 open(STDERR, '>', \$stderr) or die "Could not redirect STDERR: $!";
145 $returned = render_under_alarm($cmd, 10);
146 }
147
148 ok($returned, "render() returned");
149 like($stderr, qr/\Q... (truncated)\E/, "bounded the reported fragment");
150 ok(length($stderr) < 500, "did not dump the whole fragment") or diag(length($stderr));
151 };
152
153 # A renderer that leaves a stale errno behind.
154 {
155
156 package Fake::Renderer::DirtyErrno;
157
158 sub new { return bless({}, shift) }
159
160 sub step { $! = Errno::EAGAIN(); return }
161
162 sub render_event { return }
163 }
164
165 subtest renderer_in_the_loop_still_reaches_eof => sub {
166 # The only coverage where render() actually drives a renderer. It also
167 # exercises a renderer leaving a stale errno behind; on this perl that
168 # cannot fail, because the value it leaves is the one PerlIO retries past.
169 pipe(my $r, my $w) or die "Could not create pipe: $!";
170 $w->autoflush(1);
171
172 print $w event_line({assert => {pass => 1}});
173 print $w '{"facet_data":{"assert"'; # partial, then EOF
174 close($w);
175
176 my $cmd = build_cmd($r, Fake::Renderer::DirtyErrno->new);
177
178 my $stderr = "";
179 my $returned;
180 {
181 local *STDERR;
182 open(STDERR, '>', \$stderr) or die "Could not redirect STDERR: $!";
183 $returned = render_under_alarm($cmd, 10);
184 }
185
186 ok($returned, "reached EOF despite a renderer leaving EAGAIN in errno");
187 is($cmd->{+ASSERTS_SEEN}, 1, "processed the complete event first");
188 };
189
190 done_testing;