def test(): sp = SocketPool(reactor=CallTrace(), limits={'destinationSize': 2}) # Limits enforced on put, not async. def stillPooled(): wasStillPooled = [] for destHost, destPort in [('h', 1), ('i', 2), ('j', 3)]: while True: # do ... while (fromPool is not None) fromPool = yield sp.getPooledSocket(host=destHost, port=destPort) if fromPool: wasStillPooled.append(fromPool) if fromPool is None: break raise StopIteration(wasStillPooled) sJ = MockSok('sJ') sJ2 = MockSok('sJ2') yield sp.putSocketInPool(host='h', port=1, sock=MockSok('sH')) yield sp.putSocketInPool(host='i', port=2, sock=MockSok('sI')) yield sp.putSocketInPool(host='j', port=3, sock=sJ) yield sp.putSocketInPool(host='j', port=3, sock=sJ2) with stderr_replaced() as err: yield sp.putSocketInPool(host='j', port=3, sock=MockSok('sJ3')) self.assertEquals('', err.getvalue(), err.getvalue()) wasStillPooled = yield stillPooled() self.assertEquals(4, len(wasStillPooled)) self.assertEquals(['sH', 'sI', 'sJ3', 'sJ2'], wasStillPooled) self.assertEquals(['shutdown', 'close'], sJ.log.calledMethodNames())
def test(): sp = SocketPool(reactor=CallTrace(), limits={'destinationSize': 2}) # Limits enforced on put, not async. def stillPooled(): wasStillPooled = [] for destHost, destPort in [('h', 1), ('i', 2), ('j', 3)]: while True: # do ... while (fromPool is not None) fromPool = yield sp.getPooledSocket(host=destHost, port=destPort) if fromPool: wasStillPooled.append(fromPool) if fromPool is None: break raise StopIteration(wasStillPooled) sJ = MockSok('sJ') sJ2 = MockSok('sJ2') yield sp.putSocketInPool(host='h', port=1, sock=MockSok('sH')) yield sp.putSocketInPool(host='i', port=2, sock=MockSok('sI')) yield sp.putSocketInPool(host='j', port=3, sock=sJ) yield sp.putSocketInPool(host='j', port=3, sock=sJ2) with stderr_replaced() as err: yield sp.putSocketInPool(host='j', port=3, sock=MockSok('sJ3')) self.assertEquals('', err.getvalue(), err.getvalue()) wasStillPooled = yield stillPooled() self.assertEquals(4, len(wasStillPooled)) self.assertEquals(['sH', 'sI', 'sJ3', 'sJ2'], wasStillPooled) self.assertEquals(['shutdown', 'close'], sJ.log.calledMethodNames())
def test(): sp = SocketPool(reactor=CallTrace(), limits={'totalSize': 2}) # Limits enforced on put, not async. def stillPooled(): wasStillPooled = [] for destHost, destPort in [('h', 1), ('i', 2), ('j', 3)]: while True: # do ... while (fromPool is not None) fromPool = yield sp.getPooledSocket(host=destHost, port=destPort) if fromPool: wasStillPooled.append(fromPool) if fromPool is None: break raise StopIteration(wasStillPooled) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('sH')) yield sp.putSocketInPool(host='i', port=2, sock=MockSok('sI')) with stderr_replaced() as err: yield sp.putSocketInPool(host='j', port=3, sock=MockSok('sJ')) self.assertEquals('', err.getvalue(), err.getvalue()) wasStillPooled = yield stillPooled() self.assertEquals(2, len(wasStillPooled)) self.assertTrue( set(wasStillPooled).issubset(set(['sH', 'sI', 'sJ'])))
def test(): sp = SocketPool(reactor=CallTrace(), limits={'totalSize': 2}) # Limits enforced on put, not async. def stillPooled(): wasStillPooled = [] while True: # do ... while (fromPool is not None) fromPool = yield sp.getPooledSocket(host='h', port=1) if fromPool: wasStillPooled.append(fromPool) if fromPool is None: break raise StopIteration(wasStillPooled) s0 = MockSok('s0') s1 = MockSok('s1') yield sp.putSocketInPool(host='h', port=1, sock=s0) yield sp.putSocketInPool(host='h', port=1, sock=s1) with stderr_replaced() as err: yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s2')) self.assertEquals('', err.getvalue(), err.getvalue()) #@@ wasStillPooled = yield stillPooled() self.assertEquals(['s2', 's0'], wasStillPooled) self.assertEquals(['shutdown', 'close'], s1.log.calledMethodNames()) shutCall, closeCall = s1.log.calledMethods self.assertEquals(((SHUT_RDWR,), {}), (shutCall.args, shutCall.kwargs)) self.assertEquals(((), {}), (closeCall.args, closeCall.kwargs)) self.assertEquals([], s0.log.calledMethodNames()) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s0')) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s1')) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s2')) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s3')) wasStillPooled = yield stillPooled() self.assertEquals(['s3', 's0'], wasStillPooled)
def testPutNGetLIFO(self): sp = SocketPool(reactor=CallTrace()) retval(sp.putSocketInPool(host='x', port=1, sock='A')) retval(sp.putSocketInPool(host='x', port=1, sock='B')) retval(sp.putSocketInPool(host='x', port=1, sock='C')) self.assertEquals('C', retval(sp.getPooledSocket(host='x', port=1))) self.assertEquals('B', retval(sp.getPooledSocket(host='x', port=1))) self.assertEquals('A', retval(sp.getPooledSocket(host='x', port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host='x', port=1)))
def testPutNGetLIFO(self): sp = SocketPool(reactor=CallTrace()) retval(sp.putSocketInPool(host='x', port=1, sock='A')) retval(sp.putSocketInPool(host='x', port=1, sock='B')) retval(sp.putSocketInPool(host='x', port=1, sock='C')) self.assertEquals('C', retval(sp.getPooledSocket(host='x', port=1))) self.assertEquals('B', retval(sp.getPooledSocket(host='x', port=1))) self.assertEquals('A', retval(sp.getPooledSocket(host='x', port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host='x', port=1)))
def testPutEmptyPut(self): sp = SocketPool(reactor=CallTrace()) retval(sp.putSocketInPool(host='10.0.0.1', port=60000, sock=0)) retval(sp.putSocketInPool(host='10.0.0.1', port=60000, sock=1)) for i in reversed(range(2)): self.assertEquals(i, retval(sp.getPooledSocket(host='10.0.0.1', port=60000))) self.assertEquals(None, retval(sp.getPooledSocket(host='10.0.0.1', port=60000))) retval(sp.putSocketInPool(host='10.0.0.1', port=60000, sock=2)) self.assertEquals(2, retval(sp.getPooledSocket(host='10.0.0.1', port=60000)))
def testPutNGet1Put1StillLIFO(self): sp = SocketPool(reactor=CallTrace()) retval(sp.putSocketInPool(host='example.org', port=80, sock='A')) retval(sp.putSocketInPool(host='example.org', port=80, sock='B')) self.assertEquals('B', retval(sp.getPooledSocket(host='example.org', port=80))) retval(sp.putSocketInPool(host='example.org', port=80, sock='C')) self.assertEquals('C', retval(sp.getPooledSocket(host='example.org', port=80))) self.assertEquals('A', retval(sp.getPooledSocket(host='example.org', port=80))) self.assertEquals(None, retval(sp.getPooledSocket(host='example.org', port=80)))
def testPutEmptyPut(self): sp = SocketPool(reactor=CallTrace()) retval(sp.putSocketInPool(host='10.0.0.1', port=60000, sock=0)) retval(sp.putSocketInPool(host='10.0.0.1', port=60000, sock=1)) for i in reversed(range(2)): self.assertEquals( i, retval(sp.getPooledSocket(host='10.0.0.1', port=60000))) self.assertEquals( None, retval(sp.getPooledSocket(host='10.0.0.1', port=60000))) retval(sp.putSocketInPool(host='10.0.0.1', port=60000, sock=2)) self.assertEquals( 2, retval(sp.getPooledSocket(host='10.0.0.1', port=60000)))
def testPut3GetOnlyYours(self): sp = SocketPool(reactor=CallTrace()) retval(sp.putSocketInPool(host='x', port=1, sock='A')) retval(sp.putSocketInPool(host='x', port=2, sock='B')) retval(sp.putSocketInPool(host='y', port=1, sock='C')) # Unknown host + port self.assertEquals(None, retval(sp.getPooledSocket(host='xx', port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host='', port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host=None, port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host='x', port=0))) self.assertEquals(None, retval(sp.getPooledSocket(host='x', port=3))) # Retrieved once self.assertEquals('A', retval(sp.getPooledSocket(host='x', port=1)))
def testPut3GetOnlyYours(self): sp = SocketPool(reactor=CallTrace()) retval(sp.putSocketInPool(host='x', port=1, sock='A')) retval(sp.putSocketInPool(host='x', port=2, sock='B')) retval(sp.putSocketInPool(host='y', port=1, sock='C')) # Unknown host + port self.assertEquals(None, retval(sp.getPooledSocket(host='xx', port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host='', port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host=None, port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host='x', port=0))) self.assertEquals(None, retval(sp.getPooledSocket(host='x', port=3))) # Retrieved once self.assertEquals('A', retval(sp.getPooledSocket(host='x', port=1)))
def testPutNGet1Put1StillLIFO(self): sp = SocketPool(reactor=CallTrace()) retval(sp.putSocketInPool(host='example.org', port=80, sock='A')) retval(sp.putSocketInPool(host='example.org', port=80, sock='B')) self.assertEquals( 'B', retval(sp.getPooledSocket(host='example.org', port=80))) retval(sp.putSocketInPool(host='example.org', port=80, sock='C')) self.assertEquals( 'C', retval(sp.getPooledSocket(host='example.org', port=80))) self.assertEquals( 'A', retval(sp.getPooledSocket(host='example.org', port=80))) self.assertEquals( None, retval(sp.getPooledSocket(host='example.org', port=80)))
def test(): sp = SocketPool(reactor=CallTrace(), limits={'totalSize': 2}) # Limits enforced on put, not async. def stillPooled(): wasStillPooled = [] for destHost, destPort in [('h', 1), ('i', 2), ('j', 3)]: while True: # do ... while (fromPool is not None) fromPool = yield sp.getPooledSocket(host=destHost, port=destPort) if fromPool: wasStillPooled.append(fromPool) if fromPool is None: break raise StopIteration(wasStillPooled) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('sH')) yield sp.putSocketInPool(host='i', port=2, sock=MockSok('sI')) with stderr_replaced() as err: yield sp.putSocketInPool(host='j', port=3, sock=MockSok('sJ')) self.assertEquals('', err.getvalue(), err.getvalue()) wasStillPooled = yield stillPooled() self.assertEquals(2, len(wasStillPooled)) self.assertTrue(set(wasStillPooled).issubset(set(['sH', 'sI', 'sJ'])))
def test(): sp = SocketPool(reactor=CallTrace(), limits={'totalSize': 2}) # Limits enforced on put, not async. def stillPooled(): wasStillPooled = [] while True: # do ... while (fromPool is not None) fromPool = yield sp.getPooledSocket(host='h', port=1) if fromPool: wasStillPooled.append(fromPool) if fromPool is None: break raise StopIteration(wasStillPooled) s0 = MockSok('s0') s1 = MockSok('s1') yield sp.putSocketInPool(host='h', port=1, sock=s0) yield sp.putSocketInPool(host='h', port=1, sock=s1) with stderr_replaced() as err: yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s2')) self.assertEquals('', err.getvalue(), err.getvalue()) #@@ wasStillPooled = yield stillPooled() self.assertEquals(['s2', 's0'], wasStillPooled) self.assertEquals(['shutdown', 'close'], s1.log.calledMethodNames()) shutCall, closeCall = s1.log.calledMethods self.assertEquals(((SHUT_RDWR, ), {}), (shutCall.args, shutCall.kwargs)) self.assertEquals(((), {}), (closeCall.args, closeCall.kwargs)) self.assertEquals([], s0.log.calledMethodNames()) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s0')) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s1')) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s2')) yield sp.putSocketInPool(host='h', port=1, sock=MockSok('s3')) wasStillPooled = yield stillPooled() self.assertEquals(['s3', 's0'], wasStillPooled)
def testPutThenGetThenEmpty(self): sp = SocketPool(reactor=CallTrace()) result = retval(sp.putSocketInPool(host='x', port=1, sock='mock')) self.assertEquals(None, result) self.assertEquals('mock', retval(sp.getPooledSocket(host='x', port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host='x', port=1)))
def testPutThenGetThenEmpty(self): sp = SocketPool(reactor=CallTrace()) result = retval(sp.putSocketInPool(host='x', port=1, sock='mock')) self.assertEquals(None, result) self.assertEquals('mock', retval(sp.getPooledSocket(host='x', port=1))) self.assertEquals(None, retval(sp.getPooledSocket(host='x', port=1)))