File: extrefs.pm

package info (click to toggle)
libpoe-perl 2%3A1.3670-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,996 kB
  • ctags: 1,416
  • sloc: perl: 22,865; makefile: 9
file content (202 lines) | stat: -rw-r--r-- 5,442 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# vim: ts=2 sw=2 expandtab
use strict;

use lib qw(./mylib ../mylib);
use Test::More tests => 31;

sub POE::Kernel::ASSERT_DEFAULT () { 1 }

BEGIN {
  package POE::Kernel;
  use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'});
}

BEGIN { use_ok("POE") }

# Base reference count.
my $base_refcount = 0;

# Increment an extra reference count, and verify its value.

my $refcnt = $poe_kernel->_data_extref_inc($poe_kernel->ID, "tag-1");
is($refcnt, 1, "tag-1 incremented to 1");

$refcnt = $poe_kernel->_data_extref_inc($poe_kernel->ID, "tag-1");
is($refcnt, 2, "tag-1 incremented to 2");

# Baseline plus one reference: tag-1.  (No matter how many times you
# increment a single tag, it only counts as one session reference.
# This may change if the utility of the reference counts adding up
# outweighs the overhead of managing the session reference more.)

is(
  $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount + 1,
  "POE::Kernel properly counts tag-1 extra reference"
);

# Attempt to remove some strange tag.

eval { $poe_kernel->_data_extref_remove($poe_kernel->ID, "nonexistent") };
ok(
  $@ && $@ =~ /removing extref for nonexistent tag/,
  "can't remove nonexistent tag from a session"
);

is(
  $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount + 1,
  "POE::Kernel reference count unchanged"
);

# Remove it entirely, and verify that it's 1 again after incrementing
# again.

$poe_kernel->_data_extref_remove($poe_kernel->ID, "tag-1");
is(
  $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount + 0,
  "clear reset reference count to baseline"
);

$refcnt = $poe_kernel->_data_extref_inc($poe_kernel->ID, "tag-1");
is($refcnt, 1, "tag-1 count cleared/incremented to 1");
is(
  $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount + 1,
  "increment after clear"
);

# Set a second reference count, then verify that both are reset.

$refcnt = $poe_kernel->_data_extref_inc($poe_kernel->ID, "tag-2");
is($refcnt, 1, "tag-2 incremented to 1");

# Setting a second tag increments the master reference count.

is(
  $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount + 2,
  "POE::Kernel reference count incremented with new tag"
);

# Clear all the extra references for the session, and verify that the
# master reference count is back to the baseline.

$poe_kernel->_data_extref_clear_session($poe_kernel->ID);
is(
  $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount,
  "clearing all extrefs brings count to baseline"
);

eval { $poe_kernel->_data_extref_remove($poe_kernel->ID, "nonexistent") };
ok(
  $@ && $@ =~ /removing extref from session without any/,
  "can't remove tag from a session without any"
);

$refcnt = $poe_kernel->_data_extref_inc($poe_kernel->ID, "tag-1");
is($refcnt, 1, "tag-1 incremented back to 1");

$refcnt = $poe_kernel->_data_extref_inc($poe_kernel->ID, "tag-2");
is($refcnt, 1, "tag-2 incremented back to 1");

$refcnt = $poe_kernel->_data_extref_inc($poe_kernel->ID, "tag-2");
is($refcnt, 2, "tag-2 incremented back to 2");

# Only one session has an extra reference count.

is(
  $poe_kernel->_data_extref_count(), 1,
  "only one session has extra references"
);

# Extra references for the kernel should be two.  A nonexistent
# session should have none.

is(
  $poe_kernel->_data_extref_count_ses($poe_kernel->ID), 2,
  "POE::Kernel has two extra references"
);

is(
  $poe_kernel->_data_extref_count_ses("nothing"), 0,
  "nonexistent session has no extra references"
);

# What happens if decrementing an extra reference for a tag that
# doesn't exist?

eval { $poe_kernel->_data_extref_dec($poe_kernel->ID, "nonexistent") };
ok(
  $@ && $@ =~ /decrementing extref for nonexistent tag/,
  "can't decrement an extref if a session doesn't have it"
);

# Clear the references, and make sure the subsystem shuts down
# cleanly.

{ is(
    $poe_kernel->_data_extref_dec($poe_kernel->ID, "tag-1"), 0,
    "tag-1 decremented to 0"
  );

  is(
    $poe_kernel->_data_extref_count_ses($poe_kernel->ID), 1,
    "POE::Kernel has one extra reference"
  );

  is(
    $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount + 1,
    "POE::Kernel reference count decremented along with tag"
  );
}

{ is(
    $poe_kernel->_data_extref_dec($poe_kernel->ID, "tag-2"), 1,
    "tag-2 decremented to 1"
  );

  is(
    $poe_kernel->_data_extref_count_ses($poe_kernel->ID), 1,
    "POE::Kernel still has one extra reference"
  );

  is(
    $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount + 1,
    "POE::Kernel reference count not decremented yet"
  );
}

{ is(
    $poe_kernel->_data_extref_dec($poe_kernel->ID, "tag-2"), 0,
    "tag-2 decremented to 0"
  );

  is(
    $poe_kernel->_data_extref_count_ses($poe_kernel->ID), 0,
    "POE::Kernel has no extra references"
  );

  is(
    $poe_kernel->_data_ses_refcount($poe_kernel->ID), $base_refcount,
    "POE::Kernel reference count decremented again"
  );
}

# Catch some errors.

eval { $poe_kernel->_data_extref_dec($poe_kernel->ID, "nonexistent") };
ok(
  $@ && $@ =~ /decrementing extref for session without any/,
  "can't decrement an extref if a session doesn't have any"
);

# Clear the session again, to exercise some code that otherwise
# wouldn't be.

$poe_kernel->_data_extref_clear_session($poe_kernel->ID);

# Ensure the subsystem shuts down ok.

ok(
  $poe_kernel->_data_extref_finalize(),
  "POE::Resource::Extrefs finalized ok"
);

1;