def testConnectionManager(self): try: cm = MemcacheConnectionManager() protocol = MemcacheProtocol.create("text") connections = [] def connector(): connections.append(cm.get_connection((MEMCACHE_IP, 11212), protocol)) Tasklet.new(connector)() Tasklet.new(connector)() Tasklet.new(connector)() Tasklet.join_children() Tasklet.new(connector)() Tasklet.join_children() self.assertEquals(4, len(connections)) self.assertEquals(1, len(cm._connections)) finally: cm.close_all()
def testConnectionManager(self): try: cm = MemcacheConnectionManager() protocol = MemcacheProtocol.create("text") connections = [] def connector(): connections.append( cm.get_connection((MEMCACHE_IP, 11211), protocol)) Tasklet.new(connector)() Tasklet.new(connector)() Tasklet.new(connector)() Tasklet.join_children() Tasklet.new(connector)() Tasklet.join_children() self.assertEquals(4, len(connections)) self.assertEquals(1, len(cm._connections)) finally: cm.close_all()
def testTextProtocol(self): from concurrence.io import Socket, BufferedStream from concurrence.memcache.protocol import MemcacheProtocol socket = Socket.connect((MEMCACHE_IP, 11212)) stream = BufferedStream(socket) writer = stream.writer reader = stream.reader try: protocol = MemcacheProtocol.create("textblaat") self.fail("expected error") except MemcacheError: pass protocol = MemcacheProtocol.create("text") self.assertTrue(protocol is MemcacheProtocol.create(protocol)) protocol.set_codec("raw") protocol.write_set(writer, 'hello', 'world', 0, 0) writer.flush() self.assertEquals((MemcacheResult.STORED, None), protocol.read_set(reader)) N = 100 for i in range(N): protocol.write_set(writer, 'test%d' % i, 'hello world %d' % i, 0, 0) writer.flush() self.assertEquals((MemcacheResult.STORED, None), protocol.read_set(reader)) #single get for i in range(N): protocol.write_get(writer, ['test%d' % i]) writer.flush() result = protocol.read_get(reader) self.assertEquals((MemcacheResult.OK, {'test%d' % i: 'hello world %d' % i}), result) #multi get for i in range(0, N, 10): keys = ['test%d' % x for x in range(i, i + 10)] protocol.write_get(writer, keys) writer.flush() result, values = protocol.read_get(reader) self.assertEquals(MemcacheResult.OK, result) self.assertEquals(10, len(values)) #multi get pipeline, e.g. write N gets, but don't read out the results yet for i in range(0, N, 10): keys = ['test%d' % x for x in range(i, i + 10)] protocol.write_get(writer, keys) writer.flush() #now read the results for i in range(0, N, 10): result, values = protocol.read_get(reader) self.assertEquals(10, len(values)) self.assertTrue(('test%d' % i) in values) #pipelined multiget with same set of keys protocol.write_get(writer, ['test2', 'test8', 'test9', 'test11', 'test23', 'test24', 'test29', 'test31', 'test34']) writer.flush() protocol.write_get(writer, ['test2', 'test8', 'test9', 'test11', 'test23', 'test24', 'test29', 'test31', 'test34']) writer.flush() result1 = protocol.read_get(reader) result2 = protocol.read_get(reader) self.assertEquals(result1, result2)
def testTextProtocol(self): from concurrence.io import Socket, BufferedStream from concurrence.memcache.protocol import MemcacheProtocol socket = Socket.connect((MEMCACHE_IP, 11211)) stream = BufferedStream(socket) writer = stream.writer reader = stream.reader try: protocol = MemcacheProtocol.create("textblaat") self.fail("expected error") except MemcacheError: pass protocol = MemcacheProtocol.create("text") self.assertTrue(protocol is MemcacheProtocol.create(protocol)) protocol.set_codec("raw") protocol.write_set(writer, 'hello', 'world', 0, 0) writer.flush() self.assertEquals((MemcacheResult.STORED, None), protocol.read_set(reader)) N = 100 for i in range(N): protocol.write_set(writer, 'test%d' % i, 'hello world %d' % i, 0, 0) writer.flush() self.assertEquals((MemcacheResult.STORED, None), protocol.read_set(reader)) #single get for i in range(N): protocol.write_get(writer, ['test%d' % i]) writer.flush() result = protocol.read_get(reader) self.assertEquals((MemcacheResult.OK, { 'test%d' % i: 'hello world %d' % i }), result) #multi get for i in range(0, N, 10): keys = ['test%d' % x for x in range(i, i + 10)] protocol.write_get(writer, keys) writer.flush() result, values = protocol.read_get(reader) self.assertEquals(MemcacheResult.OK, result) self.assertEquals(10, len(values)) #multi get pipeline, e.g. write N gets, but don't read out the results yet for i in range(0, N, 10): keys = ['test%d' % x for x in range(i, i + 10)] protocol.write_get(writer, keys) writer.flush() #now read the results for i in range(0, N, 10): result, values = protocol.read_get(reader) self.assertEquals(10, len(values)) self.assertTrue(('test%d' % i) in values) #pipelined multiget with same set of keys protocol.write_get(writer, [ 'test2', 'test8', 'test9', 'test11', 'test23', 'test24', 'test29', 'test31', 'test34' ]) writer.flush() protocol.write_get(writer, [ 'test2', 'test8', 'test9', 'test11', 'test23', 'test24', 'test29', 'test31', 'test34' ]) writer.flush() result1 = protocol.read_get(reader) result2 = protocol.read_get(reader) self.assertEquals(result1, result2)