File: 05_threads.t

package info (click to toggle)
libhash-fieldhash-perl 0.15-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: perl: 1,249; ansic: 207; makefile: 10
file content (91 lines) | stat: -rw-r--r-- 1,274 bytes parent folder | download | duplicates (7)
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
#!perl -w

use strict;
use constant HAS_THREADS => eval{ require threads };
use Test::More;

BEGIN{
	if(HAS_THREADS){
		plan tests => 15*3;
	}
	else{
		plan skip_all => 'require threads';
	}
}
use threads;

#use Hash::Util::FieldHash::Compat qw(:all);
use Hash::FieldHash qw(:all);

for(1 .. 3){
	fieldhashes \my(%a, %b);

	{
		my $x = {};
		my $y = {};

		$a{$x} = 'a-x';
		$a{$y} = 'a-y';
		$b{$x} = 'b-x';
		$b{$y} = 'b-y';

		my $thr = async {
			is $a{$x}, 'a-x';
			is $a{$y}, 'a-y';
			is $b{$x}, 'b-x';
			is $b{$y}, 'b-y';

			my $thr1 = async{
				is $a{$x}, 'a-x';

				threads->yield();
				$a{$x} = 3.14;

				threads->yield();
				is $a{$x}, 3.14;
			};

			my $thr2 = async{
				fieldhash my %c;
				my $z = [];

				$c{$x} = $a{$x};
				$c{$z} = 'c-z';

				$a{$x}++;
				$b{$x}++;

				is_deeply [sort values %c], [sort qw(a-x c-z)];
			};

			threads->yield();

			is $a{$x}, 'a-x';

			my $z = {};

			threads->yield();

			is $a{$x}, 'a-x';

			$a{$z} = 42;
			is $a{$z}, 42;

			$thr1->join();
			$thr2->join();
		};

		ok $thr, sprintf 'count=%d, tid=%d', $_, $thr->tid;
		$thr->yield;

		is_deeply [sort values %a], [sort 'a-x', 'a-y'];

		$thr->join;

		is_deeply [sort values %a], [sort 'a-x', 'a-y'];
	}

	is_deeply \%a, {};
	is_deeply \%b, {};
}