File: 020grab.t

package info (click to toggle)
libcache-memcached-managed-perl 0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 264 kB
  • sloc: perl: 617; makefile: 2
file content (285 lines) | stat: -rw-r--r-- 7,088 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

# Warn for possible excessive timing when on OS X
diag( <<"DIAG" ) if $^O eq 'darwin';

This test may take very long on OS X (some 10 hours) if you use an older
version of libevent and/or memcached.  Please upgrade to the latest libevent
if this test is taking more than a few minutes.
DIAG


use lib '.';
# Make sure we have a version for the subroutine based checks
$Foo::VERSION = 'Foo::VERSION';

# Set up test and strictness
use Test::More tests => 36018;
use strict;
use warnings;

# Load modules that we need
use List::Util qw(shuffle);

# Add stopping code
my $cache;
END {
    diag( "\nStopped memcached server(s)" )
      if $cache and ok( $cache->stop, "Check if all servers have stopped" );
} #END

# Make sure we have all the support routines
require 'testlib';
my $class = 'Cache::Memcached::Managed';
use_ok( $class );

# Obtain port and create config
my $port = anyport();
ok( $port,"Check whether we have a port to work on" );
my $config = "127.0.0.1:$port";

# Create a cache object
my $memcached_class = $ENV{CACHE_MEMCACHED} || 'Cache::Memcached';
$cache = $class->new(
  data            => $config,
  memcached_class => $memcached_class,
);
isa_ok( $cache,$class,"Check whether object ok" );

# Start the server, skip further tests if failed
SKIP: {
skip( "Memcached server not started",36014 ) if !$cache->start;
sleep 2; # let the server warm up
diag("\nStarted memcached server");

# Set the number of items to check
my @item = (1,2,255,256,257,511,512,513,1023,1024,1025,4095,4096,4097);

# Set them all in random order
Foo::set($_) foreach shuffle @item;

# Check them all in random order
Foo::check($_) foreach shuffle @item;

# Obtain final stats
my $got = $cache->stats->{$config};

# Remove stuff that we cannot check reliably
delete @$got{ qw(
 bytes_read
 bytes_written
 connection_structures
 curr_connections
 limit_maxbytes
 pid
 pointer_size
 rusage_user
 rusage_system
 time
 total_connections
 uptime 
 version
) };

# Set up the expected stats for the rest
my $expected = {
 bytes       => 0,
 cmd_get     => 88764,
 cmd_set     => 35936,
 curr_items  => 0,
 get_hits    => 88722,
 get_misses  => 42,
 total_items => 35968,
};

# Check if it is what we expected
TODO: {
local $TODO = 'Need to look up changes in memcached for different versions';
diag( Data::Dumper::Dumper( $got, $expected ) ) if
  !is_deeply( $got,$expected,
    "Check if final stats with one server correct" );
} #TODO

# Stop the single memcached server setup
ok( $cache->stop, "Check if single server has stopped" );
diag("\nStopped memcached server");

# Obtain ports and create config
my @port = map { anyport() } 0 .. 1;
ok( $port[$_], "Check whether we have a port to work on for $_" )
  foreach 0 .. 1;
my @config = map { "127.0.0.1:$_" } @port;

# Create a cache object
$cache = $class->new(
  data            => $config[1],
  directory       => $config[0],
  memcached_class => $memcached_class,
);
isa_ok( $cache, $class, "Check whether object ok" );

# Start the server, give it time to warm up
diag( "\nStarted memcached servers" )
  if ok( $cache->start, "Check if memcached servers started" );
sleep 2;

# Set them all in random order
Foo::set($_) foreach shuffle @item;

# Check them all in random order
Foo::check($_) foreach shuffle @item;

# Obtain final stats for directory server
my $stats = $cache->stats;

# Remove stuff that we cannot check reliably
$got = $stats->{$config[0]};
delete @$got{ qw(
 bytes_read
 bytes_written
 connection_structures
 curr_connections
 limit_maxbytes
 pid
 pointer_size
 rusage_user
 rusage_system
 time
 total_connections
 uptime 
 version
) };

# Set up the expected stats for the rest
$expected = {
 bytes       => 0,
 cmd_get     => 53393,
 cmd_set     => 17989,
 curr_items  => 0,
 get_hits    => 53351,
 get_misses  => 42,
 total_items => 18021,
};

# Check if it is what we expected
TODO: {
local $TODO = 'Need to look up changes in memcached for different versions';
diag( Data::Dumper::Dumper( $got,$expected ) ) if
  !is_deeply( $got,$expected,
    "Check if final stats with two servers correct" );
} #TODO

# Obtain final stats for data server
$got = $stats->{ $config[1] };

# Remove stuff that we cannot check reliably
delete @$got{ qw(
 bytes_read
 bytes_written
 connection_structures
 curr_connections
 limit_maxbytes
 pid
 pointer_size
 rusage_user
 rusage_system
 time
 total_connections
 uptime 
 version
) };

# Set up the expected stats for the rest
$expected = {
 bytes       => 0,
 cmd_get     => 35371,
 cmd_set     => 17947,
 curr_items  => 0,
 get_hits    => 35371,
 get_misses  => 0,
 total_items => 17947,
};

# Check if it is what we expected
TODO: {
local $TODO = 'Need to look up changes in memcached for different versions';
diag( Data::Dumper::Dumper( $got,$expected ) ) if
  !is_deeply( $got, $expected,
  "Check if final stats with two servers correct" );
}   #TODO

} #SKIP

