Example #1
0
def test_perform_read_socket_error(context, logger):
    ('XMPPConnection.perform_read() should '
     'fire tcp_disconnect on socket error')

    conn = XMPPConnection('capulet.com', 5222, recv_chunk_size=420)
    conn.read_queue = Mock(name='read_queue')
    conn.read_queue.get.return_value = '<data />'

    read = EventHandlerMock('on_read')
    tcp_disconnect = EventHandlerMock('on_tcp_disconnect')

    conn.on.read(read)
    conn.on.tcp_disconnect(tcp_disconnect)

    sock = Mock(name='socket')
    sock.recv.side_effect = socket.error('boom')
    conn.perform_read(sock)

    sock.recv.assert_called_once_with(420)
    read.called.should.be.false

    tcp_disconnect.assert_called_once_with(ANY, 'boom')

    logger.warning.assert_called_once_with(
        'failed to read data of chunk size: %s', 420)
Example #2
0
def test_perform_write_socket_error(context, logger):
    ('XMPPConnection.perform_write() should '
     'fire tcp_disconnect on socket error')

    conn = XMPPConnection('capulet.com', 5222)
    conn.write_queue = Mock(name='write_queue')
    conn.write_queue.get.return_value = '<data />'

    write = EventHandlerMock('on_write')
    tcp_disconnect = EventHandlerMock('on_tcp_disconnect')

    conn.on.write(write)
    conn.on.tcp_disconnect(tcp_disconnect)

    sock = Mock(name='socket')
    sock.sendall.side_effect = socket.error('boom')
    conn.perform_write(sock)

    sock.sendall.assert_called_once_with('<data />')
    write.called.should.be.false

    tcp_disconnect.assert_called_once_with(ANY, 'boom')

    logger.warning.assert_called_once_with('failed to write data (%s): %s',
                                           'boom', '<data />')
Example #3
0
def test_connect_error(context, resolver, create_tcp_socket):
    ('XMPPConnection.connect() should fire the tcp_failed event')
    socket = create_tcp_socket.return_value
    socket.connect.side_effect = ValueError('boom')

    conn = XMPPConnection('capulet.com', 5222)
    tcp_failed = EventHandlerMock('on_tcp_failed')
    conn.on.tcp_failed(tcp_failed)

    conn.connect()

    tcp_failed.assert_called_once_with(ANY, "boom")
Example #4
0
def test_connect_failed_dns(context, resolver, create_tcp_socket):
    ('XMPPConnection.connect() should fire the tcp_failed event')
    resolver.NoAnswer = ValueError
    resolver.query.side_effect = resolver.NoAnswer('boom')

    conn = XMPPConnection('capulet.com', 5222)
    tcp_failed = EventHandlerMock('on_tcp_failed')
    conn.on.tcp_failed(tcp_failed)

    conn.connect()

    tcp_failed.assert_called_once_with(ANY, "failed to resolve: capulet.com")
Example #5
0
def test_connect_ok(context, resolver, create_tcp_socket):
    ('XMPPConnection.connect() should fire the tcp_failed event')
    resolver.query.return_value = [
        StubAddress('10.20.30.40'),
    ]

    conn = XMPPConnection('capulet.com', 5222)
    tcp_established = EventHandlerMock('on_tcp_established')
    conn.on.tcp_established(tcp_established)

    conn.connect()

    tcp_established.assert_called_once_with(ANY, "10.20.30.40")
    conn.is_alive().should.be.true
Example #6
0
def test_disco_events():
    ('xep0030.ServiceDiscovery should forward events of its known nodes')

    # Given a stream
    stream = FakeXMLStream('[email protected]/bar')

    # And an event handler
    handle_query_items = EventHandlerMock('query_items')
    handle_query_info = EventHandlerMock('query_info')

    # And an instance of disco
    disco = ServiceDiscovery(stream)

    # When I hookup the handler
    disco.on.query_items(handle_query_items)
    disco.on.query_info(handle_query_info)

    # When I publish a QueryInfo
    query_info = QueryInfo.create(query='info')
    stream.on.node.shout(query_info)

    # And I publish a QueryItems
    query_items = QueryItems.create(query='items')
    stream.on.node.shout(query_items)

    # Then the handler should have been called appropriately
    handle_query_items.assert_called_once_with(ANY, query_items)
    handle_query_info.assert_called_once_with(ANY, query_info)
Example #7
0
def test_perform_write_ok(context):
    ('XMPPConnection.perform_write() should consume the write queue')
    conn = XMPPConnection('capulet.com', 5222)
    conn.write_queue = Mock(name='write_queue')
    conn.write_queue.get.return_value = '<data />'

    write = EventHandlerMock('on_write')
    conn.on.write(write)

    socket = Mock(name='socket')
    conn.perform_write(socket)

    socket.sendall.assert_called_once_with('<data />')
    write.assert_called_once_with(ANY, '<data />')
    conn.write_queue.get.assert_called_once_with(block=False, timeout=3)
