~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/test/test_stateful.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
 
 
5
"""
 
6
Test cases for twisted.protocols.stateful
 
7
"""
 
8
 
 
9
from twisted.test import test_protocols
 
10
from twisted.protocols.stateful import StatefulProtocol
 
11
 
 
12
from struct import pack, unpack, calcsize
 
13
 
 
14
 
 
15
class MyInt32StringReceiver(StatefulProtocol):
 
16
    """
 
17
    A stateful Int32StringReceiver.
 
18
    """
 
19
    MAX_LENGTH = 99999
 
20
    structFormat = "!I"
 
21
    prefixLength = calcsize(structFormat)
 
22
 
 
23
    def getInitialState(self):
 
24
        return self._getHeader, 4
 
25
 
 
26
    def lengthLimitExceeded(self, length):
 
27
        self.transport.loseConnection()
 
28
 
 
29
    def _getHeader(self, msg):
 
30
        length, = unpack("!i", msg)
 
31
        if length > self.MAX_LENGTH:
 
32
            self.lengthLimitExceeded(length)
 
33
            return
 
34
        return self._getString, length
 
35
 
 
36
    def _getString(self, msg):
 
37
        self.stringReceived(msg)
 
38
        return self._getHeader, 4
 
39
 
 
40
    def stringReceived(self, msg):
 
41
        """
 
42
        Override this.
 
43
        """
 
44
        raise NotImplementedError
 
45
 
 
46
    def sendString(self, data):
 
47
        """
 
48
        Send an int32-prefixed string to the other end of the connection.
 
49
        """
 
50
        self.transport.write(pack(self.structFormat, len(data)) + data)
 
51
 
 
52
 
 
53
class TestInt32(MyInt32StringReceiver):
 
54
    def connectionMade(self):
 
55
        self.received = []
 
56
 
 
57
    def stringReceived(self, s):
 
58
        self.received.append(s)
 
59
 
 
60
    MAX_LENGTH = 50
 
61
    closed = 0
 
62
 
 
63
    def connectionLost(self, reason):
 
64
        self.closed = 1
 
65
 
 
66
 
 
67
class Int32TestCase(test_protocols.Int32TestCase):
 
68
    protocol = TestInt32
 
69
 
 
70
    def test_bigReceive(self):
 
71
        r = self.getProtocol()
 
72
        big = ""
 
73
        for s in self.strings * 4:
 
74
            big += pack("!i", len(s)) + s
 
75
        r.dataReceived(big)
 
76
        self.assertEquals(r.received, self.strings * 4)
 
77