#---------------------------------------------------------------------
# Foo::set
#
# Set information for group setting check
#
#  IN: 1 number of items

sub Foo::set {
    my ($items) = @_;

    # Set up items and group name
    my $group = "group$items";

    # Fill the group
    foreach ( shuffle 1 .. $items ) {
        ok( $cache->set(
         key   => "::$items",
         id    => $_,
         value => $items - $_ + 1,
         group => $group ), "Check if group$_ set ok for $_" );
    }
} #Foo::set

#---------------------------------------------------------------------
# Foo::check
#
# Check group information
#
#  IN: 1 number of items

sub Foo::check {
    my ($items) = @_;

    # Set up items and group name
    my $group = "group$items";
    my $key   = "Foo::$items";

    # Obtain the group key and associated IDs
    my $got = $cache->group( group => $group );
    my $expected = { $key => [ sort 1 .. $items ] }; # need alpha sorting
    diag( Data::Dumper::Dumper( $got, $expected ) ) if
      !is_deeply( $got, $expected,
        "Check if group key and IDs correct for $_" );

    # Fetch the group and data
    $got = $cache->get_group( group => $group );
    $expected = { $key => {
      $Foo::VERSION => { map { $_ => ( $items - $_ + 1 ) } 1..$items }
    } };
    diag( Data::Dumper::Dumper( $got, $expected ) ) if
      !is_deeply( $got,$expected,
        "Check if fetch group and data structure correct for $_" );

    my $values = $expected->{$key}->{$Foo::VERSION};
    foreach ( shuffle 1 .. 20 ) {
        ok( $cache->set(
         key   => $key,
         id    => $_,
         value => ($values->{$_} = $_),
         group => $group ), "Check if group$_ override ok for $_" );
    }

    # Grab the group and data
    $got = $cache->grab_group( group => $group );
    diag( Data::Dumper::Dumper( $got, $expected ) ) if
      !is_deeply( $got,$expected,
        "Check if grab group and data structure correct for $_" );

    # Grab the now empty group and data
    $got = $cache->grab_group( group => $group );
    diag( Data::Dumper::Dumper( $got, {} ) ) if
      !is_deeply( $got, {},
        "Check if second grab group and data structure fails for $_" );
} #Foo::check