def test_json_data_decoded():
    message = Message.from_bytes('''4:deadbeef:/cheese.sock:{
    "foo": "bar"
}''')
    expected = {"foo": "bar"}
    actual = message.data
    assert actual == expected, actual
def test_event_data_decoded():
    message = Message.from_bytes('''5:::{
    "name": "bar", "args": []
}''')
    expected = {u'args': [], u'name': 'bar'}
    actual = message.data
    assert actual == expected, actual
def test_event_data_decoded():
    message = Message.from_bytes('''5:::{
    "name": "bar", "args": []
}''')
    expected = {u'args': [], u'name': 'bar'}
    actual = message.data
    assert actual == expected
def test_json_data_decoded():
    message = Message.from_bytes('''4:deadbeef:/cheese.sock:{
    "foo": "bar"
}''')
    expected = {"foo": "bar"}
    actual = message.data
    assert actual == expected
Exemplo n.º 5
0
def test_can_put_onto_buffer(mk):
    mk(('echo.sock.spt', 'socket.send(socket.recv())'))
    buffer = Buffer(make_socket(), 'foo')
    expected = [FFFD + '4' + FFFD + '1:::']
    buffer.put(Message.from_bytes('1:::'))
    actual = list(buffer.flush())
    assert actual == expected
Exemplo n.º 6
0
def test_can_put_onto_buffer():
    mk(('echo.sock', 'socket.send(socket.recv())'))
    buffer = Buffer(make_socket(), 'foo')
    expected = [FFFD+'4'+FFFD+'1:::']
    buffer.put(Message.from_bytes('1:::'))
    actual = list(buffer.flush())
    assert actual == expected, actual
Exemplo n.º 7
0
def test_can_put_onto_buffer():
    mk(("echo.sock", "socket.send(socket.recv())"))
    buffer = Buffer(make_socket(), "foo")
    expected = [FFFD + "4" + FFFD + "1:::"]
    buffer.put(Message.from_bytes("1:::"))
    actual = list(buffer.flush())
    assert actual == expected, actual
def test_json_roundtrip():
    bytes = '''4:deadbeef:/cheese.sock:{
    "foo": "bar"
}'''
    message = Message.from_bytes(bytes)
    expected = bytes
    actual = str(message)
    assert actual == expected, actual
def test_json_roundtrip():
    bytes = '''4:deadbeef:/cheese.sock:{
    "foo": "bar"
}'''
    message = Message.from_bytes(bytes)
    expected = bytes
    actual = str(message)
    assert actual == expected
Exemplo n.º 10
0
 def __iter__(self):
     """Yield Message objects.
     """
     if self.bytes[:3] != FFFD:
         yield Message.from_bytes(self.bytes)
     else:
         frames = self.bytes.split(FFFD)
         frames = frames[1:]  # discard initial empty string
         nframes = len(frames)
         if nframes % 2 != 0:
             msg = "There are an odd number of frames in this packet: "
             msg += self.bytes
             raise SyntaxError(msg)
         while frames:
             # frames == [nbytes, bytes, nbytes, bytes, ...]
             # We only care about bytes.
             yield Message.from_bytes(frames[1])
             frames = frames[2:]
Exemplo n.º 11
0
 def __iter__(self):
     """Yield Message objects.
     """
     if self.bytes[:3] != FFFD:
         yield Message.from_bytes(self.bytes)
     else:
         frames = self.bytes.split(FFFD)
         frames = frames[1:]  # discard initial empty string
         nframes = len(frames)
         if nframes % 2 != 0:
             msg = b"There are an odd number of frames in this packet: "
             msg += self.bytes
             raise SyntaxError(msg)
         while frames:
             # frames == [nbytes, bytes, nbytes, bytes, ...]
             # We only care about bytes.
             yield Message.from_bytes(frames[1])
             frames = frames[2:]
