File: concurrent.t

package info (click to toggle)
libhash-sharedmem-perl 0.005-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 600 kB
  • sloc: perl: 463; makefile: 3
file content (170 lines) | stat: -r--r--r-- 4,314 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use warnings;
use strict;

use File::Temp 0.22 qw(tempdir);
use Test::Builder 0.03 ();
use Test::More 0.40 tests => 145;

BEGIN { use_ok "Hash::SharedMem", qw(
	is_shash shash_open
	shash_exists shash_get shash_set shash_gset shash_cset
	shash_count
); }

my $tmpdir = tempdir(CLEANUP => 1);
my @sh;
$sh[0] = shash_open("$tmpdir/t0", "rwc");
ok $sh[0];
ok is_shash($sh[0]);
$sh[1] = shash_open("$tmpdir/t0", "rwc");
ok $sh[1];
ok is_shash($sh[1]);
$sh[2] = shash_open("$tmpdir/t0", "rw");
ok $sh[2];
ok is_shash($sh[2]);
$sh[3] = shash_open("$tmpdir/t0", "r");
ok $sh[3];
ok is_shash($sh[3]);

is shash_get($_, "a"), undef foreach @sh;
is shash_get($_, "b"), undef foreach @sh;
is shash_get($_, "c"), undef foreach @sh;
is shash_get($_, "d"), undef foreach @sh;
shash_set($sh[0], "a", "aa");
is shash_get($_, "a"), "aa" foreach @sh;
is shash_get($_, "b"), undef foreach @sh;
is shash_get($_, "c"), undef foreach @sh;
is shash_get($_, "d"), undef foreach @sh;
shash_set($sh[1], "b", "bb");
is shash_get($_, "a"), "aa" foreach @sh;
is shash_get($_, "b"), "bb" foreach @sh;
is shash_get($_, "c"), undef foreach @sh;
is shash_get($_, "d"), undef foreach @sh;
shash_set($sh[2], "c", "cc");
is shash_get($_, "a"), "aa" foreach @sh;
is shash_get($_, "b"), "bb" foreach @sh;
is shash_get($_, "c"), "cc" foreach @sh;
is shash_get($_, "d"), undef foreach @sh;
is shash_gset($sh[0], "a", "xx"), "aa";
is shash_get($_, "a"), "xx" foreach @sh;
is shash_get($_, "b"), "bb" foreach @sh;
is shash_get($_, "c"), "cc" foreach @sh;
is shash_get($_, "d"), undef foreach @sh;
is shash_gset($sh[1], "b", "yy"), "bb";
is shash_get($_, "a"), "xx" foreach @sh;
is shash_get($_, "b"), "yy" foreach @sh;
is shash_get($_, "c"), "cc" foreach @sh;
is shash_get($_, "d"), undef foreach @sh;
ok !shash_cset($sh[2], "c", "pp", "qq");
is shash_get($_, "a"), "xx" foreach @sh;
is shash_get($_, "b"), "yy" foreach @sh;
is shash_get($_, "c"), "cc" foreach @sh;
is shash_get($_, "d"), undef foreach @sh;
ok shash_cset($sh[2], "c", "cc", "zz");
is shash_get($_, "a"), "xx" foreach @sh;
is shash_get($_, "b"), "yy" foreach @sh;
is shash_get($_, "c"), "zz" foreach @sh;
is shash_get($_, "d"), undef foreach @sh;

@sh = ();
my($rp0, $wp0, $rp1, $wp1, $pid);
alarm 0;
$SIG{ALRM} = "DEFAULT";

pipe($rp0, $wp0) or die "pipe: $!";
pipe($rp1, $wp1) or die "pipe: $!";
alarm 1000;
$pid = fork();
defined $pid or die "fork: $!";
if($pid == 0) {
	Test::More->builder->no_ending(1);
	$File::Temp::KEEP_ALL = 1;
	close $wp0;
	close $rp1;
	my $sh = shash_open("$tmpdir/t0", "rw");
	close $wp1;
	scalar <$rp0>;
	my $x = 5;
	for(my $j = 0; $j != 50000; $j++) {
		$x = ($x*21+7) % 100000;
		shash_set($sh, sprintf("%05dx", $x), "a$x");
	}
	exit 0;
} else {
	close $rp0;
	close $wp1;
	my $sh = shash_open("$tmpdir/t0", "rw");
	close $wp0;
	scalar <$rp1>;
	my $y = 5;
	for(my $j = 0; $j != 50000; $j++) {
		$y = ($y*61+19) % 100000;
		shash_set($sh, sprintf("%05dy", $y), "b$y");
	}
	close $rp1;
	waitpid $pid, 0;
}
alarm 0;
{
	my %ph;
	my $x = 5;
	my $y = 5;
	for(my $j = 0; $j != 50000; $j++) {
		$x = ($x*21+7) % 100000;
		$y = ($y*61+19) % 100000;
		$ph{sprintf("%05dx", $x)} = "a$x";
		$ph{sprintf("%05dy", $y)} = "b$y";
	}
	my $sh = shash_open("$tmpdir/t0", "r");
	is shash_count($sh), 100003;
	is_deeply +{ map {
		(shash_exists($sh, $_) ? ($_ => shash_get($sh, $_)) : ())
	} map { $_."x", $_."y" } "00000".."99999" }, \%ph;
}

shash_set(shash_open("$tmpdir/t0", "rw"), "k", 0);
pipe($rp0, $wp0) or die "pipe: $!";
pipe($rp1, $wp1) or die "pipe: $!";
alarm 1000;
$pid = fork();
defined $pid or die "fork: $!";
if($pid == 0) {
	Test::More->builder->no_ending(1);
	$File::Temp::KEEP_ALL = 1;
	close $wp0;
	close $rp1;
	my $sh = shash_open("$tmpdir/t0", "rw");
	close $wp1;
	scalar <$rp0>;
	for(my $i = 0; $i != 100000; $i++) {
		my($ov, $nv);
		do {
			$ov = shash_get($sh, "k");
			$nv = $ov + 1;
		} until shash_cset($sh, "k", $ov, $nv);
	}
	exit 0;
} else {
	close $rp0;
	close $wp1;
	my $sh = shash_open("$tmpdir/t0", "rw");
	close $wp0;
	scalar <$rp1>;
	for(my $i = 0; $i != 100000; $i++) {
		my($ov, $nv);
		do {
			$ov = shash_get($sh, "k");
			$nv = $ov + 1;
		} until shash_cset($sh, "k", $ov, $nv);
	}
	close $rp1;
	waitpid $pid, 0;
}
alarm 0;
{
	my $sh = shash_open("$tmpdir/t0", "r");
	is shash_count($sh), 100004;
	is shash_get($sh, "k"), 200000;
}

1;