tests/test-wireproto-clientreactor.py
author Gregory Szorc <gregory.szorc@gmail.com>
Fri, 12 Oct 2018 19:49:02 +0200
changeset 40211 d216ae4cc3f6
parent 40132 e67522413ca8
child 40482 07b87ee2ea75
permissions -rw-r--r--
py3: r'' prefix default values for mimetypes.guess_mime() As suggested by @yuja in D4967. Differential Revision: https://phab.mercurial-scm.org/D5020
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
from __future__ import absolute_import
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
import unittest
40132
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
     4
import zlib
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
from mercurial import (
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
    error,
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
     8
    ui as uimod,
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
    wireprotoframing as framing,
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
)
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
    11
from mercurial.utils import (
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
    12
    cborutil,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
    13
)
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
40132
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
    15
try:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
    16
    from mercurial import zstd
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
    17
    zstd.__version__
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
    18
except ImportError:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
    19
    zstd = None
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
    20
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    21
ffs = framing.makeframefromhumanstring
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    22
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
    23
globalui = uimod.ui()
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
    24
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    25
def sendframe(reactor, frame):
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    26
    """Send a frame bytearray to a reactor."""
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    27
    header = framing.parseheader(frame)
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    28
    payload = frame[framing.FRAME_HEADER_SIZE:]
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    29
    assert len(payload) == header.length
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    30
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    31
    return reactor.onframerecv(framing.frame(header.requestid,
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    32
                                             header.streamid,
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    33
                                             header.streamflags,
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    34
                                             header.typeid,
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    35
                                             header.flags,
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    36
                                             payload))
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
    37
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
class SingleSendTests(unittest.TestCase):
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
    """A reactor that can only send once rejects subsequent sends."""
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    40
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    41
    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    42
        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    43
        # the regex version.
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    44
        assertRaisesRegex = (# camelcase-required
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    45
            unittest.TestCase.assertRaisesRegexp)
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    46
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
    def testbasic(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
    48
        reactor = framing.clientreactor(globalui,
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
    49
                                        hasmultiplesend=False,
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
    50
                                        buffersends=True)
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
        request, action, meta = reactor.callcommand(b'foo', {})
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    53
        self.assertEqual(request.state, b'pending')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    54
        self.assertEqual(action, b'noop')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
        action, meta = reactor.flushcommands()
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    57
        self.assertEqual(action, b'sendframes')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    59
        for frame in meta[b'framegen']:
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    60
            self.assertEqual(request.state, b'sending')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    62
        self.assertEqual(request.state, b'sent')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    64
        with self.assertRaisesRegex(error.ProgrammingError,
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
                                     'cannot issue new commands'):
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
            reactor.callcommand(b'foo', {})
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
    68
        with self.assertRaisesRegex(error.ProgrammingError,
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
                                     'cannot issue new commands'):
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
            reactor.callcommand(b'foo', {})
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
class NoBufferTests(unittest.TestCase):
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
    """A reactor without send buffering sends requests immediately."""
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
    def testbasic(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
    75
        reactor = framing.clientreactor(globalui,
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
    76
                                        hasmultiplesend=True,
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
    77
                                        buffersends=False)
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    78
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    79
        request, action, meta = reactor.callcommand(b'command1', {})
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    80
        self.assertEqual(request.requestid, 1)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    81
        self.assertEqual(action, b'sendframes')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    82
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    83
        self.assertEqual(request.state, b'pending')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    84
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    85
        for frame in meta[b'framegen']:
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    86
            self.assertEqual(request.state, b'sending')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    87
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    88
        self.assertEqual(request.state, b'sent')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    89
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    90
        action, meta = reactor.flushcommands()
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    91
        self.assertEqual(action, b'noop')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    92
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    93
        # And we can send another command.
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    94
        request, action, meta = reactor.callcommand(b'command2', {})
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    95
        self.assertEqual(request.requestid, 3)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    96
        self.assertEqual(action, b'sendframes')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    97
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    98
        for frame in meta[b'framegen']:
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
    99
            self.assertEqual(request.state, b'sending')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   100
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   101
        self.assertEqual(request.state, b'sent')
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   102
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   103
class BadFrameRecvTests(unittest.TestCase):
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
   104
    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
   105
        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
   106
        # the regex version.
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
   107
        assertRaisesRegex = (# camelcase-required
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
   108
            unittest.TestCase.assertRaisesRegexp)
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
   109
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   110
    def testoddstream(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   111
        reactor = framing.clientreactor(globalui)
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   112
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   113
        action, meta = sendframe(reactor, ffs(b'1 1 0 1 0 foo'))
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   114
        self.assertEqual(action, b'error')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   115
        self.assertEqual(meta[b'message'],
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   116
                         b'received frame with odd numbered stream ID: 1')
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   117
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   118
    def testunknownstream(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   119
        reactor = framing.clientreactor(globalui)
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   120
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   121
        action, meta = sendframe(reactor, ffs(b'1 0 0 1 0 foo'))
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   122
        self.assertEqual(action, b'error')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   123
        self.assertEqual(meta[b'message'],
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   124
                         b'received frame on unknown stream without beginning '
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   125
                         b'of stream flag set')
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   126
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   127
    def testunhandledframetype(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   128
        reactor = framing.clientreactor(globalui, buffersends=False)
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   129
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   130
        request, action, meta = reactor.callcommand(b'foo', {})
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   131
        for frame in meta[b'framegen']:
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   132
            pass
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   133
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37682
diff changeset
   134
        with self.assertRaisesRegex(error.ProgrammingError,
37544
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   135
                                     'unhandled frame type'):
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   136
            sendframe(reactor, ffs(b'1 0 stream-begin text-output 0 foo'))
55b5ba8d4e68 wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37543
diff changeset
   137
37656
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   138
class StreamTests(unittest.TestCase):
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   139
    def testmultipleresponseframes(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   140
        reactor = framing.clientreactor(globalui, buffersends=False)
37656
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   141
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   142
        request, action, meta = reactor.callcommand(b'foo', {})
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   143
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   144
        self.assertEqual(action, b'sendframes')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   145
        for f in meta[b'framegen']:
37656
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   146
            pass
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   147
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   148
        action, meta = sendframe(
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   149
            reactor,
37724
deff7cf7eefd wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37715
diff changeset
   150
            ffs(b'%d 0 stream-begin command-response 0 foo' %
deff7cf7eefd wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37715
diff changeset
   151
                request.requestid))
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   152
        self.assertEqual(action, b'responsedata')
37656
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   153
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   154
        action, meta = sendframe(
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   155
            reactor,
37724
deff7cf7eefd wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37715
diff changeset
   156
            ffs(b'%d 0 0 command-response eos bar' % request.requestid))
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37656
diff changeset
   157
        self.assertEqual(action, b'responsedata')
37656
e6870bca1f47 wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
   158
40024
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   159
class RedirectTests(unittest.TestCase):
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   160
    def testredirect(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   161
        reactor = framing.clientreactor(globalui, buffersends=False)
40024
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   162
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   163
        redirect = {
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   164
            b'targets': [b'a', b'b'],
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   165
            b'hashes': [b'sha256'],
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   166
        }
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   167
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   168
        request, action, meta = reactor.callcommand(
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   169
            b'foo', {}, redirect=redirect)
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   170
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   171
        self.assertEqual(action, b'sendframes')
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   172
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   173
        frames = list(meta[b'framegen'])
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   174
        self.assertEqual(len(frames), 1)
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   175
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   176
        self.assertEqual(frames[0],
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   177
                         ffs(b'1 1 stream-begin command-request new '
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   178
                             b"cbor:{b'name': b'foo', "
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   179
                             b"b'redirect': {b'targets': [b'a', b'b'], "
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   180
                             b"b'hashes': [b'sha256']}}"))
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
   181
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   182
class StreamSettingsTests(unittest.TestCase):
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   183
    def testnoflags(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   184
        reactor = framing.clientreactor(globalui, buffersends=False)
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   185
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   186
        request, action, meta = reactor.callcommand(b'foo', {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   187
        for f in meta[b'framegen']:
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   188
            pass
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   189
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   190
        action, meta = sendframe(reactor,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   191
            ffs(b'1 2 stream-begin stream-settings 0 '))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   192
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   193
        self.assertEqual(action, b'error')
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   194
        self.assertEqual(meta, {
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   195
            b'message': b'stream encoding settings frame must have '
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   196
                        b'continuation or end of stream flag set',
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   197
        })
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   198
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   199
    def testconflictflags(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   200
        reactor = framing.clientreactor(globalui, buffersends=False)
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   201
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   202
        request, action, meta = reactor.callcommand(b'foo', {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   203
        for f in meta[b'framegen']:
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   204
            pass
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   205
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   206
        action, meta = sendframe(reactor,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   207
            ffs(b'1 2 stream-begin stream-settings continuation|eos '))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   208
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   209
        self.assertEqual(action, b'error')
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   210
        self.assertEqual(meta, {
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   211
            b'message': b'stream encoding settings frame cannot have both '
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   212
                        b'continuation and end of stream flags set',
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   213
        })
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   214
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   215
    def testemptypayload(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   216
        reactor = framing.clientreactor(globalui, buffersends=False)
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   217
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   218
        request, action, meta = reactor.callcommand(b'foo', {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   219
        for f in meta[b'framegen']:
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   220
            pass
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   221
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   222
        action, meta = sendframe(reactor,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   223
            ffs(b'1 2 stream-begin stream-settings eos '))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   224
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   225
        self.assertEqual(action, b'error')
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   226
        self.assertEqual(meta, {
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   227
            b'message': b'stream encoding settings frame did not contain '
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   228
                        b'CBOR data'
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   229
        })
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   230
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   231
    def testbadcbor(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   232
        reactor = framing.clientreactor(globalui, buffersends=False)
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   233
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   234
        request, action, meta = reactor.callcommand(b'foo', {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   235
        for f in meta[b'framegen']:
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   236
            pass
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   237
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   238
        action, meta = sendframe(reactor,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   239
            ffs(b'1 2 stream-begin stream-settings eos badvalue'))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   240
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   241
        self.assertEqual(action, b'error')
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   242
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   243
    def testsingleobject(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   244
        reactor = framing.clientreactor(globalui, buffersends=False)
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   245
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   246
        request, action, meta = reactor.callcommand(b'foo', {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   247
        for f in meta[b'framegen']:
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   248
            pass
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   249
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   250
        action, meta = sendframe(reactor,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   251
            ffs(b'1 2 stream-begin stream-settings eos cbor:b"identity"'))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   252
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   253
        self.assertEqual(action, b'noop')
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   254
        self.assertEqual(meta, {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   255
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   256
    def testmultipleobjects(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   257
        reactor = framing.clientreactor(globalui, buffersends=False)
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   258
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   259
        request, action, meta = reactor.callcommand(b'foo', {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   260
        for f in meta[b'framegen']:
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   261
            pass
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   262
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   263
        data = b''.join([
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   264
            b''.join(cborutil.streamencode(b'identity')),
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   265
            b''.join(cborutil.streamencode({b'foo', b'bar'})),
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   266
        ])
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   267
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   268
        action, meta = sendframe(reactor,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   269
            ffs(b'1 2 stream-begin stream-settings eos %s' % data))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   270
40132
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   271
        self.assertEqual(action, b'error')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   272
        self.assertEqual(meta, {
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   273
            b'message': b'error setting stream decoder: identity decoder '
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   274
                        b'received unexpected additional values',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   275
        })
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   276
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   277
    def testmultipleframes(self):
40129
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40128
diff changeset
   278
        reactor = framing.clientreactor(globalui, buffersends=False)
40128
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   279
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   280
        request, action, meta = reactor.callcommand(b'foo', {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   281
        for f in meta[b'framegen']:
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   282
            pass
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   283
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   284
        data = b''.join(cborutil.streamencode(b'identity'))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   285
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   286
        action, meta = sendframe(reactor,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   287
            ffs(b'1 2 stream-begin stream-settings continuation %s' %
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   288
                data[0:3]))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   289
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   290
        self.assertEqual(action, b'noop')
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   291
        self.assertEqual(meta, {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   292
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   293
        action, meta = sendframe(reactor,
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   294
            ffs(b'1 2 0 stream-settings eos %s' % data[3:]))
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   295
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   296
        self.assertEqual(action, b'noop')
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   297
        self.assertEqual(meta, {})
57782791b7e9 wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40024
diff changeset
   298
40132
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   299
    def testinvalidencoder(self):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   300
        reactor = framing.clientreactor(globalui, buffersends=False)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   301
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   302
        request, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   303
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   304
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   305
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   306
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   307
            ffs(b'1 2 stream-begin stream-settings eos cbor:b"badvalue"'))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   308
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   309
        self.assertEqual(action, b'error')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   310
        self.assertEqual(meta, {
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   311
            b'message': b'error setting stream decoder: unknown stream '
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   312
                        b'decoder: badvalue',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   313
        })
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   314
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   315
    def testzlibencoding(self):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   316
        reactor = framing.clientreactor(globalui, buffersends=False)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   317
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   318
        request, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   319
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   320
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   321
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   322
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   323
            ffs(b'%d 2 stream-begin stream-settings eos cbor:b"zlib"' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   324
                request.requestid))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   325
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   326
        self.assertEqual(action, b'noop')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   327
        self.assertEqual(meta, {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   328
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   329
        result = {
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   330
            b'status': b'ok',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   331
        }
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   332
        encoded = b''.join(cborutil.streamencode(result))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   333
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   334
        compressed = zlib.compress(encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   335
        self.assertEqual(zlib.decompress(compressed), encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   336
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   337
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   338
            ffs(b'%d 2 encoded command-response eos %s' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   339
                (request.requestid, compressed)))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   340
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   341
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   342
        self.assertEqual(meta[b'data'], encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   343
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   344
    def testzlibencodingsinglebyteframes(self):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   345
        reactor = framing.clientreactor(globalui, buffersends=False)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   346
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   347
        request, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   348
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   349
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   350
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   351
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   352
            ffs(b'%d 2 stream-begin stream-settings eos cbor:b"zlib"' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   353
                request.requestid))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   354
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   355
        self.assertEqual(action, b'noop')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   356
        self.assertEqual(meta, {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   357
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   358
        result = {
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   359
            b'status': b'ok',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   360
        }
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   361
        encoded = b''.join(cborutil.streamencode(result))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   362
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   363
        compressed = zlib.compress(encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   364
        self.assertEqual(zlib.decompress(compressed), encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   365
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   366
        chunks = []
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   367
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   368
        for i in range(len(compressed)):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   369
            char = compressed[i:i + 1]
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   370
            if char == b'\\':
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   371
                char = b'\\\\'
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   372
            action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   373
                ffs(b'%d 2 encoded command-response continuation %s' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   374
                    (request.requestid, char)))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   375
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   376
            self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   377
            chunks.append(meta[b'data'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   378
            self.assertTrue(meta[b'expectmore'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   379
            self.assertFalse(meta[b'eos'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   380
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   381
        # zlib will have the full data decoded at this point, even though
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   382
        # we haven't flushed.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   383
        self.assertEqual(b''.join(chunks), encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   384
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   385
        # End the stream for good measure.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   386
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   387
            ffs(b'%d 2 stream-end command-response eos ' % request.requestid))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   388
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   389
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   390
        self.assertEqual(meta[b'data'], b'')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   391
        self.assertFalse(meta[b'expectmore'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   392
        self.assertTrue(meta[b'eos'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   393
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   394
    def testzlibmultipleresponses(self):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   395
        # We feed in zlib compressed data on the same stream but belonging to
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   396
        # 2 different requests. This tests our flushing behavior.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   397
        reactor = framing.clientreactor(globalui, buffersends=False,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   398
                                        hasmultiplesend=True)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   399
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   400
        request1, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   401
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   402
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   403
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   404
        request2, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   405
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   406
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   407
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   408
        outstream = framing.outputstream(2)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   409
        outstream.setencoder(globalui, b'zlib')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   410
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   411
        response1 = b''.join(cborutil.streamencode({
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   412
            b'status': b'ok',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   413
            b'extra': b'response1' * 10,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   414
        }))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   415
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   416
        response2 = b''.join(cborutil.streamencode({
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   417
            b'status': b'error',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   418
            b'extra': b'response2' * 10,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   419
        }))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   420
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   421
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   422
            ffs(b'%d 2 stream-begin stream-settings eos cbor:b"zlib"' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   423
                request1.requestid))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   424
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   425
        self.assertEqual(action, b'noop')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   426
        self.assertEqual(meta, {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   427
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   428
        # Feeding partial data in won't get anything useful out.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   429
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   430
            ffs(b'%d 2 encoded command-response continuation %s' % (
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   431
                request1.requestid, outstream.encode(response1))))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   432
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   433
        self.assertEqual(meta[b'data'], b'')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   434
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   435
        # But flushing data at both ends will get our original data.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   436
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   437
            ffs(b'%d 2 encoded command-response eos %s' % (
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   438
                request1.requestid, outstream.flush())))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   439
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   440
        self.assertEqual(meta[b'data'], response1)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   441
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   442
        # We should be able to reuse the compressor/decompressor for the
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   443
        # 2nd response.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   444
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   445
            ffs(b'%d 2 encoded command-response continuation %s' % (
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   446
                request2.requestid, outstream.encode(response2))))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   447
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   448
        self.assertEqual(meta[b'data'], b'')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   449
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   450
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   451
            ffs(b'%d 2 encoded command-response eos %s' % (
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   452
                request2.requestid, outstream.flush())))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   453
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   454
        self.assertEqual(meta[b'data'], response2)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   455
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   456
    @unittest.skipUnless(zstd, 'zstd not available')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   457
    def testzstd8mbencoding(self):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   458
        reactor = framing.clientreactor(globalui, buffersends=False)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   459
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   460
        request, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   461
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   462
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   463
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   464
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   465
            ffs(b'%d 2 stream-begin stream-settings eos cbor:b"zstd-8mb"' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   466
                request.requestid))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   467
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   468
        self.assertEqual(action, b'noop')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   469
        self.assertEqual(meta, {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   470
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   471
        result = {
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   472
            b'status': b'ok',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   473
        }
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   474
        encoded = b''.join(cborutil.streamencode(result))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   475
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   476
        encoder = framing.zstd8mbencoder(globalui)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   477
        compressed = encoder.encode(encoded) + encoder.finish()
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   478
        self.assertEqual(zstd.ZstdDecompressor().decompress(
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   479
            compressed, max_output_size=len(encoded)), encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   480
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   481
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   482
            ffs(b'%d 2 encoded command-response eos %s' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   483
                (request.requestid, compressed)))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   484
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   485
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   486
        self.assertEqual(meta[b'data'], encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   487
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   488
    @unittest.skipUnless(zstd, 'zstd not available')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   489
    def testzstd8mbencodingsinglebyteframes(self):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   490
        reactor = framing.clientreactor(globalui, buffersends=False)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   491
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   492
        request, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   493
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   494
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   495
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   496
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   497
            ffs(b'%d 2 stream-begin stream-settings eos cbor:b"zstd-8mb"' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   498
                request.requestid))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   499
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   500
        self.assertEqual(action, b'noop')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   501
        self.assertEqual(meta, {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   502
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   503
        result = {
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   504
            b'status': b'ok',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   505
        }
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   506
        encoded = b''.join(cborutil.streamencode(result))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   507
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   508
        compressed = zstd.ZstdCompressor().compress(encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   509
        self.assertEqual(zstd.ZstdDecompressor().decompress(compressed),
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   510
                         encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   511
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   512
        chunks = []
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   513
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   514
        for i in range(len(compressed)):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   515
            char = compressed[i:i + 1]
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   516
            if char == b'\\':
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   517
                char = b'\\\\'
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   518
            action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   519
                ffs(b'%d 2 encoded command-response continuation %s' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   520
                    (request.requestid, char)))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   521
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   522
            self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   523
            chunks.append(meta[b'data'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   524
            self.assertTrue(meta[b'expectmore'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   525
            self.assertFalse(meta[b'eos'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   526
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   527
        # zstd decompressor will flush at frame boundaries.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   528
        self.assertEqual(b''.join(chunks), encoded)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   529
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   530
        # End the stream for good measure.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   531
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   532
            ffs(b'%d 2 stream-end command-response eos ' % request.requestid))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   533
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   534
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   535
        self.assertEqual(meta[b'data'], b'')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   536
        self.assertFalse(meta[b'expectmore'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   537
        self.assertTrue(meta[b'eos'])
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   538
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   539
    @unittest.skipUnless(zstd, 'zstd not available')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   540
    def testzstd8mbmultipleresponses(self):
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   541
        # We feed in zstd compressed data on the same stream but belonging to
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   542
        # 2 different requests. This tests our flushing behavior.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   543
        reactor = framing.clientreactor(globalui, buffersends=False,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   544
                                        hasmultiplesend=True)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   545
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   546
        request1, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   547
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   548
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   549
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   550
        request2, action, meta = reactor.callcommand(b'foo', {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   551
        for f in meta[b'framegen']:
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   552
            pass
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   553
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   554
        outstream = framing.outputstream(2)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   555
        outstream.setencoder(globalui, b'zstd-8mb')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   556
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   557
        response1 = b''.join(cborutil.streamencode({
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   558
            b'status': b'ok',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   559
            b'extra': b'response1' * 10,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   560
        }))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   561
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   562
        response2 = b''.join(cborutil.streamencode({
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   563
            b'status': b'error',
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   564
            b'extra': b'response2' * 10,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   565
        }))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   566
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   567
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   568
            ffs(b'%d 2 stream-begin stream-settings eos cbor:b"zstd-8mb"' %
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   569
                request1.requestid))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   570
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   571
        self.assertEqual(action, b'noop')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   572
        self.assertEqual(meta, {})
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   573
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   574
        # Feeding partial data in won't get anything useful out.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   575
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   576
            ffs(b'%d 2 encoded command-response continuation %s' % (
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   577
                request1.requestid, outstream.encode(response1))))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   578
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   579
        self.assertEqual(meta[b'data'], b'')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   580
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   581
        # But flushing data at both ends will get our original data.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   582
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   583
            ffs(b'%d 2 encoded command-response eos %s' % (
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   584
                request1.requestid, outstream.flush())))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   585
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   586
        self.assertEqual(meta[b'data'], response1)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   587
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   588
        # We should be able to reuse the compressor/decompressor for the
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   589
        # 2nd response.
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   590
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   591
            ffs(b'%d 2 encoded command-response continuation %s' % (
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   592
                request2.requestid, outstream.encode(response2))))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   593
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   594
        self.assertEqual(meta[b'data'], b'')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   595
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   596
        action, meta = sendframe(reactor,
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   597
            ffs(b'%d 2 encoded command-response eos %s' % (
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   598
                request2.requestid, outstream.flush())))
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   599
        self.assertEqual(action, b'responsedata')
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   600
        self.assertEqual(meta[b'data'], response2)
e67522413ca8 wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   601
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   602
if __name__ == '__main__':
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   603
    import silenttestrunner
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   604
    silenttestrunner.main(__name__)