File: 09_session.t

package info (click to toggle)
libmojo-ioloop-readwriteprocess-perl 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 540 kB
  • sloc: perl: 4,655; sh: 101; makefile: 2
file content (109 lines) | stat: -rw-r--r-- 2,510 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl

use warnings;
use strict;
use Test::More;
use POSIX;
use FindBin;
use Mojo::File qw(tempfile path);
use lib ("$FindBin::Bin/lib", "../lib", "lib");

use Mojo::IOLoop::ReadWriteProcess              qw(process);
use Mojo::IOLoop::ReadWriteProcess::Test::Utils qw(attempt);
use Mojo::IOLoop;

use Mojo::IOLoop::ReadWriteProcess::Session qw(session);

subtest register => sub {
  my $s = session;
  my $p = process(sub { });
  $s->register(1 => $p);

  is_deeply ${$s->process_table()->{1}}, $p, 'Equal' or die diag explain $s;

  ${$s->process_table()->{1}}->{foo} = 'bar';

  is $p->{foo}, 'bar';

  session->resolve(1)->{foo} = 'kaboom';

  is $p->{foo}, 'kaboom';
};

subtest unregister => sub {
  session->clean();

  my $p = process(sub { });
  session->register(1 => $p);

  is_deeply ${session->process_table()->{1}}, $p, 'Equal'
    or die diag explain session();

  session->unregister(1);
  is session->all()->size, 0;
  is session->resolve(1),  undef;

  session->register(1 => $p);
  is session->all()->size, 1;

  session->clean();
  is session->all()->size, 0;
};

subtest disable => sub {
  local $SIG{CHLD} = 'DEFAULT';

  session->enable();
  is session->handler, 'DEFAULT', 'previous handler saved';

  isnt $SIG{CHLD}, 'DEFAULT', 'Handler has changed';
  session->disable();
  is $SIG{CHLD}, 'DEFAULT', 'handler restored';
};

subtest reset => sub {
  session->reset;

  session->register(1 => process(sub { }));
  session->register(2 => process(sub { }));
  session->register(3 => process(sub { }));
  session->orphans->{5} = 1;

  is session->all->size, 4, 'There are 4 processes';
  session->reset();

  is session->all->size, 0, 'Reset cleaned up processes';

  session->on(foo => sub { 'bar' });

  is((values %{session->{events}}), 1, '1 event is present');
  session->reset();
  is((values %{session->{events}}), 0, '0 events are present');

};

subtest protect => sub {

  my $handler;
  session->on(protect => sub { shift; $handler = pop @{shift()} });
  session->protect(sub { });
  is $handler, SIGCHLD;

  Mojo::IOLoop::ReadWriteProcess::Session->new->protect(sub { });
  is $handler, SIGCHLD;

  Mojo::IOLoop::ReadWriteProcess::Session::protect(sub { });
  is $handler, SIGCHLD;

  session->protect(sub { } => SIGTERM);
  is $handler, SIGTERM;

  Mojo::IOLoop::ReadWriteProcess::Session->new->protect(sub { } => SIGTERM);
  is $handler, SIGTERM;

  Mojo::IOLoop::ReadWriteProcess::Session::protect(sub { } => SIGTERM);
  is $handler, SIGTERM;

};

done_testing();