Example #8
0
def test_disconnect_failed(context):
    ('XMPPConnection.disconnect() should fire the tcp_failed event if suitable'
     )

    conn = XMPPConnection('host', 5222)
    conn.socket = Mock(name='socket')
    conn.socket.shutdown.side_effect = socket.error('boom 1')
    conn.socket.close.side_effect = socket.error('boom 2')

    tcp_disconnect = EventHandlerMock('on_tcp_disconnect')

    conn.on.tcp_disconnect(tcp_disconnect)
    conn.disconnect()

    tcp_disconnect.assert_called_once_with(ANY, "intentional")
Example #9
0
def test_send_whitespace_keepalive_failed(context, socket_ready):
    ('XMPPConnection.send_whitespace_keepalive() should send')
    sockets = socket_ready.return_value
    sockets.write.sendall.side_effect = socket.error('boom')

    conn = XMPPConnection('capulet.com', 5222)
    conn.socket = 'w00t'
    tcp_disconnect = EventHandlerMock('on_tcp_disconnect')
    conn.on.tcp_disconnect(tcp_disconnect)

    conn.send_whitespace_keepalive()

    sockets.write.sendall.assert_called_once_with(b' ')

    socket_ready.assert_called_once_with('w00t', 3)
    tcp_disconnect.assert_called_once_with(ANY, 'boom')
Example #10
0
def test_reconnect_failed(context, create_tcp_socket):
    ('XMPPConnection.reconnect() should fire the tcp_failed event if suitable')
    socket = create_tcp_socket.return_value
    error = RuntimeError('boom')
    socket.connect.side_effect = error
    conn = XMPPConnection('host', 5222)

    tcp_restablished = EventHandlerMock('on_tcp_restablished')
    tcp_failed = EventHandlerMock('on_tcp_failed')

    conn.on.tcp_restablished(tcp_restablished)
    conn.on.tcp_failed(tcp_failed)
    conn.reconnect()

    socket.connect.assert_called_once_with(('host', 5222))

    tcp_failed.assert_called_once_with(ANY, error)
Example #11
0
def test_reconnect(context, create_tcp_socket):
    ('XMPPConnection.reconnect() should fire the tcp_restablished event if suitable'
     )
    socket = create_tcp_socket.return_value

    conn = XMPPConnection('host', 5222)

    tcp_restablished = EventHandlerMock('on_tcp_restablished')
    tcp_failed = EventHandlerMock('on_tcp_failed')

    conn.on.tcp_restablished(tcp_restablished)
    conn.on.tcp_failed(tcp_failed)
    conn.reconnect()

    socket.connect.assert_called_once_with(('host', 5222))

    tcp_restablished.assert_called_once_with(ANY, 'host:5222')
Example #12
0
def test_perform_read_ok(context):
    ('XMPPConnection.perform_read() should consume the read queue')
    conn = XMPPConnection('capulet.com', 5222, recv_chunk_size=420)
    conn.read_queue = Mock(name='read_queue')

    read = EventHandlerMock('on_read')
    conn.on.read(read)

    socket = Mock(name='socket')
    socket.recv.return_value = '<data />'
    conn.perform_read(socket)

    socket.recv.assert_called_once_with(420)
    read.assert_called_once_with(ANY, '<data />')
    conn.read_queue.put.assert_called_once_with('<data />',
                                                block=False,
                                                timeout=3)
Example #13
0
def test_is_authenticated_component():
    ('XMLStream.is_authenticated_component() should return '
     'true if there is success node in the stream')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)
    success_handler = EventHandlerMock('on_success')

    component = stream.extension['0114']
    component.on.success(success_handler)

    handshake = SuccessHandshake.create()
    component.route_nodes(None, handshake)

    component.is_authenticated().should.be.true

    success_handler.assert_called_once_with(ANY, handshake)
Example #14
0
def test_send_whitespace_keepalive(context, socket_ready):
    ('XMPPConnection.send_whitespace_keepalive() should send')
    sockets = socket_ready.return_value

    conn = XMPPConnection('capulet.com', 5222)
    conn.socket = 'w00t'
    tcp_disconnect = EventHandlerMock('on_tcp_disconnect')
    conn.on.tcp_disconnect(tcp_disconnect)

    conn.send_whitespace_keepalive()

    tcp_disconnect.called.should.be.false
    sockets.write.sendall.assert_called_once_with(b' ')

    socket_ready.assert_called_once_with('w00t', 3)
Example #15
0
def test_perform_write_empty_queue(context):
    ('XMPPConnection.perform_write() should do '
     'nothing when has an empty write queue')

    conn = XMPPConnection('capulet.com', 5222)
    conn.write_queue = Mock(name='write_queue')
    conn.write_queue.get.side_effect = Queue.Empty('dang!')

    write = EventHandlerMock('on_write')
    conn.on.write(write)

    socket = Mock(name='socket')
    conn.perform_write(socket)

    socket.sendall.called.should.be.false
    write.called.should.be.false
Example #16
0
def test_perform_read_no_data(context):
    ('XMPPConnection.perform_read() should do nothing '
     'if no data was received')

    conn = XMPPConnection('capulet.com', 5222, recv_chunk_size=420)
    conn.read_queue = Mock(name='read_queue')

    read = EventHandlerMock('on_read')
    conn.on.read(read)

    socket = Mock(name='socket')
    socket.recv.return_value = ''
    conn.perform_read(socket)

    socket.recv.assert_called_once_with(420)
    read.called.should.be.false
    conn.read_queue.put.called.should.be.false