Пример #1
0
    def test_fullReceive(self):
        msg = toSendBuffer(self.sampleBuffer)
        msglen = len(msg)

        rcv = ReceiveBuffer()
        assert not rcv.isDone(), 'initial ReceiveBuffer isDone test'

        rcv.addMore(msg)

        self.finalTests(rcv, self.sampleBuffer, 'completion')
Пример #2
0
    def test_fullReceiveTinyMessage(self):
        msg = toSendBuffer('I')
        msglen = len(msg)

        rcv = ReceiveBuffer()
        self.assertFalse(rcv.isDone(), 'initial ReceiveBuffer isDone test')

        rcv.addMore(msg)

        self.finalTests(rcv, 'I', 'completion')
Пример #3
0
    def test_fullReceive(self):
        msg = toSendBuffer(self.sampleBuffer)
        msglen = len(msg)

        rcv = ReceiveBuffer()
        assert not rcv.isDone(), 'initial ReceiveBuffer isDone test'

        rcv.addMore(msg)

        self.finalTests(rcv, self.sampleBuffer, 'completion')
Пример #4
0
    def test_fullReceiveTinyMessage(self):
        msg = toSendBuffer('I')
        msglen = len(msg)

        rcv = ReceiveBuffer()
        assert not rcv.isDone(), 'initial ReceiveBuffer isDone test'

        rcv.addMore(msg)

        self.finalTests(rcv, 'I', 'completion')
Пример #5
0
class TCPIncoming(PauseWithBackoff):
    def __init__(self, acceptResponse):
        super(TCPIncoming, self).__init__()
        self._aResp = acceptResponse
        self._rData = ReceiveBuffer(serializer.loads)
        self._expires = datetime.now() + MAX_INCOMING_SOCKET_PERIOD
        self._aResp[0].setblocking(0)
        # Disable Nagle to transmit headers and acks asap
        self._aResp[0].setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self._openSock = True
        self.failCount = 0

    @property
    def socket(self):
        return self._aResp[0] if self._openSock else None

    @property
    def fromAddress(self):
        return ActorAddress(TCPv4ActorAddress(*(self._aResp[1])))

    def delay(self):
        now = datetime.now()
        # n.b. include _pauseUntil from PauseWithBackoff
        return max(
            timedelta(seconds=0),
            min(self._expires - now,
                getattr(self, '_pauseUntil', self._expires) - now))

    def addData(self, newData):
        self._rData.addMore(newData)

    def remainingSize(self):
        return self._rData.remainingAmount()

    def receivedAllData(self):
        return self._rData.isDone()

    @property
    def data(self):
        return self._rData.completed()[0]

    def close(self):
        s = self.socket
        if s:
            self._openSock = False
            _safeSocketShutdown(s)

    def __del__(self):
        s = self.socket
        if s:
            self._openSock = False
            _safeSocketShutdown(s)

    def __str__(self):
        return 'TCPInc(%s)<%s>' % (str(self._aResp), str(self._rData))
Пример #6
0
    def test_fullReceiveHugeMessage(self):
        bigMessage = 'I' * 1024 * 1024 * 100  # 100MB
        msg = toSendBuffer(bigMessage)
        msglen = len(msg)

        rcv = ReceiveBuffer()
        assert not rcv.isDone(), 'initial ReceiveBuffer isDone test'

        rcv.addMore(msg)

        self.finalTests(rcv, bigMessage, 'completion')
Пример #7
0
    def test_fullReceiveHugeMessage(self):
        bigMessage = 'I' * 1024 * 1024 * 100   # 100MB
        msg = toSendBuffer(bigMessage)
        msglen = len(msg)

        rcv = ReceiveBuffer()
        self.assertFalse(rcv.isDone(), 'initial ReceiveBuffer isDone test')

        rcv.addMore(msg)

        self.finalTests(rcv, bigMessage, 'completion')
Пример #8
0
    def test_singleBreakAtVariousPoints(self):
        msg = toSendBuffer(self.sampleBuffer)
        msglen = len(msg)
        for point in range(msglen):
            rcv = ReceiveBuffer()
            self.assertFalse(rcv.isDone(), 'initial ReceiveBuffer isDone test')

            rcv.addMore(msg[:point])
            self.partialTests(rcv, point, msglen, 'sample message first add @ %s'%point)

            rcv.addMore(msg[point:])
            self.finalTests(rcv, self.sampleBuffer, 'sample message completion @ %s'%point)
Пример #9
0
    def test_singleBreakAtVariousPointsBigMessage(self):
        bigMessage = '0123456789' * 1024 * 1024 * 10   # 100MB
        msg = toSendBuffer(bigMessage)
        msglen = len(msg)
        for point in fibonacci(msglen):
            rcv = ReceiveBuffer()
            self.assertFalse(rcv.isDone(), 'initial ReceiveBuffer isDone test')

            rcv.addMore(msg[:point])
            self.partialTests(rcv, point, msglen, 'big message first add @ %s'%point)

            rcv.addMore(msg[point:])
            self.finalTests(rcv, bigMessage, 'big message completion @ %s'%point)
