| | 51 | |
|---|
| | 52 | my $set = sub { |
|---|
| | 53 | my ($key, $exp, $orig_flags, $orig_value) = @_; |
|---|
| | 54 | $mc->set($key, $exp, $orig_flags, $orig_value); |
|---|
| | 55 | $check->($key, $orig_flags, $orig_value); |
|---|
| | 56 | }; |
|---|
| | 57 | |
|---|
| | 58 | my $empty = sub { |
|---|
| | 59 | my $key = shift; |
|---|
| | 60 | my $rv =()= eval { $mc->get($key) }; |
|---|
| | 61 | is($rv, 0, "Didn't get a result from get"); |
|---|
| | 62 | ok($@->not_found, "We got a not found error when we expected one"); |
|---|
| | 63 | |
|---|
| | 64 | }; |
|---|
| | 65 | |
|---|
| | 66 | my $delete = sub { |
|---|
| | 67 | my ($key, $when) = @_; |
|---|
| | 68 | $mc->delete($key, $when); |
|---|
| | 69 | $empty->($key); |
|---|
| | 70 | }; |
|---|
| | 71 | |
|---|
| | 72 | $mc->flush if 0; |
|---|
| | 73 | |
|---|
| | 74 | { |
|---|
| | 75 | diag "Test Version"; |
|---|
| | 76 | my $v = $mc->version; |
|---|
| | 77 | ok(defined $v && length($v), "Proper version"); |
|---|
| | 78 | } |
|---|
| | 90 | $set->('x', 5, 19, "somevaluex"); |
|---|
| | 91 | $set->('y', 5, 17, "somevaluey"); |
|---|
| | 92 | $mc->flush; |
|---|
| | 93 | $empty->('x'); |
|---|
| | 94 | $empty->('y'); |
|---|
| | 95 | |
|---|
| | 96 | diag "Test increment"; |
|---|
| | 97 | $mc->flush; |
|---|
| | 98 | is($mc->incr("x"), 0, "First incr call is zero"); |
|---|
| | 99 | is($mc->incr("x"), 1, "Second incr call is one"); |
|---|
| | 100 | is($mc->incr("x", 211), 212, "Adding 211 gives you 212"); |
|---|
| | 101 | is($mc->incr("x", 2**33), 8589934804, "Blast the 32bit border"); |
|---|
| | 102 | |
|---|
| 88 | | $set->('x', 5, 19, "somevaluex"); |
|---|
| 89 | | $set->('y', 5, 17, "somevaluey"); |
|---|
| 90 | | $mc->flush; |
|---|
| 91 | | $empty->('x'); |
|---|
| 92 | | $empty->('y'); |
|---|
| 93 | | } |
|---|
| 94 | | |
|---|
| 95 | | diag "Test increment"; |
|---|
| 96 | | { |
|---|
| 97 | | $mc->flush; |
|---|
| 98 | | is($mc->incr("x"), 0, "First incr call is zero"); |
|---|
| 99 | | is($mc->incr("x"), 1, "Second incr call is one"); |
|---|
| 100 | | is($mc->incr("x", 211), 212, "Adding 211 gives you 212"); |
|---|
| 101 | | is($mc->incr("x", 2**33), 858993480, "Blast the 32bit border"); |
|---|
| 102 | | } |
|---|
| 103 | | |
|---|
| 104 | | diag "Reservation delete"; |
|---|
| 105 | | { |
|---|
| | 104 | diag "Reservation delete"; |
|---|
| | 114 | { |
|---|
| | 115 | diag "Add"; |
|---|
| | 116 | $empty->('i'); |
|---|
| | 117 | $mc->add('i', 5, 19, "ex"); |
|---|
| | 118 | $check->('i', 19, "ex"); |
|---|
| | 119 | |
|---|
| | 120 | my $rv =()= eval { $mc->add('i', 5, 19, "ex2") }; |
|---|
| | 121 | is($rv, 0, "Add didn't return anything"); |
|---|
| | 122 | ok($@->exists, "Expected exists error received"); |
|---|
| | 123 | |
|---|
| | 124 | $check->('i', 19, "ex"); |
|---|
| | 125 | } |
|---|
| | 126 | |
|---|
| | 127 | { |
|---|
| | 128 | diag "Replace"; |
|---|
| | 129 | $empty->('j'); |
|---|
| | 130 | |
|---|
| | 131 | my $rv =()= eval { $mc->replace('j', 5, 19, "ex") }; |
|---|
| | 132 | is($rv, 0, "Replace didn't return anything"); |
|---|
| | 133 | ok($@->not_found, "Expected not_found error received"); |
|---|
| | 134 | |
|---|
| | 135 | $empty->('j'); |
|---|
| | 136 | |
|---|
| | 137 | $mc->add('j', 5, 14, "ex2"); |
|---|
| | 138 | $check->('j', 14, "ex2"); |
|---|
| | 139 | |
|---|
| | 140 | $mc->replace('j', 5, 24, "ex3"); |
|---|
| | 141 | $check->('j', 24, "ex3"); |
|---|
| | 142 | } |
|---|
| | 143 | |
|---|
| 116 | | |
|---|
| 117 | | def testAdd(self): |
|---|
| 118 | | """Test add functionality.""" |
|---|
| 119 | | self.assertNotExists("x") |
|---|
| 120 | | self.mc.add("x", 5, 19, "ex") |
|---|
| 121 | | self.assertEquals((19, "ex"), self.mc.get("x")) |
|---|
| 122 | | try: |
|---|
| 123 | | self.mc.add("x", 5, 19, "ex2") |
|---|
| 124 | | self.fail("Expected failure to add existing key") |
|---|
| 125 | | except MemcachedError, e: |
|---|
| 126 | | self.assertEquals(memcacheConstants.ERR_EXISTS, e.status) |
|---|
| 127 | | self.assertEquals((19, "ex"), self.mc.get("x")) |
|---|
| 128 | | |
|---|
| 129 | | def testReplace(self): |
|---|
| 130 | | """Test replace functionality.""" |
|---|
| 131 | | self.assertNotExists("x") |
|---|
| 132 | | try: |
|---|
| 133 | | self.mc.replace("x", 5, 19, "ex") |
|---|
| 134 | | self.fail("Expected failure to replace missing key") |
|---|
| 135 | | except MemcachedError, e: |
|---|
| 136 | | self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) |
|---|
| 137 | | self.mc.add("x", 5, 19, "ex") |
|---|
| 138 | | self.assertEquals((19, "ex"), self.mc.get("x")) |
|---|
| 139 | | self.mc.replace("x", 5, 19, "ex2") |
|---|
| 140 | | self.assertEquals((19, "ex2"), self.mc.get("x")) |
|---|
| 141 | | |
|---|
| 336 | | return $self->_doCmd($cmd, $key, '', pack(::INCRDECR_PKT_FMT, $amt >> 32, 0xFFFFFFFF & $amt, $init >> 32, 0xFFFFFFFF & $init, $exp)); |
|---|
| | 339 | |
|---|
| | 340 | my $amt_hi = int($amt / 2 ** 32); |
|---|
| | 341 | my $amt_lo = int($amt % 2 ** 32); |
|---|
| | 342 | |
|---|
| | 343 | my $init_hi = int($init / 2 ** 32); |
|---|
| | 344 | my $init_lo = int($init % 2 ** 32); |
|---|
| | 345 | |
|---|
| | 346 | return $self->_doCmd($cmd, $key, '', pack(::INCRDECR_PKT_FMT, $amt_hi, $amt_lo, $init_hi, $init_lo, $exp)); |
|---|
| | 380 | sub gets { |
|---|
| | 381 | my $self = shift; |
|---|
| | 382 | my $key = shift; |
|---|
| | 383 | |
|---|
| | 384 | my $data = $self->_doCmd(::CMD_GETS, $key, ''); |
|---|
| | 385 | my $header = substr $data, 0, 12, ''; |
|---|
| | 386 | my ($flags, $ident_hi, $ident_lo) = unpack "NNN", $data; |
|---|
| | 387 | my $ident = ($ident_hi * 2 ** 32) + $ident_lo; |
|---|
| | 388 | |
|---|
| | 389 | return $flags, $ident, $data; |
|---|
| | 390 | } |
|---|
| | 391 | |
|---|