c92125a90789fababfdd7e2fb4d1b8fbf66d8274
[awesomized/libmemcached] / memcached / t / expirations.t
1 #!/usr/bin/perl
2
3 use strict;
4 use Test::More tests => 15;
5 use FindBin qw($Bin);
6 use lib "$Bin/lib";
7 use MemcachedTest;
8
9 my $server = new_memcached();
10 my $sock = $server->sock;
11 my $expire;
12
13 sub wait_for_early_second {
14 my $have_hires = eval "use Time::HiRes (); 1";
15 if ($have_hires) {
16 my $tsh = Time::HiRes::time();
17 my $ts = int($tsh);
18 return if ($tsh - $ts) < 0.5;
19 }
20
21 my $ts = int(time());
22 while (1) {
23 my $t = int(time());
24 return if $t != $ts;
25 select undef, undef, undef, 0.10; # 1/10th of a second sleeps until time changes.
26 }
27 }
28
29 wait_for_early_second();
30
31 print $sock "set foo 0 1 6\r\nfooval\r\n";
32 is(scalar <$sock>, "STORED\r\n", "stored foo");
33
34 mem_get_is($sock, "foo", "fooval");
35 sleep(1.5);
36 mem_get_is($sock, "foo", undef);
37
38 $expire = time() - 1;
39 print $sock "set foo 0 $expire 6\r\nfooval\r\n";
40 is(scalar <$sock>, "STORED\r\n", "stored foo");
41 mem_get_is($sock, "foo", undef, "already expired");
42
43 $expire = time() + 1;
44 print $sock "set foo 0 $expire 6\r\nfoov+1\r\n";
45 is(scalar <$sock>, "STORED\r\n", "stored foo");
46 mem_get_is($sock, "foo", "foov+1");
47 sleep(2.2);
48 mem_get_is($sock, "foo", undef, "now expired");
49
50 $expire = time() - 20;
51 print $sock "set boo 0 $expire 6\r\nbooval\r\n";
52 is(scalar <$sock>, "STORED\r\n", "stored boo");
53 mem_get_is($sock, "boo", undef, "now expired");
54
55 print $sock "add add 0 2 6\r\naddval\r\n";
56 is(scalar <$sock>, "STORED\r\n", "stored add");
57 mem_get_is($sock, "add", "addval");
58 # second add fails
59 print $sock "add add 0 2 7\r\naddval2\r\n";
60 is(scalar <$sock>, "NOT_STORED\r\n", "add failure");
61 sleep(2.3);
62 print $sock "add add 0 2 7\r\naddval3\r\n";
63 is(scalar <$sock>, "STORED\r\n", "stored add again");
64 mem_get_is($sock, "add", "addval3");