Пример #10
0
    def test_singleBreakAtVariousPoints(self):
        msg = toSendBuffer(self.sampleBuffer)
        msglen = len(msg)
        for point in range(msglen):
            rcv = ReceiveBuffer()
            assert not rcv.isDone(), 'initial ReceiveBuffer isDone test'

            rcv.addMore(msg[:point])
            self.partialTests(rcv, point, msglen,
                              'sample message first add @ %s' % point)

            rcv.addMore(msg[point:])
            self.finalTests(rcv, self.sampleBuffer,
                            'sample message completion @ %s' % point)
Пример #11
0
    def test_singleBreakAtVariousPointsBigMessage(self):
        bigMessage = '0123456789' * 1024 * 1024 * 10  # 100MB
        msg = toSendBuffer(bigMessage)
        msglen = len(msg)
        for point in fibonacci(msglen):
            rcv = ReceiveBuffer()
            assert not rcv.isDone(), 'initial ReceiveBuffer isDone test'

            rcv.addMore(msg[:point])
            self.partialTests(rcv, point, msglen,
                              'big message first add @ %s' % point)

            rcv.addMore(msg[point:])
            self.finalTests(rcv, bigMessage,
                            'big message completion @ %s' % point)
Пример #12
0
    def test_all_points_incomplete_each_byte(self):
        message = 'hello'
        extra = b'world'
        msg = toSendBuffer(message)
        origmsglen = len(msg)
        msg += extra
        for point in range(1, origmsglen - 1):
            rcv = ReceiveBuffer()
            assert not rcv.isDone()
            assert rcv.is_empty()

            for bpos in range(point):
                rcv.addMore(msg[bpos:bpos + 1])
            assert not rcv.isDone()
            assert not rcv.is_empty()
            assert rcv.completed() is None
Пример #13
0
    def test_all_points_incomplete_each_byte(self):
        message = 'hello'
        extra = b'world'
        msg = toSendBuffer(message)
        origmsglen = len(msg)
        msg += extra
        for point in range(1, origmsglen - 1):
            rcv = ReceiveBuffer()
            assert not rcv.isDone()
            assert rcv.is_empty()

            for bpos in range(point):
                rcv.addMore(msg[bpos:bpos+1])
            assert not rcv.isDone()
            assert not rcv.is_empty()
            assert rcv.completed() is None
Пример #14
0
    def test_multipleBreaksAtVariousSizes(self):
        # This test is important because it breaks at *every* size,
        # meaning that all of the prefix length and corresponding
        # elements are tested in various multiple segment pieces.
        msg = toSendBuffer(self.sampleBuffer)
        msglen = len(msg)
        for partLen in range(1, msglen):
            rcv = ReceiveBuffer()
            self.assertFalse(rcv.isDone(), 'initial ReceiveBuffer isDone test')

            for partnum, point in enumerate(range(0, msglen, partLen)):
                rcv.addMore(msg[point:point+partLen])
                if point + partLen < msglen:
                    self.partialTests(rcv, point+partLen, msglen,
                                      'partial add #%d of %d' % (partnum, partLen))

            self.finalTests(rcv, self.sampleBuffer, 'completion')
Пример #15
0
    def test_multipleBreaksAtVariousSizes(self):
        # This test is important because it breaks at *every* size,
        # meaning that all of the prefix length and corresponding
        # elements are tested in various multiple segment pieces.
        msg = toSendBuffer(self.sampleBuffer)
        msglen = len(msg)
        for partLen in range(1, msglen):
            rcv = ReceiveBuffer()
            assert not rcv.isDone(), 'initial ReceiveBuffer isDone test'

            for partnum, point in enumerate(range(0, msglen, partLen)):
                rcv.addMore(msg[point:point + partLen])
                if point + partLen < msglen:
                    self.partialTests(
                        rcv, point + partLen, msglen,
                        'partial add #%d of %d' % (partnum, partLen))

            self.finalTests(rcv, self.sampleBuffer, 'completion')
Пример #16
0
    def test_all_points_extra(self):
        message = 'hello'
        extra = b'world'
        msg = toSendBuffer(message)
        origmsglen = len(msg)
        msg += extra
        for point in range(origmsglen + 1, len(msg)):
            rcv = ReceiveBuffer()
            assert not rcv.isDone()
            assert rcv.is_empty()

            rcv.addMore(msg[:point])
            assert rcv.isDone()
            assert not rcv.is_empty()

            print(rcv.completed())
            rmsg, rextra = rcv.completed()
            assert rmsg == message
            assert rextra == extra[:point - origmsglen]
Пример #17
0
    def test_exact(self):
        message = 'hello'
        extra = b'world'
        msg = toSendBuffer(message)
        origmsglen = len(msg)
        msg += extra

        rcv = ReceiveBuffer()
        assert not rcv.isDone()
        assert rcv.is_empty()

        rcv.addMore(msg[:origmsglen])
        assert rcv.isDone()
        assert not rcv.is_empty()
        # py.test bug, cannot just: assert rcv.completed() == message, b''
        rmsg, rextra = rcv.completed()
        print(rmsg, rextra)
        assert rmsg == message
        assert b'' == rextra