Exemplo n.º 12
0
def test_channel_passes_send_on_to_four_sockets():
    mk(('echo.sock', 'channel.send(channel.recv())'))
    channel = Channel('foo', ThreadedBuffer)
    sockets = [make_socket(channel=channel) for i in range(4)]
    channel.send('foo')

    for socket in sockets:
        expected = deque([Message.from_bytes('3::/echo.sock:foo')])
        actual = socket.outgoing.queue
        assert actual == expected, actual
Exemplo n.º 13
0
def test_channel_passes_send_on_to_one_socket():
    mk(('echo.sock', ''))
    socket = make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)
    channel.send('foo')

    expected = deque([Message.from_bytes('3::/echo.sock:foo')])
    actual = socket.outgoing.queue
    assert actual == expected, actual
Exemplo n.º 14
0
def test_channel_passes_send_on_to_one_socket(mk):
    mk(('echo.sock.spt', ''))
    socket = make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)
    channel.send('foo')

    expected = deque([Message.from_bytes('3::/echo.sock:foo')])
    actual = socket.outgoing.queue
    assert actual == expected
Exemplo n.º 15
0
def test_channel_passes_send_on_to_four_sockets(mk):
    mk(('echo.sock.spt', 'channel.send(channel.recv())'))
    channel = Channel('foo', ThreadedBuffer)
    sockets = [make_socket(channel=channel) for i in range(4)]
    channel.send('foo')

    for socket in sockets:
        expected = deque([Message.from_bytes('3::/echo.sock:foo')])
        actual = socket.outgoing.queue
        assert actual == expected
def test_transport_GET_gets_data_from_socket(make_transport):
    transport = make_transport(state=1)
    message = Message.from_bytes(b"3:::Greetings, program!")
    transport.socket.outgoing.put(message)

    request = Request('GET')
    response = transport.respond(request)

    expected = FFFD + b'23' + FFFD + b'3:::Greetings, program!'
    actual = response.body.next()
    assert actual == expected
Exemplo n.º 17
0
def test_transport_GET_gets_data_from_socket():
    transport = make_transport(state=1)
    message = Message.from_bytes("3:::Greetings, program!")
    transport.socket.outgoing.put(message)
    
    request = Request('GET')
    response = transport.respond(request)
   
    expected = FFFD+'23'+FFFD+'3:::Greetings, program!'
    actual = response.body.next()
    assert actual == expected, actual
Exemplo n.º 18
0
def test_buffer_flush_performance():

    return # This test makes my lap hot.

    M = lambda: Message.from_bytes("3::/echo.sock:Greetings, program!")
    N = 10000
    buffer = Buffer([M() for i in range(N)])
    out = list(buffer.flush())
    nbuffer = len(buffer)
    nout = len(out)
    assert nbuffer + nout == N, (nbuffer, nout)
    assert nout > 500
Exemplo n.º 19
0
def test_buffer_flush_performance():

    return  # This test makes my lap hot.

    M = lambda: Message.from_bytes("3::/echo.sock:Greetings, program!")
    N = 10000
    buffer = Buffer([M() for i in range(N)])
    out = list(buffer.flush())
    nbuffer = len(buffer)
    nout = len(out)
    assert nbuffer + nout == N
    assert nout > 500
Exemplo n.º 20
0
def test_from_bytes_type_lower_bound_instantiable():
    message = Message.from_bytes('0:::')
    expected = 0
    actual = message.type
    assert actual == expected
Exemplo n.º 21
0
def test_packet_Packetable_with_framed_bytes():
    expected = [Message.from_bytes(b'1:::')]
    actual = list(Packet(FFFD + b'4' + FFFD + b'1:::'))
    assert actual == expected
def test_id_passes_through():
    message = Message.from_bytes('3:deadbeef::')
    expected = 'deadbeef'
    actual = message.id
    assert actual == expected, actual
def test_message_can_be_instantiated_from_bytes():
    expected = Message
    actual = Message.from_bytes('3:::').__class__
    assert actual is expected, actual
def test_from_bytes_data_part_is_optional():
    message = Message.from_bytes('3::')
    expected = ""
    actual = message.data
    assert actual == expected, actual
