Esempio n. 1
0
        def test():
            sA, sB, sC, s1, s2, s3 = (MockSok(x) for x in ['A', 'B', 'C', 1, 2, 3])
            top = be((Observable(),
                (SocketPool(reactor=reactor(), unusedTimeout=0.025),),
            ))

            # Make sure 1st check all-sockets-ok
            yield sleep(seconds=(0.001))

            # Initial set
            yield top.any.putSocketInPool(host='x', port=80, sock=sA)
            yield top.any.putSocketInPool(host='x', port=80, sock=sB)
            yield top.any.putSocketInPool(host='x', port=80, sock=sC)

            yield top.any.putSocketInPool(host='example.org', port=8080, sock=s1)
            yield top.any.putSocketInPool(host='example.org', port=8080, sock=s2)
            yield top.any.putSocketInPool(host='example.org', port=8080, sock=s3)

            self.assertEquals([], s2.log.calledMethodNames())  # sample

            # Pass time, no timeout - 1st check always all-sockets-ok
            yield sleep(seconds=(0.025 + 0.022))  # +/- 0.003 until next mostly-fatal check
            self.assertEquals([], s2.log.calledMethodNames())  # sample

            # Use some, put some back
            _sockC = yield top.any.getPooledSocket(host='x', port=80)
            _sockB = yield top.any.getPooledSocket(host='x', port=80)
            _sock3 = yield top.any.getPooledSocket(host='example.org', port=8080)
            self.assertEquals([sC, sB, s3], [_sockC, _sockB, _sock3])
            self.assertEquals([], sC.log.calledMethodNames())
            self.assertEquals([], sB.log.calledMethodNames())
            self.assertEquals([], s3.log.calledMethodNames())

            yield top.any.putSocketInPool(host='x', port=80, sock=sC)
            yield top.any.putSocketInPool(host='example.org', port=8080, sock=s3)

            yield sleep(seconds=0.015)  # 0.025 - (0.015 - 0.003) = 0.013 until all-fatal check

            inPool = []
            while True:
                result = yield top.any.getPooledSocket(host='x', port=80)
                if result == None:
                    break
                inPool.append(result)

            while True:
                result = yield top.any.getPooledSocket(host='example.org', port=8080)
                if result == None:
                    break
                inPool.append(result)

            self.assertEquals([sC, s3], inPool)
            self.assertEquals([], sC.log.calledMethodNames())
            self.assertEquals([], s3.log.calledMethodNames())
            self.assertEquals(['shutdown', 'close'], s1.log.calledMethodNames())  # sample
            shutdown, close = s1.log.calledMethods
            self.assertEquals(((SHUT_RDWR,), {}), (shutdown.args, shutdown.kwargs))
            self.assertEquals(((), {}), (close.args, close.kwargs))
Esempio n. 2
0
        def test():
            top = be((Observable(),
                (SocketPool(reactor=reactor(), unusedTimeout=0.02),),
            ))
            yield top.any.putSocketInPool(host='x', port=80, sock=MockSok('A'))
            yield top.any.putSocketInPool(host='x', port=80, sock=MockSok('B'))
            yield sleep(seconds=0.001)

            result = yield top.any.getPooledSocket(host='x', port=80)
            self.assertEquals('B', result)

            yield sleep(seconds=0.04)

            result = yield top.any.getPooledSocket(host='x', port=80)
            self.assertEquals(None, result)
        def test():
            top = be((
                Observable(),
                (SocketPool(reactor=reactor(), unusedTimeout=0.02), ),
            ))
            yield top.any.putSocketInPool(host='x', port=80, sock=MockSok('A'))
            yield top.any.putSocketInPool(host='x', port=80, sock=MockSok('B'))
            yield sleep(seconds=0.001)

            result = yield top.any.getPooledSocket(host='x', port=80)
            self.assertEquals('B', result)

            yield sleep(seconds=0.04)

            result = yield top.any.getPooledSocket(host='x', port=80)
            self.assertEquals(None, result)
        def test():
            sA, sB, sC, s1, s2, s3 = (MockSok(x)
                                      for x in ['A', 'B', 'C', 1, 2, 3])
            top = be((
                Observable(),
                (SocketPool(reactor=reactor(), unusedTimeout=0.025), ),
            ))

            # Make sure 1st check all-sockets-ok
            yield sleep(seconds=(0.001))

            # Initial set
            yield top.any.putSocketInPool(host='x', port=80, sock=sA)
            yield top.any.putSocketInPool(host='x', port=80, sock=sB)
            yield top.any.putSocketInPool(host='x', port=80, sock=sC)

            yield top.any.putSocketInPool(host='example.org',
                                          port=8080,
                                          sock=s1)
            yield top.any.putSocketInPool(host='example.org',
                                          port=8080,
                                          sock=s2)
            yield top.any.putSocketInPool(host='example.org',
                                          port=8080,
                                          sock=s3)

            self.assertEquals([], s2.log.calledMethodNames())  # sample

            # Pass time, no timeout - 1st check always all-sockets-ok
            yield sleep(
                seconds=(0.025 +
                         0.022))  # +/- 0.003 until next mostly-fatal check
            self.assertEquals([], s2.log.calledMethodNames())  # sample

            # Use some, put some back
            _sockC = yield top.any.getPooledSocket(host='x', port=80)
            _sockB = yield top.any.getPooledSocket(host='x', port=80)
            _sock3 = yield top.any.getPooledSocket(host='example.org',
                                                   port=8080)
            self.assertEquals([sC, sB, s3], [_sockC, _sockB, _sock3])
            self.assertEquals([], sC.log.calledMethodNames())
            self.assertEquals([], sB.log.calledMethodNames())
            self.assertEquals([], s3.log.calledMethodNames())

            yield top.any.putSocketInPool(host='x', port=80, sock=sC)
            yield top.any.putSocketInPool(host='example.org',
                                          port=8080,
                                          sock=s3)

            yield sleep(
                seconds=0.015
            )  # 0.025 - (0.015 - 0.003) = 0.013 until all-fatal check

            inPool = []
            while True:
                result = yield top.any.getPooledSocket(host='x', port=80)
                if result == None:
                    break
                inPool.append(result)

            while True:
                result = yield top.any.getPooledSocket(host='example.org',
                                                       port=8080)
                if result == None:
                    break
                inPool.append(result)

            self.assertEquals([sC, s3], inPool)
            self.assertEquals([], sC.log.calledMethodNames())
            self.assertEquals([], s3.log.calledMethodNames())
            self.assertEquals(['shutdown', 'close'],
                              s1.log.calledMethodNames())  # sample
            shutdown, close = s1.log.calledMethods
            self.assertEquals(((SHUT_RDWR, ), {}),
                              (shutdown.args, shutdown.kwargs))
            self.assertEquals(((), {}), (close.args, close.kwargs))