Пример #18
0
    def test_all_points_extra(self):
        message = 'hello'
        extra = b'world'
        msg = toSendBuffer(message)
        origmsglen = len(msg)
        msg += extra
        for point in range(origmsglen+1, len(msg)):
            rcv = ReceiveBuffer()
            assert not rcv.isDone()
            assert rcv.is_empty()

            rcv.addMore(msg[:point])
            assert rcv.isDone()
            assert not rcv.is_empty()

            print(rcv.completed())
            rmsg, rextra = rcv.completed()
            assert rmsg == message
            assert rextra == extra[:point-origmsglen]
Пример #19
0
    def test_exact(self):
        message = 'hello'
        extra = b'world'
        msg = toSendBuffer(message)
        origmsglen = len(msg)
        msg += extra

        rcv = ReceiveBuffer()
        assert not rcv.isDone()
        assert rcv.is_empty()

        rcv.addMore(msg[:origmsglen])
        assert rcv.isDone()
        assert not rcv.is_empty()
        # py.test bug, cannot just: assert rcv.completed() == message, b''
        rmsg, rextra = rcv.completed()
        print(rmsg, rextra)
        assert rmsg == message
        assert b'' == rextra
Пример #20
0
    def test_multipleBreaksAtVariousBigBuffer(self):
        # This test is less specific than the
        # test_multipleBreaksAtVariousSizes because it only breaks the
        # middle of the buffer... it mostly just verifies large buffer
        # reconstruction.
        bigMessage = 'ABCDEfghij' * 1024 * 1024 * 10   # 100MB
        msg = toSendBuffer(bigMessage)
        msglen = len(msg)
        # Check last three fibonacci sizes only... don't have all day
        for partLen in [F for F in fibonacci(msglen)][-1:-3:-1]:
            if partLen < 5000: continue  # those sizes should already have been verified
            rcv = ReceiveBuffer()
            self.assertFalse(rcv.isDone(), 'initial ReceiveBuffer isDone test')

            for partnum, point in enumerate(range(0, msglen, partLen)):
                rcv.addMore(msg[point:point+partLen])
                if point + partLen < msglen:
                    self.partialTests(rcv, point+partLen, msglen,
                                      'partial add #%d of %d' % (partnum, partLen))

            self.finalTests(rcv, bigMessage, 'completion')
Пример #21
0
    def test_multipleBreaksAtVariousBigBuffer(self):
        # This test is less specific than the
        # test_multipleBreaksAtVariousSizes because it only breaks the
        # middle of the buffer... it mostly just verifies large buffer
        # reconstruction.
        bigMessage = 'ABCDEfghij' * 1024 * 1024 * 10  # 100MB
        msg = toSendBuffer(bigMessage)
        msglen = len(msg)
        # Check last three fibonacci sizes only... don't have all day
        for partLen in [F for F in fibonacci(msglen)][-1:-3:-1]:
            if partLen < 5000:
                continue  # those sizes should already have been verified
            rcv = ReceiveBuffer()
            assert not rcv.isDone(), 'initial ReceiveBuffer isDone test'

            for partnum, point in enumerate(range(0, msglen, partLen)):
                rcv.addMore(msg[point:point + partLen])
                if point + partLen < msglen:
                    self.partialTests(
                        rcv, point + partLen, msglen,
                        'partial add #%d of %d' % (partnum, partLen))

            self.finalTests(rcv, bigMessage, 'completion')
Пример #22
0
class TCPIncoming(PauseWithBackoff):
    def __init__(self, acceptResponse):
        super(TCPIncoming, self).__init__()
        self._aResp = acceptResponse
        self._rData = ReceiveBuffer(serializer.loads)
        self._expires = datetime.now() + MAX_INCOMING_SOCKET_PERIOD
        self._aResp[0].setblocking(0)
        # Disable Nagle to transmit headers and acks asap
        self._aResp[0].setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self._openSock = True
        self.failCount = 0
    @property
    def socket(self): return self._aResp[0] if self._openSock else None
    @property
    def fromAddress(self): return ActorAddress(TCPv4ActorAddress(*(self._aResp[1])))
    def delay(self):
        now = datetime.now()
        # n.b. include _pauseUntil from PauseWithBackoff
        return max(timedelta(seconds=0),
                   min(self._expires - now,
                       getattr(self, '_pauseUntil', self._expires) - now))
    def addData(self, newData): self._rData.addMore(newData)
    def remainingSize(self): return self._rData.remainingAmount()
    def receivedAllData(self): return self._rData.isDone()
    @property
    def data(self): return self._rData.completed()[0]
    def close(self):
        s = self.socket
        if s:
            self._openSock = False
            _safeSocketShutdown(s)
    def __del__(self):
        s = self.socket
        if s:
            self._openSock = False
            _safeSocketShutdown(s)
    def __str__(self): return 'TCPInc(%s)<%s>'%(str(self._aResp), str(self._rData))