File: check.pl

package info (click to toggle)
libanyevent-memcached-perl 0.08-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 336 kB
  • ctags: 243
  • sloc: perl: 3,494; makefile: 7
file content (204 lines) | stat: -rw-r--r-- 6,507 bytes parent folder | download | duplicates (2)
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
use common::sense;
runtest {
	my ($host,$port,%args) = @_;
	my $cv;$cv = AE::cv;
	diag "testing $host:$port";
	require Test::NoWarnings;Test::NoWarnings->import;
	plan tests => 52+1;

	my $memd = AnyEvent::Memcached->new(
		servers   => [ "$host:$port" ],
		cv        => $cv,
		debug     => 0,
		%args,
		namespace => "AE::Memd::t/$$/" . (time() % 100) . "/",
	);

	isa_ok($memd, 'AnyEvent::Memcached');
	$cv->begin;
	$memd->set('cas2','val2',cb => sub { ok(shift,"set cas2 as val1") or diag "  Error: @_"; });
	$memd->set('cas1','val1',cb => sub {
		ok(shift,"set cas as val1") or diag "  Error: @_";
		$memd->gets('cas1',cb => sub {
			my $value = shift;
			if ($value) {
			ok $value, 'got result' or diag "  Error: @_";
			is ref $value,'ARRAY', 'retval is array';
			is $value->[1], 'val1', 'value correct';
			# Now, break the value
			$memd->set('cas1','val2',cb => sub {
				ok(shift,"set cas as val2") or diag "  Error: @_";
				$memd->cas('cas1', $value->[0], 'val3',cb => sub {
					ok(!shift,"try cas as val3");
					ok(!@_, 'cas have no errors') or diag "  Error: @_";
					$memd->gets('cas1',cb => sub {
						ok my $value = shift, 'gets again';
						$memd->cas('cas1', $value->[0], 'val4',cb => sub {
							ok(shift,"set cas as val4");
							ok(!@_, 'cas have no errors') or diag "  Error: @_";
							
							#Now, test 2 keys at once
							$memd->gets(['cas1','cas2'], cb => sub {
								ok my $values = shift, 'got gets* result' or diag "  Error: @_";
								is ref $values, 'HASH', 'retval is hash';
								ok exists $values->{cas1}, 'have cas1';
								ok exists $values->{cas2}, 'have cas2';
								is ref $values->{cas1}, 'ARRAY', 'value 1 correct';
								is ref $values->{cas2}, 'ARRAY', 'value 2 correct';
								$memd->cas('cas1', $values->{cas1}[0], 'val5',cb => sub {
									ok(shift,"set cas1 as val5");
									ok(!@_, 'cas1 have no errors') or diag "  Error: @_";
								});
								$memd->cas('cas2', $values->{cas2}[0], 'val5',cb => sub {
									ok(shift,"set cas2 as val5");
									ok(!@_, 'cas2 have no errors') or diag "  Error: @_";
								});
								
							});
							
						});
					});
				});
				
			});
			} else {
				my $error = shift;
				SKIP: {
					if ($error =~ /not enabled/) {
						skip "gets not enabled",19;
					} else {
						fail "gets failed";
						diag "$error";
						skip "gets failed",18;
					}
				}
			}
		});
	});
	$memd->set("key1", "val1", cb => sub {
		ok(shift,"set key1 as val1") or diag "  Error: @_";
		$memd->get("key1", cb => sub {
			is(shift, "val1", "get key1 is val1") or diag "  Error: @_";
			$memd->add("key1", "val-replace", cb => sub {
				ok(! shift, "add key1 properly failed");
				$memd->add("key2", "val2", cb => sub {
					ok(shift, "add key2 as val2");
					$memd->get("key2", cb => sub {
						is(shift, "val2", "get key2 is val2") or diag "@_";
						$memd->replace("key2", "val-replace", cb => sub {
							ok(shift, "replace key2 as val-replace");
							$memd->get("key2", cb => sub {
								is(shift, "val-replace", "get key2 is val-replace") or diag "@_";
								$memd->set( key4 => {ref => 1}, cb => sub {
									ok shift, 'set ref' or diag "@_";
									$memd->get(
										[qw(key2 key4)],
										cb => sub {
											ok(my $r = shift, 'get multi');
											is_deeply $r,
												{ qw(key2 val-replace key4 ), {ref => 1} },
												'get multi values';
										},
									);
								});
								
								$memd->rget('1','0', cb => sub {
									my ($r,$e) = @_;
									
									if (!$e) {
										$memd->set("key3", "val3", cb => sub {
											ok(shift,"set key3 as val3");
											$memd->rget('key2','key3', cb => sub { # +left, +right
												my $r = shift;
												is( $r->{ 'key2' }, 'val-replace', 'rget[].key2' );
												is( $r->{ 'key3' }, 'val3', 'rget[].key3' );
											});
											$memd->rget('key2','key3', '+right' => 0, cb => sub {
												my $r = shift;
												is( $r->{ 'key2' }, 'val-replace', 'rget[).key2' );
												ok(! exists $r->{ 'key3' }, '!rget[).key3' );
											});
											$memd->rget('key2','key3', '+left' => 0, cb => sub {
												my $r = shift;
												ok(! exists $r->{ 'key2' }, '!rget(].key2' );
												is( $r->{ 'key3' }, 'val3', 'rget(].key3' );
											});
											
											$memd->rget('key2','key3', rv => 'array', cb => sub { # +left, +right
												my $r = shift;
												is_deeply $r,
													[qw(key2 val-replace key3 val3)],
													'rget[] array';
											});
											$memd->rget('key2','key3', '+right' => 0, rv => 'array', cb => sub {
												my $r = shift;
												is_deeply $r,
													[qw(key2 val-replace)],
													'rget[) array';
											});
											$memd->rget('key2','key3', '+left' => 0, rv => 'array', cb => sub {
												my $r = shift;
												is_deeply $r,
													[qw(key3 val3)],
													'rget(] array';
											});
										});
									} else {
										like( $e, qr/rget not supported/, 'rget fails' );
										SKIP: { skip "Have no rget",6+3 }
									}
								});
								
							});
						});
					});
				});
				$memd->delete("key1", cb => sub {
					ok(shift, "delete key1");
					$memd->get("key1", cb => sub {
						ok(! shift, "get key1 properly failed");
					});
					
				});
			});
		});
	});
	$memd->replace("key-noexist", "bogus", cb => sub {
		ok(!shift , "replace key-noexist properly failed");
	});
	my $need;
	$memd->set("ikey", $need = 3, cb => sub {
		ok(shift,"set ikey as 3") or diag "  Error: @_";
		#$memd->incr(ikey => 1, noreply => 1) and warn("norply ok"), ++$need;
		$memd->incr(ikey => 1, cb => sub {
			++$need;
			my $igot = shift;
			is $igot, $need, 'incr ikey = '.$igot or diag "  Error: @_";
			$need = $igot-2;
			#$memd->decr(ikey => 2, noreply => 1);# or $need -= 2;
			$memd->decr(ikey => 2, cb => sub {
				my $dgot = shift;
				is $dgot, $need, 'decr ikey = '.$dgot or diag "  Error: @_";
				$memd->get('ikey', cb => sub {
					diag "get after incr/decr = ".shift;
				});
			});
		});
	});
	$memd->incadd(iakey => 42, cb => sub {
		is $_[0],42, 'incadd works as add';
		$memd->get(iakey => cb => sub {
			is $_[0],42, 'incadd works as add (get check)';
			$memd->incadd(iakey => 42, cb => sub {
				is $_[0], 42*2, 'incadd works as inc';
				$memd->get(iakey => cb => sub {
					is $_[0],42*2, 'incadd works as inc (get check)';
				});
			});
		});
	});
	$cv->end;
	$cv->recv;
	$memd->destroy();

};