Changeset 678

Show
Ignore:
Timestamp:
12/13/07 02:46:03 (1 year ago)
Author:
hachi
Message:

More of the tests working... 64bit counters isn't being tested properly yet.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/binary/server/t/binary.t

    r677 r678  
    99 
    1010# Command constants 
    11 use constant CMD_GET => 0; 
    12 use constant CMD_SET => 1; 
     11use constant CMD_GET     => 0; 
     12use constant CMD_SET     => 1; 
    1313# CMD_ADD = 2 
    1414# CMD_REPLACE = 3 
    15 use constant CMD_DELETE => 4; 
    16 # CMD_INCR = 5 
     15use constant CMD_DELETE => 4; 
     16use constant CMD_INCR    => 5; 
    1717# CMD_QUIT = 6 
    18 use constant CMD_FLUSH => 7; 
     18use constant CMD_FLUSH   => 7; 
    1919# CMD_GETQ = 8 
    20 # CMD_NOOP = 9 
     20use constant CMD_NOOP    => 9; 
    2121use constant CMD_VERSION => 10; 
    2222# 
     
    3333# 
    3434# amount, initial value, expiration 
    35 # INCRDECR_PKT_FMT=">qQi" 
     35use constant INCRDECR_PKT_FMT => "xxxxNxxxxNN"; 
    3636# 
    3737use constant REQ_MAGIC_BYTE => 0x0f; 
     
    4343# 
    4444# 
    45 #ERR_UNKNOWN_CMD = 0x81 
    46 #ERR_NOT_FOUND = 0x1 
    47 #ERR_EXISTS = 0x2 
    4845 
    4946my $mc = MC::Client->new; 
     
    6461}; 
    6562 
     63diag "Noop"; 
     64$mc->noop; 
     65         
    6666diag "Simple set/get"; 
    6767$set->('x', 5, 19, "somevalue"); 
    6868 
    6969my $delete = sub { 
    70         my ($key) = @_; 
    71         $mc->delete($key); 
    72         my $rv =()= $mc->get($key); 
    73         is($rv, 0, "Empty array from get means nothing stored here"); 
     70        my ($key, $when) = @_; 
     71        $mc->delete($key, $when); 
     72        my $rv =()= eval { $mc->get($key) }; 
     73        is($rv, 0, "Didn't get a result from get"); 
     74        ok($@->not_found, "We got a not found error when we expected one"); 
    7475}; 
    7576 
     
    7778$delete->('x'); 
    7879 
     80diag "Test increment"; 
     81{ 
     82        $mc->flush; 
     83        is($mc->incr("x"), 0, "First incr call is zero"); 
     84        is($mc->incr("x"), 1, "Second incr call is one"); 
     85        is($mc->incr("x", 211), 212, "Adding 211 gives you 212"); 
     86        is($mc->incr("x", 2**33), 858993480, "Blast the 32bit border"); 
     87} 
     88 
     89diag "Reservation delete"; 
     90{ 
     91        $set->('y', 5, 19, "someothervalue"); 
     92        $delete->('y', 1); 
     93        $mc->add('y', 5, 19, "yetanothervalue"); 
     94        sleep 2; 
     95        $mc->add('y', 5, 19, "wibblevalue"); 
     96} 
     97 
     98 
    7999<<EOT; 
    80     def testReservedDelete(self): 
    81         """Test a delete with a reservation timestamp.""" 
    82         self.mc.set("x", 5, 19, "somevalue") 
    83         self.assertEquals((19, "somevalue"), self.mc.get("x")) 
    84         self.mc.delete("x", 1) 
    85         self.assertNotExists("x") 
    86         try: 
    87             self.mc.add("x", 5, 19, "ex2") 
    88             self.fail("Expected failure to add during timed delete") 
    89         except MemcachedError, e: 
    90             self.assertEquals(memcacheConstants.ERR_EXISTS, e.status) 
    91         time.sleep(1.1) 
    92         self.mc.add("x", 5, 19, "ex2") 
    93  
    94     def testFlush(self): 
    95         """Test flushing.""" 
    96         self.mc.set("x", 5, 19, "somevaluex") 
    97         self.mc.set("y", 5, 17, "somevaluey") 
    98         self.assertEquals((19, "somevaluex"), self.mc.get("x")) 
    99         self.assertEquals((17, "somevaluey"), self.mc.get("y")) 
    100         self.mc.flush() 
    101         self.assertNotExists("x") 
    102         self.assertNotExists("y") 
    103  
    104     def testNoop(self): 
    105         """Making sure noop is understood.""" 
    106         self.mc.noop() 
    107  
    108     def testAdd(self): 
    109         """Test add functionality.""" 
    110         self.assertNotExists("x") 
    111         self.mc.add("x", 5, 19, "ex") 
    112         self.assertEquals((19, "ex"), self.mc.get("x")) 
    113         try: 
    114             self.mc.add("x", 5, 19, "ex2") 
    115             self.fail("Expected failure to add existing key") 
    116         except MemcachedError, e: 
    117             self.assertEquals(memcacheConstants.ERR_EXISTS, e.status) 
    118         self.assertEquals((19, "ex"), self.mc.get("x")) 
    119  
    120     def testReplace(self): 
    121         """Test replace functionality.""" 
    122         self.assertNotExists("x") 
    123         try: 
    124             self.mc.replace("x", 5, 19, "ex") 
    125             self.fail("Expected failure to replace missing key") 
    126         except MemcachedError, e: 
    127             self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) 
    128         self.mc.add("x", 5, 19, "ex") 
    129         self.assertEquals((19, "ex"), self.mc.get("x")) 
    130         self.mc.replace("x", 5, 19, "ex2") 
    131         self.assertEquals((19, "ex2"), self.mc.get("x")) 
    132  
    133     def testMultiGet(self): 
    134         """Testing multiget functionality""" 
    135         self.mc.add("x", 5, 1, "ex") 
    136         self.mc.add("y", 5, 2, "why") 
    137         vals=self.mc.getMulti('xyz') 
    138         self.assertEquals((1, 'ex'), vals['x']) 
    139         self.assertEquals((2, 'why'), vals['y']) 
    140         self.assertEquals(2, len(vals)) 
    141  
    142     def testIncrDoesntExistNoCreate(self): 
    143         """Testing incr when a value doesn't exist (and not creating).""" 
    144         try: 
    145             self.mc.incr("x", exp=-1) 
    146             self.fail("Expected failure to increment non-existent key") 
    147         except MemcachedError, e: 
    148             self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) 
    149         self.assertNotExists("x") 
    150  
    151     def testIncrDoesntExistCreate(self): 
    152         """Testing incr when a value doesn't exist (and we make a new one)""" 
    153         self.assertNotExists("x") 
    154         self.assertEquals(19, self.mc.incr("x", init=19)) 
    155  
    156     def testDecrDoesntExistNoCreate(self): 
    157         """Testing decr when a value doesn't exist (and not creating).""" 
    158         try: 
    159             self.mc.decr("x", exp=-1) 
    160             self.fail("Expected failiure to decrement non-existent key.") 
    161         except MemcachedError, e: 
    162             self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) 
    163         self.assertNotExists("x") 
    164  
    165     def testDecrDoesntExistCreate(self): 
    166         """Testing decr when a value doesn't exist (and we make a new one)""" 
    167         self.assertNotExists("x") 
    168         self.assertEquals(19, self.mc.decr("x", init=19)) 
    169  
    170100    def testIncr(self): 
    171101        """Simple incr test.""" 
     
    188118        self.assertEquals(0, val) 
    189119 
     120    def testReservedDelete(self): 
     121        """Test a delete with a reservation timestamp.""" 
     122        self.mc.set("x", 5, 19, "somevalue") 
     123        self.assertEquals((19, "somevalue"), self.mc.get("x")) 
     124        self.mc.delete("x", 1) 
     125        self.assertNotExists("x") 
     126        try: 
     127            self.mc.add("x", 5, 19, "ex2") 
     128            self.fail("Expected failure to add during timed delete") 
     129        except MemcachedError, e: 
     130            self.assertEquals(memcacheConstants.ERR_EXISTS, e.status) 
     131        time.sleep(1.1) 
     132        self.mc.add("x", 5, 19, "ex2") 
     133 
     134    def testFlush(self): 
     135        """Test flushing.""" 
     136        self.mc.set("x", 5, 19, "somevaluex") 
     137        self.mc.set("y", 5, 17, "somevaluey") 
     138        self.assertEquals((19, "somevaluex"), self.mc.get("x")) 
     139        self.assertEquals((17, "somevaluey"), self.mc.get("y")) 
     140        self.mc.flush() 
     141        self.assertNotExists("x") 
     142        self.assertNotExists("y") 
     143 
     144 
     145    def testAdd(self): 
     146        """Test add functionality.""" 
     147        self.assertNotExists("x") 
     148        self.mc.add("x", 5, 19, "ex") 
     149        self.assertEquals((19, "ex"), self.mc.get("x")) 
     150        try: 
     151            self.mc.add("x", 5, 19, "ex2") 
     152            self.fail("Expected failure to add existing key") 
     153        except MemcachedError, e: 
     154            self.assertEquals(memcacheConstants.ERR_EXISTS, e.status) 
     155        self.assertEquals((19, "ex"), self.mc.get("x")) 
     156 
     157    def testReplace(self): 
     158        """Test replace functionality.""" 
     159        self.assertNotExists("x") 
     160        try: 
     161            self.mc.replace("x", 5, 19, "ex") 
     162            self.fail("Expected failure to replace missing key") 
     163        except MemcachedError, e: 
     164            self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) 
     165        self.mc.add("x", 5, 19, "ex") 
     166        self.assertEquals((19, "ex"), self.mc.get("x")) 
     167        self.mc.replace("x", 5, 19, "ex2") 
     168        self.assertEquals((19, "ex2"), self.mc.get("x")) 
     169 
     170    def testMultiGet(self): 
     171        """Testing multiget functionality""" 
     172        self.mc.add("x", 5, 1, "ex") 
     173        self.mc.add("y", 5, 2, "why") 
     174        vals=self.mc.getMulti('xyz') 
     175        self.assertEquals((1, 'ex'), vals['x']) 
     176        self.assertEquals((2, 'why'), vals['y']) 
     177        self.assertEquals(2, len(vals)) 
     178 
     179    def testIncrDoesntExistNoCreate(self): 
     180        """Testing incr when a value doesn't exist (and not creating).""" 
     181        try: 
     182            self.mc.incr("x", exp=-1) 
     183            self.fail("Expected failure to increment non-existent key") 
     184        except MemcachedError, e: 
     185            self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) 
     186        self.assertNotExists("x") 
     187 
     188    def testIncrDoesntExistCreate(self): 
     189        """Testing incr when a value doesn't exist (and we make a new one)""" 
     190        self.assertNotExists("x") 
     191        self.assertEquals(19, self.mc.incr("x", init=19)) 
     192 
     193    def testDecrDoesntExistNoCreate(self): 
     194        """Testing decr when a value doesn't exist (and not creating).""" 
     195        try: 
     196            self.mc.decr("x", exp=-1) 
     197            self.fail("Expected failiure to decrement non-existent key.") 
     198        except MemcachedError, e: 
     199            self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) 
     200        self.assertNotExists("x") 
     201 
     202    def testDecrDoesntExistCreate(self): 
     203        """Testing decr when a value doesn't exist (and we make a new one)""" 
     204        self.assertNotExists("x") 
     205        self.assertEquals(19, self.mc.decr("x", init=19)) 
     206 
    190207    def testCas(self): 
    191208        """Test CAS operation.""" 
     
    294311 
    295312        if ($errcode) { 
    296                 die "Memcache error ($errcode): $rv\n"
     313                die MC::Error->new($errcode, $rv)
    297314        } 
    298315 
     
    342359} 
    343360 
     361sub __incrdecr { 
     362        my $self = shift; 
     363        my ($cmd, $key, $amt, $init, $exp) = @_; 
     364        return $self->_doCmd($cmd, $key, '', pack(::INCRDECR_PKT_FMT, $amt, $init, $exp)); 
     365} 
     366 
     367sub incr { 
     368        my $self = shift; 
     369        my ($key, $amt, $init, $exp) = @_; 
     370        $amt = 1 unless defined $amt; 
     371        $init = 0 unless defined $init; 
     372        $exp = 0 unless defined $exp; 
     373 
     374        return $self->__incrdecr(::CMD_INCR, $key, $amt, $init, $exp); 
     375} 
     376 
     377sub decr { 
     378        my $self = shift; 
     379        my ($key, $amt, $init, $exp) = @_; 
     380        $amt = 1 unless defined $amt; 
     381        $init = 0 unless defined $init; 
     382        $exp = 0 unless defined $exp; 
     383 
     384        return $self->__incrdecr(::CMD_INCR, $key, 0 - $amt, $init, $exp); 
     385} 
     386 
    344387<<EOT; 
    345     def __incrdecr(self, cmd, key, amt, init, exp): 
    346         return long(self._doCmd(cmd, key, '', 
    347             struct.pack(memcacheConstants.INCRDECR_PKT_FMT, amt, init, exp))) 
    348  
    349388    def incr(self, key, amt=1, init=0, exp=0): 
    350389        """Increment or create the named counter.""" 
     
    399438        return rv 
    400439 
    401     def noop(self): 
    402         """Send a noop command.""" 
    403         self._doCmd(memcacheConstants.CMD_NOOP, '', '') 
    404  
    405     def delete(self, key, when=0): 
    406         """Delete the value for a given key within the memcached server.""" 
    407         self._doCmd(memcacheConstants.CMD_DELETE, key, '', 
    408             struct.pack(DEL_PKT_FMT, when)) 
    409  
    410440EOT 
     441 
     442sub noop { 
     443        my $self = shift; 
     444        return $self->_doCmd(::CMD_NOOP, '', ''); 
     445} 
    411446 
    412447sub delete { 
     
    428463} 
    429464 
     465package MC::Error; 
     466 
     467use strict; 
     468use warnings; 
     469 
     470use constant ERR_UNKNOWN_CMD => 0x81; 
     471use constant ERR_NOT_FOUND   => 0x1; 
     472use constant ERR_EXISTS      => 0x2; 
     473 
     474use overload '""' => sub { 
     475        my $self = shift; 
     476 
     477        return "Memcache Error ($self->[0]): $self->[1]"; 
     478}; 
     479 
     480sub new { 
     481        my $class = shift; 
     482        my $error = [@_]; 
     483         
     484        my $self = bless $error, (ref $class || $class); 
     485 
     486        return $self; 
     487} 
     488 
     489sub not_found { 
     490        my $self = shift; 
     491 
     492        return $self->[0] == ERR_NOT_FOUND; 
     493}