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
Example #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
Example #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
Example #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
Example #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:]
Example #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:]
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
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
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
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
Example #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
Example #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
Example #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
def test_from_bytes_type_lower_bound_instantiable():
    message = Message.from_bytes('0:::')
    expected = 0
    actual = message.type
    assert actual == expected
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
def test_packet_Packetable_with_unframed_bytes():
    expected = [Message.from_bytes('1:::')]
    actual = list(Packet('1:::'))
    assert actual == expected, actual
Example #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)
def test_message_can_be_instantiated_from_bytes():
    expected = Message
    actual = Message.from_bytes('3:::').__class__
    assert actual is expected
Example #28
0
def test_packet_Packetable_with_unframed_bytes():
    expected = [Message.from_bytes('1:::')]
    actual = list(Packet('1:::'))
    assert actual == expected, actual
def test_from_bytes_data_part_is_optional():
    message = Message.from_bytes('3::')
    expected = ""
    actual = message.data
    assert actual == expected
Example #30
0
 def __send(self, type_, data):
     message = Message()
     message.type = type_
     message.endpoint = self.endpoint
     message.data = data
     self.outgoing.put(message)
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
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
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
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
Example #39
0
 def __send(self, type_, data):
     message = Message()
     message.type = type_
     message.endpoint = self.endpoint
     message.data = data
     self.outgoing.put(message)
def test_from_bytes_type_upper_bound_instantiable():
    message = Message.from_bytes('8:::')
    expected = 8
    actual = message.type
    assert actual == expected
Example #41
0
def test_from_bytes_type_lower_bound_instantiable():
    message = Message.from_bytes('0:::')
    expected = 0
    actual = message.type
    assert actual == expected, actual
def test_id_passes_through():
    message = Message.from_bytes('3:deadbeef::')
    expected = 'deadbeef'
    actual = message.id
    assert actual == expected
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