[go: up one dir, main page]

File: shared.t

package info (click to toggle)
rex 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,800 kB
  • sloc: perl: 30,667; xml: 264; makefile: 8
file content (79 lines) | stat: -rw-r--r-- 2,093 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;

BEGIN {
  use Test::More tests => 18;
  use Test::Deep;
  use Time::HiRes;
  use Rex::Shared::Var;
  share(qw($scalar @array %hash));
}

$scalar = "scalar";
is( $scalar, "scalar", "scalar test" );

is( shift @array, undef, "shift from empty shared array" );
is( pop @array,   undef, "pop from empty shared array" );

@array = qw(one two three four);
is( join( "-", @array ), "one-two-three-four", "array test" );

push( @array, "five" );
is( $array[-1], "five", "array push" );

is( shift @array, "one", "shift from shared array" );
is_deeply( \@array, [qw(two three four five)], "shared array after shift" );

is( pop @array, "five", "pop from shared array" );
is_deeply( \@array, [qw(two three four)], "shared array after pop" );

unshift( @array, "six" );
is( $array[0], "six", "unshift to shared array" );

push( @array, qw(seven eight) );
is_deeply( [ @array[ -2, -1 ] ],
  [qw(seven eight)], "push list to shared array" );

unshift( @array, qw(nine ten) );
is_deeply( [ @array[ 0, 1 ] ], [qw(nine ten)], "unshift list to shared array" );

%hash = (
  name     => "joe",
  surename => "doe",
  multi    => {
    key1 => "foo",
    arr1 => [qw/bar baz/],
  }
);

is( $hash{name},               "joe", "hash test, key 1" );
is( $hash{surename},           "doe", "hash test, key 2" );
is( $hash{multi}->{key1},      "foo", "multidimension, key1" );
is( $hash{multi}->{arr1}->[0], "bar", "multidimension, arr1 - key0" );
is( $hash{multi}->{arr1}->[1], "baz", "multidimension, arr1 - key1" );

{
  # Sleeping in a test is not ideal.  But can't replace Time::HiRes::usleep()
  # with POSIX::pause() because kill doesn't send signals on win32. See
  # 'perldoc -f kill', 'perldoc perlport' and
  # https://github.com/RexOps/Rex/pull/774

  @array = (0);
  my @pids;

  for my $i ( 0 .. 5 ) {
    my $pid = fork();
    if ( $pid == 0 ) {

      # child
      Time::HiRes::usleep 100_000; # .1 seconds
      push @array, 1;
      exit 0;
    }
    push @pids, $pid;
  }

  waitpid $_, 0 for @pids;

  cmp_deeply \@array, [qw/0 1 1 1 1 1 1/], 'race condition avoided';
}