tests/test-wireproto-serverreactor.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 29 Oct 2019 11:07:25 +0100
branchstable
changeset 43348 daa3b58906d5
parent 43076 2372284d9457
child 45957 89a2afe31e82
permissions -rw-r--r--
formatting: drop `grey`, our custom black version Now that the official black has all we want, we can drop this.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
from __future__ import absolute_import, print_function
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
import unittest
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
from mercurial import (
40130
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40127
diff changeset
     6
    ui as uimod,
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
    util,
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
    wireprotoframing as framing,
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    10
from mercurial.utils import cborutil
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
ffs = framing.makeframefromhumanstring
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
41137
a181a1c8af1d tests: migrate test-wireproto-serverreactor.py to our internal CBOR
Augie Fackler <augie@google.com>
parents: 40138
diff changeset
    14
OK = b''.join(cborutil.streamencode({b'status': b'ok'}))
37725
3ea8323d6f95 wireprotov2: change command response protocol to include a leading map
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
    15
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    16
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
    17
def makereactor(deferoutput=False):
40130
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40127
diff changeset
    18
    ui = uimod.ui()
293835e0fff7 wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40127
diff changeset
    19
    return framing.serverreactor(ui, deferoutput=deferoutput)
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    21
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
def sendframes(reactor, gen):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
    """Send a generator of frame bytearray to a reactor.
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
    Emits a generator of results from ``onframerecv()`` calls.
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
    """
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
    for frame in gen:
37064
884a0c1604ad wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37063
diff changeset
    28
        header = framing.parseheader(frame)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    29
        payload = frame[framing.FRAME_HEADER_SIZE :]
37064
884a0c1604ad wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37063
diff changeset
    30
        assert len(payload) == header.length
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    32
        yield reactor.onframerecv(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    33
            framing.frame(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    34
                header.requestid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    35
                header.streamid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    36
                header.streamflags,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    37
                header.typeid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    38
                header.flags,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    39
                payload,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    40
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    41
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    42
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
    44
def sendcommandframes(reactor, stream, rid, cmd, args, datafh=None):
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
    """Generate frames to run a command and send them to a reactor."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    46
    return sendframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    47
        reactor, framing.createcommandframes(stream, rid, cmd, args, datafh)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    48
    )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
37063
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37061
diff changeset
    50
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
class ServerReactorTests(unittest.TestCase):
37286
12bfc724217d tests: fix duplicate and failing test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37066
diff changeset
    52
    def _sendsingleframe(self, reactor, f):
12bfc724217d tests: fix duplicate and failing test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37066
diff changeset
    53
        results = list(sendframes(reactor, [f]))
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
        self.assertEqual(len(results), 1)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
        return results[0]
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
    def assertaction(self, res, expected):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
        self.assertIsInstance(res, tuple)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
        self.assertEqual(len(res), 2)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
        self.assertIsInstance(res[1], dict)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
        self.assertEqual(res[0], expected)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
    64
    def assertframesequal(self, frames, framestrings):
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
    65
        expected = [ffs(s) for s in framestrings]
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
    66
        self.assertEqual(list(frames), expected)
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
    67
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
    def test1framecommand(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
        """Receiving a command in a single frame yields request to run it."""
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
    71
        stream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
    72
        results = list(sendcommandframes(reactor, stream, 1, b'mycommand', {}))
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
        self.assertEqual(len(results), 1)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
    74
        self.assertaction(results[0], b'runcommand')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    75
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    76
            results[0][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    77
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    78
                b'requestid': 1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    79
                b'command': b'mycommand',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    80
                b'args': {},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    81
                b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    82
                b'data': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    83
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    84
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    85
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
    86
        result = reactor.oninputeof()
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
    87
        self.assertaction(result, b'noop')
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
    88
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    89
    def test1argument(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    90
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
    91
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    92
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    93
            sendcommandframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    94
                reactor, stream, 41, b'mycommand', {b'foo': b'bar'}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    95
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    96
        )
37292
3d0e2cd86e05 wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37290
diff changeset
    97
        self.assertEqual(len(results), 1)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
    98
        self.assertaction(results[0], b'runcommand')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
    99
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   100
            results[0][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   101
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   102
                b'requestid': 41,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   103
                b'command': b'mycommand',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   104
                b'args': {b'foo': b'bar'},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   105
                b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   106
                b'data': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   107
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   108
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   109
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   110
    def testmultiarguments(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   111
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   112
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   113
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   114
            sendcommandframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   115
                reactor,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   116
                stream,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   117
                1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   118
                b'mycommand',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   119
                {b'foo': b'bar', b'biz': b'baz'},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   120
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   121
        )
37292
3d0e2cd86e05 wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37290
diff changeset
   122
        self.assertEqual(len(results), 1)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   123
        self.assertaction(results[0], b'runcommand')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   124
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   125
            results[0][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   126
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   127
                b'requestid': 1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   128
                b'command': b'mycommand',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   129
                b'args': {b'foo': b'bar', b'biz': b'baz'},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   130
                b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   131
                b'data': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   132
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   133
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   134
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   135
    def testsimplecommanddata(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   136
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   137
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   138
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   139
            sendcommandframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   140
                reactor, stream, 1, b'mycommand', {}, util.bytesio(b'data!')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   141
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   142
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   143
        self.assertEqual(len(results), 2)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   144
        self.assertaction(results[0], b'wantframe')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   145
        self.assertaction(results[1], b'runcommand')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   146
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   147
            results[1][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   148
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   149
                b'requestid': 1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   150
                b'command': b'mycommand',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   151
                b'args': {},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   152
                b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   153
                b'data': b'data!',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   154
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   155
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   156
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   157
    def testmultipledataframes(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   158
        frames = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   159
            ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   160
                b'1 1 stream-begin command-request new|have-data '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   161
                b"cbor:{b'name': b'mycommand'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   162
            ),
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   163
            ffs(b'1 1 0 command-data continuation data1'),
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   164
            ffs(b'1 1 0 command-data continuation data2'),
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   165
            ffs(b'1 1 0 command-data eos data3'),
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   166
        ]
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   167
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   168
        reactor = makereactor()
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   169
        results = list(sendframes(reactor, frames))
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   170
        self.assertEqual(len(results), 4)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   171
        for i in range(3):
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   172
            self.assertaction(results[i], b'wantframe')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   173
        self.assertaction(results[3], b'runcommand')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   174
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   175
            results[3][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   176
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   177
                b'requestid': 1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   178
                b'command': b'mycommand',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   179
                b'args': {},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   180
                b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   181
                b'data': b'data1data2data3',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   182
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   183
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   184
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   185
    def testargumentanddata(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   186
        frames = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   187
            ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   188
                b'1 1 stream-begin command-request new|have-data '
37292
3d0e2cd86e05 wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37290
diff changeset
   189
                b"cbor:{b'name': b'command', b'args': {b'key': b'val',"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   190
                b"b'foo': b'bar'}}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   191
            ),
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   192
            ffs(b'1 1 0 command-data continuation value1'),
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   193
            ffs(b'1 1 0 command-data eos value2'),
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   194
        ]
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   195
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   196
        reactor = makereactor()
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   197
        results = list(sendframes(reactor, frames))
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   198
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   199
        self.assertaction(results[-1], b'runcommand')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   200
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   201
            results[-1][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   202
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   203
                b'requestid': 1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   204
                b'command': b'command',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   205
                b'args': {b'key': b'val', b'foo': b'bar',},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   206
                b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   207
                b'data': b'value1value2',
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   208
            },
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   209
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   210
37292
3d0e2cd86e05 wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37290
diff changeset
   211
    def testnewandcontinuation(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   212
        result = self._sendsingleframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   213
            makereactor(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   214
            ffs(b'1 1 stream-begin command-request new|continuation '),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   215
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   216
        self.assertaction(result, b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   217
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   218
            result[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   219
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   220
                b'message': b'received command request frame with both new and '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   221
                b'continuation flags set',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   222
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   223
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   224
37292
3d0e2cd86e05 wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37290
diff changeset
   225
    def testneithernewnorcontinuation(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   226
        result = self._sendsingleframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   227
            makereactor(), ffs(b'1 1 stream-begin command-request 0 ')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   228
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   229
        self.assertaction(result, b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   230
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   231
            result[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   232
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   233
                b'message': b'received command request frame with neither new nor '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   234
                b'continuation flags set',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   235
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   236
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   237
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   238
    def testunexpectedcommanddata(self):
37292
3d0e2cd86e05 wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37290
diff changeset
   239
        """Command data frame when not running a command is an error."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   240
        result = self._sendsingleframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   241
            makereactor(), ffs(b'1 1 stream-begin command-data 0 ignored')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   242
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   243
        self.assertaction(result, b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   244
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   245
            result[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   246
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   247
                b'message': b'expected sender protocol settings or command request '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   248
                b'frame; got 2',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   249
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   250
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   251
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   252
    def testunexpectedcommanddatareceiving(self):
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   253
        """Same as above except the command is receiving."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   254
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   255
            sendframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   256
                makereactor(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   257
                [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   258
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   259
                        b'1 1 stream-begin command-request new|more '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   260
                        b"cbor:{b'name': b'ignored'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   261
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   262
                    ffs(b'1 1 0 command-data eos ignored'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   263
                ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   264
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   265
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   266
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   267
        self.assertaction(results[0], b'wantframe')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   268
        self.assertaction(results[1], b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   269
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   270
            results[1][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   271
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   272
                b'message': b'received command data frame for request that is not '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   273
                b'expecting data: 1',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   274
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   275
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   276
37286
12bfc724217d tests: fix duplicate and failing test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37066
diff changeset
   277
    def testconflictingrequestidallowed(self):
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   278
        """Multiple fully serviced commands with same request ID is allowed."""
37286
12bfc724217d tests: fix duplicate and failing test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37066
diff changeset
   279
        reactor = makereactor()
12bfc724217d tests: fix duplicate and failing test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37066
diff changeset
   280
        results = []
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   281
        outstream = reactor.makeoutputstream()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   282
        results.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   283
            self._sendsingleframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   284
                reactor,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   285
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   286
                    b'1 1 stream-begin command-request new '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   287
                    b"cbor:{b'name': b'command'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   288
                ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   289
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   290
        )
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   291
        result = reactor.oncommandresponsereadyobjects(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   292
            outstream, 1, [b'response1']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   293
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   294
        self.assertaction(result, b'sendframes')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   295
        list(result[1][b'framegen'])
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   296
        results.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   297
            self._sendsingleframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   298
                reactor,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   299
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   300
                    b'1 1 stream-begin command-request new '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   301
                    b"cbor:{b'name': b'command'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   302
                ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   303
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   304
        )
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   305
        result = reactor.oncommandresponsereadyobjects(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   306
            outstream, 1, [b'response2']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   307
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   308
        self.assertaction(result, b'sendframes')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   309
        list(result[1][b'framegen'])
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   310
        results.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   311
            self._sendsingleframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   312
                reactor,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   313
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   314
                    b'1 1 stream-begin command-request new '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   315
                    b"cbor:{b'name': b'command'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   316
                ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   317
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   318
        )
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   319
        result = reactor.oncommandresponsereadyobjects(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   320
            outstream, 1, [b'response3']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   321
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   322
        self.assertaction(result, b'sendframes')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   323
        list(result[1][b'framegen'])
37286
12bfc724217d tests: fix duplicate and failing test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37066
diff changeset
   324
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   325
        for i in range(3):
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   326
            self.assertaction(results[i], b'runcommand')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   327
            self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   328
                results[i][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   329
                {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   330
                    b'requestid': 1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   331
                    b'command': b'command',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   332
                    b'args': {},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   333
                    b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   334
                    b'data': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   335
                },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   336
            )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   337
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   338
    def testconflictingrequestid(self):
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   339
        """Request ID for new command matching in-flight command is illegal."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   340
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   341
            sendframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   342
                makereactor(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   343
                [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   344
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   345
                        b'1 1 stream-begin command-request new|more '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   346
                        b"cbor:{b'name': b'command'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   347
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   348
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   349
                        b'1 1 0 command-request new '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   350
                        b"cbor:{b'name': b'command1'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   351
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   352
                ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   353
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   354
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   355
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   356
        self.assertaction(results[0], b'wantframe')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   357
        self.assertaction(results[1], b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   358
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   359
            results[1][1], {b'message': b'request with ID 1 already received',}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   360
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   361
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   362
    def testinterleavedcommands(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   363
        cbor1 = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   364
            cborutil.streamencode(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   365
                {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   366
                    b'name': b'command1',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   367
                    b'args': {b'foo': b'bar', b'key1': b'val',},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   368
                }
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   369
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   370
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   371
        cbor3 = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   372
            cborutil.streamencode(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   373
                {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   374
                    b'name': b'command3',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   375
                    b'args': {b'biz': b'baz', b'key': b'val',},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   376
                }
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   377
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   378
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   379
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   380
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   381
            sendframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   382
                makereactor(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   383
                [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   384
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   385
                        b'1 1 stream-begin command-request new|more %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   386
                        % cbor1[0:6]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   387
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   388
                    ffs(b'3 1 0 command-request new|more %s' % cbor3[0:10]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   389
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   390
                        b'1 1 0 command-request continuation|more %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   391
                        % cbor1[6:9]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   392
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   393
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   394
                        b'3 1 0 command-request continuation|more %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   395
                        % cbor3[10:13]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   396
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   397
                    ffs(b'3 1 0 command-request continuation %s' % cbor3[13:]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   398
                    ffs(b'1 1 0 command-request continuation %s' % cbor1[9:]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   399
                ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   400
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   401
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   402
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   403
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   404
            [t[0] for t in results],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   405
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   406
                b'wantframe',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   407
                b'wantframe',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   408
                b'wantframe',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   409
                b'wantframe',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   410
                b'runcommand',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   411
                b'runcommand',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   412
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   413
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   414
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   415
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   416
            results[4][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   417
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   418
                b'requestid': 3,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   419
                b'command': b'command3',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   420
                b'args': {b'biz': b'baz', b'key': b'val'},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   421
                b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   422
                b'data': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   423
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   424
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   425
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   426
            results[5][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   427
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   428
                b'requestid': 1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   429
                b'command': b'command1',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   430
                b'args': {b'foo': b'bar', b'key1': b'val'},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   431
                b'redirect': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   432
                b'data': None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   433
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   434
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   435
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   436
    def testmissingcommanddataframe(self):
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   437
        # The reactor doesn't currently handle partially received commands.
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   438
        # So this test is failing to do anything with request 1.
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   439
        frames = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   440
            ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   441
                b'1 1 stream-begin command-request new|have-data '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   442
                b"cbor:{b'name': b'command1'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   443
            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   444
            ffs(b'3 1 0 command-request new ' b"cbor:{b'name': b'command2'}"),
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   445
        ]
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   446
        results = list(sendframes(makereactor(), frames))
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   447
        self.assertEqual(len(results), 2)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   448
        self.assertaction(results[0], b'wantframe')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   449
        self.assertaction(results[1], b'runcommand')
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   450
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   451
    def testmissingcommanddataframeflags(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   452
        frames = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   453
            ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   454
                b'1 1 stream-begin command-request new|have-data '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   455
                b"cbor:{b'name': b'command1'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   456
            ),
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   457
            ffs(b'1 1 0 command-data 0 data'),
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   458
        ]
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   459
        results = list(sendframes(makereactor(), frames))
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   460
        self.assertEqual(len(results), 2)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   461
        self.assertaction(results[0], b'wantframe')
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   462
        self.assertaction(results[1], b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   463
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   464
            results[1][1], {b'message': b'command data frame without flags',}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   465
        )
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   466
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   467
    def testframefornonreceivingrequest(self):
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   468
        """Receiving a frame for a command that is not receiving is illegal."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   469
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   470
            sendframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   471
                makereactor(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   472
                [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   473
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   474
                        b'1 1 stream-begin command-request new '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   475
                        b"cbor:{b'name': b'command1'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   476
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   477
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   478
                        b'3 1 0 command-request new|have-data '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   479
                        b"cbor:{b'name': b'command3'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   480
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   481
                    ffs(b'5 1 0 command-data eos ignored'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   482
                ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   483
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   484
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   485
        self.assertaction(results[2], b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   486
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   487
            results[2][1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   488
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   489
                b'message': b'received frame for request that is not receiving: 5',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   490
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   491
        )
37061
c5e9c3b47366 wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37060
diff changeset
   492
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   493
    def testsimpleresponse(self):
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   494
        """Bytes response to command sends result frames."""
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   495
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   496
        instream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   497
        list(sendcommandframes(reactor, instream, 1, b'mycommand', {}))
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   498
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   499
        outstream = reactor.makeoutputstream()
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   500
        result = reactor.oncommandresponsereadyobjects(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   501
            outstream, 1, [b'response']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   502
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   503
        self.assertaction(result, b'sendframes')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   504
        self.assertframesequal(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   505
            result[1][b'framegen'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   506
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   507
                b'1 2 stream-begin stream-settings eos cbor:b"identity"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   508
                b'1 2 encoded command-response continuation %s' % OK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   509
                b'1 2 encoded command-response continuation cbor:b"response"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   510
                b'1 2 0 command-response eos ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   511
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   512
        )
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   513
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   514
    def testmultiframeresponse(self):
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   515
        """Bytes response spanning multiple frames is handled."""
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   516
        first = b'x' * framing.DEFAULT_MAX_FRAME_SIZE
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   517
        second = b'y' * 100
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   518
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   519
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   520
        instream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   521
        list(sendcommandframes(reactor, instream, 1, b'mycommand', {}))
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   522
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   523
        outstream = reactor.makeoutputstream()
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   524
        result = reactor.oncommandresponsereadyobjects(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   525
            outstream, 1, [first + second]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   526
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   527
        self.assertaction(result, b'sendframes')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   528
        self.assertframesequal(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   529
            result[1][b'framegen'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   530
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   531
                b'1 2 stream-begin stream-settings eos cbor:b"identity"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   532
                b'1 2 encoded command-response continuation %s' % OK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   533
                b'1 2 encoded command-response continuation Y\x80d',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   534
                b'1 2 encoded command-response continuation %s' % first,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   535
                b'1 2 encoded command-response continuation %s' % second,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   536
                b'1 2 0 command-response eos ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   537
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   538
        )
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   539
37726
0c184ca594bb wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
   540
    def testservererror(self):
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   541
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   542
        instream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   543
        list(sendcommandframes(reactor, instream, 1, b'mycommand', {}))
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   544
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   545
        outstream = reactor.makeoutputstream()
37726
0c184ca594bb wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
   546
        result = reactor.onservererror(outstream, 1, b'some message')
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   547
        self.assertaction(result, b'sendframes')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   548
        self.assertframesequal(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   549
            result[1][b'framegen'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   550
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   551
                b"1 2 stream-begin error-response 0 "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   552
                b"cbor:{b'type': b'server', "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   553
                b"b'message': [{b'msg': b'some message'}]}",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   554
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   555
        )
37058
61393f888dfe wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37055
diff changeset
   556
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   557
    def test1commanddeferresponse(self):
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   558
        """Responses when in deferred output mode are delayed until EOF."""
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   559
        reactor = makereactor(deferoutput=True)
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   560
        instream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   561
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   562
            sendcommandframes(reactor, instream, 1, b'mycommand', {})
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   563
        )
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   564
        self.assertEqual(len(results), 1)
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   565
        self.assertaction(results[0], b'runcommand')
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   566
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   567
        outstream = reactor.makeoutputstream()
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   568
        result = reactor.oncommandresponsereadyobjects(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   569
            outstream, 1, [b'response']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   570
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   571
        self.assertaction(result, b'noop')
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   572
        result = reactor.oninputeof()
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   573
        self.assertaction(result, b'sendframes')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   574
        self.assertframesequal(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   575
            result[1][b'framegen'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   576
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   577
                b'1 2 stream-begin stream-settings eos cbor:b"identity"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   578
                b'1 2 encoded command-response continuation %s' % OK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   579
                b'1 2 encoded command-response continuation cbor:b"response"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   580
                b'1 2 0 command-response eos ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   581
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   582
        )
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   583
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   584
    def testmultiplecommanddeferresponse(self):
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   585
        reactor = makereactor(deferoutput=True)
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   586
        instream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   587
        list(sendcommandframes(reactor, instream, 1, b'command1', {}))
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   588
        list(sendcommandframes(reactor, instream, 3, b'command2', {}))
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   589
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   590
        outstream = reactor.makeoutputstream()
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   591
        result = reactor.oncommandresponsereadyobjects(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   592
            outstream, 1, [b'response1']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   593
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   594
        self.assertaction(result, b'noop')
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   595
        result = reactor.oncommandresponsereadyobjects(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   596
            outstream, 3, [b'response2']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   597
        )
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   598
        self.assertaction(result, b'noop')
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   599
        result = reactor.oninputeof()
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   600
        self.assertaction(result, b'sendframes')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   601
        self.assertframesequal(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   602
            result[1][b'framegen'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   603
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   604
                b'1 2 stream-begin stream-settings eos cbor:b"identity"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   605
                b'1 2 encoded command-response continuation %s' % OK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   606
                b'1 2 encoded command-response continuation cbor:b"response1"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   607
                b'1 2 0 command-response eos ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   608
                b'3 2 encoded command-response continuation %s' % OK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   609
                b'3 2 encoded command-response continuation cbor:b"response2"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   610
                b'3 2 0 command-response eos ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   611
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   612
        )
37060
2ec1fb9de638 wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37059
diff changeset
   613
2ec1fb9de638 wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37059
diff changeset
   614
    def testrequestidtracking(self):
2ec1fb9de638 wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37059
diff changeset
   615
        reactor = makereactor(deferoutput=True)
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   616
        instream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   617
        list(sendcommandframes(reactor, instream, 1, b'command1', {}))
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   618
        list(sendcommandframes(reactor, instream, 3, b'command2', {}))
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   619
        list(sendcommandframes(reactor, instream, 5, b'command3', {}))
37060
2ec1fb9de638 wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37059
diff changeset
   620
2ec1fb9de638 wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37059
diff changeset
   621
        # Register results for commands out of order.
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   622
        outstream = reactor.makeoutputstream()
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   623
        reactor.oncommandresponsereadyobjects(outstream, 3, [b'response3'])
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   624
        reactor.oncommandresponsereadyobjects(outstream, 1, [b'response1'])
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   625
        reactor.oncommandresponsereadyobjects(outstream, 5, [b'response5'])
37060
2ec1fb9de638 wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37059
diff changeset
   626
2ec1fb9de638 wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37059
diff changeset
   627
        result = reactor.oninputeof()
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   628
        self.assertaction(result, b'sendframes')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   629
        self.assertframesequal(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   630
            result[1][b'framegen'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   631
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   632
                b'3 2 stream-begin stream-settings eos cbor:b"identity"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   633
                b'3 2 encoded command-response continuation %s' % OK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   634
                b'3 2 encoded command-response continuation cbor:b"response3"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   635
                b'3 2 0 command-response eos ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   636
                b'1 2 encoded command-response continuation %s' % OK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   637
                b'1 2 encoded command-response continuation cbor:b"response1"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   638
                b'1 2 0 command-response eos ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   639
                b'5 2 encoded command-response continuation %s' % OK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   640
                b'5 2 encoded command-response continuation cbor:b"response5"',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   641
                b'5 2 0 command-response eos ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   642
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   643
        )
37059
861e9d37e56e wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   644
37066
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   645
    def testduplicaterequestonactivecommand(self):
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   646
        """Receiving a request ID that matches a request that isn't finished."""
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   647
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   648
        stream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   649
        list(sendcommandframes(reactor, stream, 1, b'command1', {}))
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   650
        results = list(sendcommandframes(reactor, stream, 1, b'command1', {}))
37066
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   651
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   652
        self.assertaction(results[0], b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   653
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   654
            results[0][1], {b'message': b'request with ID 1 is already active',}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   655
        )
37066
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   656
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   657
    def testduplicaterequestonactivecommandnosend(self):
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   658
        """Same as above but we've registered a response but haven't sent it."""
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   659
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   660
        instream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   661
        list(sendcommandframes(reactor, instream, 1, b'command1', {}))
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   662
        outstream = reactor.makeoutputstream()
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   663
        reactor.oncommandresponsereadyobjects(outstream, 1, [b'response'])
37066
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   664
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   665
        # We've registered the response but haven't sent it. From the
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   666
        # perspective of the reactor, the command is still active.
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   667
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   668
        results = list(sendcommandframes(reactor, instream, 1, b'command1', {}))
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   669
        self.assertaction(results[0], b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   670
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   671
            results[0][1], {b'message': b'request with ID 1 is already active',}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   672
        )
37066
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   673
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   674
    def testduplicaterequestaftersend(self):
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   675
        """We can use a duplicate request ID after we've sent the response."""
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   676
        reactor = makereactor()
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37287
diff changeset
   677
        instream = framing.stream(1)
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   678
        list(sendcommandframes(reactor, instream, 1, b'command1', {}))
37289
5fadc63ac99f wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37288
diff changeset
   679
        outstream = reactor.makeoutputstream()
40135
966b5f7fd30b wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40130
diff changeset
   680
        res = reactor.oncommandresponsereadyobjects(outstream, 1, [b'response'])
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   681
        list(res[1][b'framegen'])
37066
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   682
37287
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37286
diff changeset
   683
        results = list(sendcommandframes(reactor, instream, 1, b'command1', {}))
37682
cb71e0f9ac6f tests: add all missing b prefixes in reactor tests
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   684
        self.assertaction(results[0], b'runcommand')
37066
39304dd63589 wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37064
diff changeset
   685
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   686
    def testprotocolsettingsnoflags(self):
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   687
        result = self._sendsingleframe(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   688
            makereactor(), ffs(b'0 1 stream-begin sender-protocol-settings 0 ')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   689
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   690
        self.assertaction(result, b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   691
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   692
            result[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   693
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   694
                b'message': b'sender protocol settings frame must have '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   695
                b'continuation or end of stream flag set',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   696
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   697
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   698
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   699
    def testprotocolsettingsconflictflags(self):
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   700
        result = self._sendsingleframe(
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   701
            makereactor(),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   702
            ffs(b'0 1 stream-begin sender-protocol-settings continuation|eos '),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   703
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   704
        self.assertaction(result, b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   705
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   706
            result[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   707
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   708
                b'message': b'sender protocol settings frame cannot have both '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   709
                b'continuation and end of stream flags set',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   710
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   711
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   712
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   713
    def testprotocolsettingsemptypayload(self):
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   714
        result = self._sendsingleframe(
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   715
            makereactor(),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   716
            ffs(b'0 1 stream-begin sender-protocol-settings eos '),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   717
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   718
        self.assertaction(result, b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   719
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   720
            result[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   721
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   722
                b'message': b'sender protocol settings frame did not contain CBOR '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   723
                b'data',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   724
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   725
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   726
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   727
    def testprotocolsettingsmultipleobjects(self):
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   728
        result = self._sendsingleframe(
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   729
            makereactor(),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   730
            ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   731
                b'0 1 stream-begin sender-protocol-settings eos '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   732
                b'\x46foobar\x43foo'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   733
            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   734
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   735
        self.assertaction(result, b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   736
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   737
            result[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   738
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   739
                b'message': b'sender protocol settings frame contained multiple '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   740
                b'CBOR values',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   741
            },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   742
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   743
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   744
    def testprotocolsettingscontentencodings(self):
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   745
        reactor = makereactor()
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   746
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   747
        result = self._sendsingleframe(
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   748
            reactor,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   749
            ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   750
                b'0 1 stream-begin sender-protocol-settings eos '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   751
                b'cbor:{b"contentencodings": [b"a", b"b"]}'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   752
            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   753
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   754
        self.assertaction(result, b'wantframe')
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   755
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   756
        self.assertEqual(reactor._state, b'idle')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   757
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   758
            reactor._sendersettings[b'contentencodings'], [b'a', b'b']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   759
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   760
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   761
    def testprotocolsettingsmultipleframes(self):
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   762
        reactor = makereactor()
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   763
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   764
        data = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   765
            cborutil.streamencode(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   766
                {b'contentencodings': [b'value1', b'value2'],}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   767
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   768
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   769
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   770
        results = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   771
            sendframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   772
                reactor,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   773
                [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   774
                    ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   775
                        b'0 1 stream-begin sender-protocol-settings continuation %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   776
                        % data[0:5]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   777
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   778
                    ffs(b'0 1 0 sender-protocol-settings eos %s' % data[5:]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   779
                ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   780
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   781
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   782
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   783
        self.assertEqual(len(results), 2)
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   784
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   785
        self.assertaction(results[0], b'wantframe')
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   786
        self.assertaction(results[1], b'wantframe')
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   787
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   788
        self.assertEqual(reactor._state, b'idle')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   789
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   790
            reactor._sendersettings[b'contentencodings'], [b'value1', b'value2']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   791
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   792
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   793
    def testprotocolsettingsbadcbor(self):
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   794
        result = self._sendsingleframe(
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   795
            makereactor(),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   796
            ffs(b'0 1 stream-begin sender-protocol-settings eos badvalue'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   797
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   798
        self.assertaction(result, b'error')
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   799
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   800
    def testprotocolsettingsnoninitial(self):
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   801
        # Cannot have protocol settings frames as non-initial frames.
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   802
        reactor = makereactor()
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   803
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   804
        stream = framing.stream(1)
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   805
        results = list(sendcommandframes(reactor, stream, 1, b'mycommand', {}))
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   806
        self.assertEqual(len(results), 1)
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   807
        self.assertaction(results[0], b'runcommand')
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   808
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   809
        result = self._sendsingleframe(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   810
            reactor, ffs(b'0 1 0 sender-protocol-settings eos ')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   811
        )
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   812
        self.assertaction(result, b'error')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   813
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   814
            result[1], {b'message': b'expected command request frame; got 8',}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   815
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   816
40127
327d40b94bed wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40026
diff changeset
   817
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   818
if __name__ == '__main__':
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   819
    import silenttestrunner
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41137
diff changeset
   820
37055
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   821
    silenttestrunner.main(__name__)