Debian Codegraph @

t/65-seg_size.t

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 use warnings;
2 use strict;
3
4 use Config;
5 use IPC::Shareable;
6 use Test::More;
7
8 BEGIN {
9 if (! $ENV{CI_TESTING}) {
10 plan skip_all => "Not on a legit CI platform...";
11 }
12 if ($Config{ivsize} < 8) {
13 plan skip_all => "This test script can't be run on a perl < 64-bit";
14 }
15 }
16
17 warn "Segs Before: " . IPC::Shareable::ipcs() . "\n" if $ENV{PRINT_SEGS};
18
19 use constant BYTES => 2000000; # ~2MB
20
21 # limit
22 {
23 my $size_ok_limit = eval {
24 tie my $var, 'IPC::Shareable', {
25 create => 1,
26 size => 2_000_000_000,
27 destroy => 1
28 };
29 1;
30 };
31
32 is $size_ok_limit, undef, "size larger than MAX croaks ok";
33 like $@, qr/larger than max size/, "...and error is sane";
34
35 if ($ENV{IPC_MEM}) {
36 my $size_ok_no_limit = eval {
37 tie my $var, 'IPC::Shareable', {
38 limit => 0,
39 create => 1,
40 size => 2_000_000_000,
41 destroy => 1
42 };
43 1;
44 };
45
46 is $size_ok_no_limit, 1, "size larger than MAX succeeeds with limit=>0 ok";
47 }
48 else {
49 warn "IPC_MEM env var not set, skipping the exhaust memory test\n";
50 }
51 }
52
53 # beyond RAM limits
54 {
55 my $size_ok = eval {
56 tie my $var, 'IPC::Shareable', {
57 limit => 0,
58 size => 999999999999,
59 destroy => 1
60 };
61 1;
62 };
63
64 is $size_ok, undef, "We croak if size is greater than max RAM";
65
66 like $@, qr/Cannot allocate memory|Out of memory|Invalid argument/, "...and error is sane";
67 }
68
69 my $k = tie my $sv, 'IPC::Shareable', {
70 create => 1,
71 destroy => 1,
72 size => BYTES,
73 };
74
75 my $seg = $k->seg;
76
77 my $id = $seg->id;
78 my $size = $seg->size;
79
80 my $actual_size;
81
82 if ($^O eq 'linux') {
83 my $record = `ipcs -m -i $id`;
84 $actual_size = 0;
85
86 if ($record =~ /bytes=(\d+)/s) {
87 $actual_size = $1;
88 }
89 }
90 else {
91 $actual_size = 0;
92 }
93
94 is BYTES, $size, "size param is the same as the segment size";
95
96 # ipcs -i doesn't work on MacOS or FreeBSD, so skip it for now
97
98 TODO: {
99 local $TODO = 'Not yet working on FreeBSD or macOS';
100 };
101
102 # ...and only run it on Linux systems
103
104 if ($^O eq 'linux') {
105 is $size, $actual_size, "actual size in bytes ok if sending in custom size";
106 }
107
108 $k->clean_up_all;
109
110 IPC::Shareable::_end;
111 warn "Segs After: " . IPC::Shareable::ipcs() . "\n" if $ENV{PRINT_SEGS};
112
113 done_testing();