tests/test-wireproto-framing.py
author Mads Kiilerich <mads@kiilerich.com>
Mon, 14 Oct 2019 00:09:25 +0200
changeset 43200 a819b5777727
parent 43076 2372284d9457
child 45942 89a2afe31e82
permissions -rw-r--r--
eol: test-eol-update.t coverage around update --clean using filters ... badly This will reveal problems and track their fixes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37052
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 (
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
    util,
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
    wireprotoframing as framing,
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
ffs = framing.makeframefromhumanstring
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    12
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    13
class FrameHumanStringTests(unittest.TestCase):
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    14
    def testbasic(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    15
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    16
            ffs(b'1 1 0 1 0 '), b'\x00\x00\x00\x01\x00\x01\x00\x10'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    17
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    18
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    19
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    20
            ffs(b'2 4 0 1 0 '), b'\x00\x00\x00\x02\x00\x04\x00\x10'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    21
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    22
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    23
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    24
            ffs(b'2 4 0 1 0 foo'), b'\x03\x00\x00\x02\x00\x04\x00\x10foo'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    25
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    26
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    27
    def testcborint(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    28
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    29
            ffs(b'1 1 0 1 0 cbor:15'), b'\x01\x00\x00\x01\x00\x01\x00\x10\x0f'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    30
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    31
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    32
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    33
            ffs(b'1 1 0 1 0 cbor:42'), b'\x02\x00\x00\x01\x00\x01\x00\x10\x18*'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    34
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    35
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    36
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    37
            ffs(b'1 1 0 1 0 cbor:1048576'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    38
            b'\x05\x00\x00\x01\x00\x01\x00\x10\x1a' b'\x00\x10\x00\x00',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    39
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    40
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    41
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    42
            ffs(b'1 1 0 1 0 cbor:0'), b'\x01\x00\x00\x01\x00\x01\x00\x10\x00'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    43
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    44
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    45
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    46
            ffs(b'1 1 0 1 0 cbor:-1'), b'\x01\x00\x00\x01\x00\x01\x00\x10 '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    47
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    48
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    49
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    50
            ffs(b'1 1 0 1 0 cbor:-342542'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    51
            b'\x05\x00\x00\x01\x00\x01\x00\x10:\x00\x05:\r',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    52
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    53
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    54
    def testcborstrings(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    55
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    56
            ffs(b"1 1 0 1 0 cbor:b'foo'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    57
            b'\x04\x00\x00\x01\x00\x01\x00\x10Cfoo',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    58
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    59
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    60
    def testcborlists(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    61
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    62
            ffs(b"1 1 0 1 0 cbor:[None, True, False, 42, b'foo']"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    63
            b'\n\x00\x00\x01\x00\x01\x00\x10\x85\xf6\xf5\xf4' b'\x18*Cfoo',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    64
        )
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    65
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    66
    def testcbordicts(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    67
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    68
            ffs(b"1 1 0 1 0 " b"cbor:{b'foo': b'val1', b'bar': b'val2'}"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    69
            b'\x13\x00\x00\x01\x00\x01\x00\x10\xa2' b'CbarDval2CfooDval1',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    70
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    71
37290
cc5a040fe150 wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37289
diff changeset
    72
37052
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
class FrameTests(unittest.TestCase):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
    def testdataexactframesize(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    75
        data = util.bytesio(b'x' * framing.DEFAULT_MAX_FRAME_SIZE)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
    77
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    78
        frames = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    79
            framing.createcommandframes(stream, 1, b'command', {}, data)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    80
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    81
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    82
            frames,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    83
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    84
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    85
                    b'1 1 stream-begin command-request new|have-data '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    86
                    b"cbor:{b'name': b'command'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    87
                ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    88
                ffs(b'1 1 0 command-data continuation %s' % data.getvalue()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    89
                ffs(b'1 1 0 command-data eos '),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    90
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    91
        )
37052
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    92
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    93
    def testdatamultipleframes(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    94
        data = util.bytesio(b'x' * (framing.DEFAULT_MAX_FRAME_SIZE + 1))
37285
3ed344546d9e wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37284
diff changeset
    95
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
    96
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    97
        frames = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    98
            framing.createcommandframes(stream, 1, b'command', {}, data)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
    99
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   100
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   101
            frames,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   102
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   103
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   104
                    b'1 1 stream-begin command-request new|have-data '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   105
                    b"cbor:{b'name': b'command'}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   106
                ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   107
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   108
                    b'1 1 0 command-data continuation %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   109
                    % (b'x' * framing.DEFAULT_MAX_FRAME_SIZE)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   110
                ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   111
                ffs(b'1 1 0 command-data eos x'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   112
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   113
        )
37052
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   114
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   115
    def testargsanddata(self):
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   116
        data = util.bytesio(b'x' * 100)
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   117
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
   118
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   119
        frames = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   120
            framing.createcommandframes(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   121
                stream,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   122
                1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   123
                b'command',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   124
                {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   125
                    b'key1': b'key1value',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   126
                    b'key2': b'key2value',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   127
                    b'key3': b'key3value',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   128
                },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   129
                data,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   130
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   131
        )
37052
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   132
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   133
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   134
            frames,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   135
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   136
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   137
                    b'1 1 stream-begin command-request new|have-data '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   138
                    b"cbor:{b'name': b'command', b'args': {b'key1': b'key1value', "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   139
                    b"b'key2': b'key2value', b'key3': b'key3value'}}"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   140
                ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   141
                ffs(b'1 1 0 command-data eos %s' % data.getvalue()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   142
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   143
        )
37052
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   144
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   145
    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   146
        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   147
        # the regex version.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   148
        assertRaisesRegex = (  # camelcase-required
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   149
            unittest.TestCase.assertRaisesRegexp
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   150
        )
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   151
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   152
    def testtextoutputformattingstringtype(self):
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   153
        """Formatting string must be bytes."""
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   154
        with self.assertRaisesRegex(ValueError, 'must use bytes formatting '):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   155
            list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   156
                framing.createtextoutputframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   157
                    None, 1, [(b'foo'.decode('ascii'), [], [])]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   158
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   159
            )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   160
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   161
    def testtextoutputargumentbytes(self):
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   162
        with self.assertRaisesRegex(ValueError, 'must use bytes for argument'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   163
            list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   164
                framing.createtextoutputframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   165
                    None, 1, [(b'foo', [b'foo'.decode('ascii')], [])]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   166
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   167
            )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   168
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   169
    def testtextoutputlabelbytes(self):
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 37542
diff changeset
   170
        with self.assertRaisesRegex(ValueError, 'must use bytes for labels'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   171
            list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   172
                framing.createtextoutputframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   173
                    None, 1, [(b'foo', [], [b'foo'.decode('ascii')])]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   174
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   175
            )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   176
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   177
    def testtextoutput1simpleatom(self):
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
   178
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   179
        val = list(framing.createtextoutputframe(stream, 1, [(b'foo', [], [])]))
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   180
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   181
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   182
            val,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   183
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   184
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   185
                    b'1 1 stream-begin text-output 0 '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   186
                    b"cbor:[{b'msg': b'foo'}]"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   187
                ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   188
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   189
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   190
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   191
    def testtextoutput2simpleatoms(self):
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
   192
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   193
        val = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   194
            framing.createtextoutputframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   195
                stream, 1, [(b'foo', [], []), (b'bar', [], []),]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   196
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   197
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   198
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   199
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   200
            val,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   201
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   202
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   203
                    b'1 1 stream-begin text-output 0 '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   204
                    b"cbor:[{b'msg': b'foo'}, {b'msg': b'bar'}]"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   205
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   206
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   207
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   208
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   209
    def testtextoutput1arg(self):
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
   210
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   211
        val = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   212
            framing.createtextoutputframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   213
                stream, 1, [(b'foo %s', [b'val1'], []),]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   214
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   215
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   216
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   217
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   218
            val,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   219
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   220
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   221
                    b'1 1 stream-begin text-output 0 '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   222
                    b"cbor:[{b'msg': b'foo %s', b'args': [b'val1']}]"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   223
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   224
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   225
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   226
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   227
    def testtextoutput2arg(self):
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
   228
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   229
        val = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   230
            framing.createtextoutputframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   231
                stream, 1, [(b'foo %s %s', [b'val', b'value'], []),]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   232
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   233
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   234
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   235
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   236
            val,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   237
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   238
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   239
                    b'1 1 stream-begin text-output 0 '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   240
                    b"cbor:[{b'msg': b'foo %s %s', b'args': [b'val', b'value']}]"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   241
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   242
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   243
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   244
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   245
    def testtextoutput1label(self):
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
   246
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   247
        val = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   248
            framing.createtextoutputframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   249
                stream, 1, [(b'foo', [], [b'label']),]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   250
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   251
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   252
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   253
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   254
            val,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   255
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   256
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   257
                    b'1 1 stream-begin text-output 0 '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   258
                    b"cbor:[{b'msg': b'foo', b'labels': [b'label']}]"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   259
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   260
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   261
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   262
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   263
    def testargandlabel(self):
37288
9bfcbe4f4745 wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37285
diff changeset
   264
        stream = framing.stream(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   265
        val = list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   266
            framing.createtextoutputframe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   267
                stream, 1, [(b'foo %s', [b'arg'], [b'label']),]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   268
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   269
        )
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   270
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   271
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   272
            val,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   273
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   274
                ffs(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   275
                    b'1 1 stream-begin text-output 0 '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   276
                    b"cbor:[{b'msg': b'foo %s', b'args': [b'arg'], "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   277
                    b"b'labels': [b'label']}]"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   278
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   279
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   280
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   281
37060
0a6c5cc09a88 wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37058
diff changeset
   282
37052
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   283
if __name__ == '__main__':
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   284
    import silenttestrunner
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39410
diff changeset
   285
37052
8c3c47362934 wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   286
    silenttestrunner.main(__name__)