Exemplo n.º 25
0
def test_packet_Packetable_with_unframed_bytes():
    expected = [Message.from_bytes('1:::')]
    actual = list(Packet('1:::'))
    assert actual == expected, actual
Exemplo n.º 26
0
def test_packet_Packetable_with_multiple_frames():
    expected = [Message.from_bytes(x) for x in ('0:::', '1:::')]
    actual = list(Packet(FFFD+'4'+FFFD+'0:::'+FFFD+'4'+FFFD+'1:::'))
    assert actual == expected, repr(actual)
Exemplo n.º 27
0
def test_message_can_be_instantiated_from_bytes():
    expected = Message
    actual = Message.from_bytes('3:::').__class__
    assert actual is expected
Exemplo n.º 28
0
def test_packet_Packetable_with_unframed_bytes():
    expected = [Message.from_bytes('1:::')]
    actual = list(Packet('1:::'))
    assert actual == expected, actual
Exemplo n.º 29
0
def test_from_bytes_data_part_is_optional():
    message = Message.from_bytes('3::')
    expected = ""
    actual = message.data
    assert actual == expected
Exemplo n.º 30
0
 def __send(self, type_, data):
     message = Message()
     message.type = type_
     message.endpoint = self.endpoint
     message.data = data
     self.outgoing.put(message)
Exemplo n.º 31
0
def test_from_bytes_too_many_colons_and_the_extras_end_up_in_the_data():
    message = Message.from_bytes('3::::')
    expected = ":"
    actual = message.data
    assert actual == expected
def test_data_passes_through():
    message = Message.from_bytes('3:deadbeef:/cheese.sock:Greetings, program!')
    expected = 'Greetings, program!'
    actual = message.data
    assert actual == expected, actual
Exemplo n.º 33
0
def test_packet_Packetable_with_multiple_frames():
    expected = [Message.from_bytes(x) for x in ('0:::', '1:::')]
    actual = list(
        Packet(FFFD + '4' + FFFD + '0:::' + FFFD + '4' + FFFD + '1:::'))
    assert actual == expected, repr(actual)
def test_from_bytes_too_many_colons_and_the_extras_end_up_in_the_data():
    message = Message.from_bytes('3::::')
    expected = ":"
    actual = message.data
    assert actual == expected, actual
Exemplo n.º 35
0
def test_endpoint_passes_through():
    message = Message.from_bytes('3:deadbeef:/cheese.sock:')
    expected = '/cheese.sock'
    actual = message.endpoint
    assert actual == expected
def test_from_bytes_type_upper_bound_instantiable():
    message = Message.from_bytes('8:::')
    expected = 8
    actual = message.type
    assert actual == expected, actual
Exemplo n.º 37
0
def test_data_passes_through():
    message = Message.from_bytes('3:deadbeef:/cheese.sock:Greetings, program!')
    expected = 'Greetings, program!'
    actual = message.data
    assert actual == expected
def test_endpoint_passes_through():
    message = Message.from_bytes('3:deadbeef:/cheese.sock:')
    expected = '/cheese.sock'
    actual = message.endpoint
    assert actual == expected, actual
Exemplo n.º 39
0
 def __send(self, type_, data):
     message = Message()
     message.type = type_
     message.endpoint = self.endpoint
     message.data = data
     self.outgoing.put(message)
Exemplo n.º 40
0
def test_from_bytes_type_upper_bound_instantiable():
    message = Message.from_bytes('8:::')
    expected = 8
    actual = message.type
    assert actual == expected
Exemplo n.º 41
0
def test_from_bytes_type_lower_bound_instantiable():
    message = Message.from_bytes('0:::')
    expected = 0
    actual = message.type
    assert actual == expected, actual
Exemplo n.º 42
0
def test_id_passes_through():
    message = Message.from_bytes('3:deadbeef::')
    expected = 'deadbeef'
    actual = message.id
    assert actual == expected
Exemplo n.º 43
0
def test_packet_Packetable_with_framed_bytes():
    expected = [Message.from_bytes(b'1:::')]
    actual = list(Packet(FFFD + b'4' + FFFD + b'1:::'))
    assert actual == expected