Example #1
0
def parametrize_test_one_location_one_destination_direct_connection(
        reliability=1, destination_reliability=(1, 1), iterations=100):
    """
    Confirms the basic expectation that publishing a message

    Args:
        reliability: simulated connection reliability between the location
            and destination
        destination_reliability: expected destination reliability after running
            communication channel
    """
    l = Location(1)
    d = Destination(1)
    cb = get_callback()
    l.subscribe(cb)

    LocalSocket.connect(l, d, reliability=reliability)

    # for loop ensures that message dependibility doesn't decrease over time
    for i in range(iterations):
        msgs = {b"hello world", b"127", b"my name is greg"}
        for m in msgs:
            d.publish(m)
        if reliability >= 1:
            assert cb.log == msgs, "sent messages should be received"
        else:
            assert cb.log | msgs == msgs, "no weird messages"

        cb.log = set()
    assert d.reliability >= destination_reliability[0]
    assert d.reliability <= destination_reliability[1]
    assert d.distance == 1
Example #2
0
def test_one_location_two_destinations_indirect_connection():
    """
    Confirms that destiantions work together in the most basic capacity. One
    of the destinations will have to send its message through another
    destination
    """
    l = Location(1)
    d1 = Destination(1)
    d2 = Destination(1)
    cb = get_callback()
    l.subscribe(cb)

    LocalSocket.connect(l, d1)
    LocalSocket.connect(d1, d2)

    msgs = {b"hello world", b"127", b"my name is greg"}
    for i in range(100):
        for m in msgs:
            d1.publish(m)
        assert cb.log == msgs, "sent messages should be received"
        cb.log = set()  # clears the log
    assert d1.distance == 1
    for i in range(100):
        for m in msgs:
            d2.publish(m)
        assert cb.log == msgs, "sent messages should be received"
        cb.log = set()
    assert d2.distance == 2
Example #3
0
def test_use_socket_multiple_times_on_location():
    """
    Verify that one socket used more than once on a location raises an error
    """
    l = Location(1)
    s = LocalSocket.get_sockets(1)[0]
    l.use(s)
def test_publish_subscribe_multiple_socket_network():
    """
    Creates several sockets on the same network and verifies that publishes make
    it to all sockets
    """
    cb = get_callback()

    sockets = LocalSocket.get_sockets(10)
    # each socket has their own callback
    callbacks = [get_callback() for s in sockets]

    # subscribe all sockets to their corresponding callbacks
    for s, cb, i in zip(sockets, callbacks, range(len(sockets))):
        s._subscribe(i, cb)

    sockets[1]._publish(11, b"hello world")
    for cb in callbacks:
        assert cb.log == []

    sockets[1]._publish(2, b"hello world")
    for i, cb in zip(range(len(callbacks)), callbacks):
        if i == 2:
            assert cb.log == [(sockets[2], 2, b"hello world")]
        else:
            assert cb.log == []

    sockets[1]._publish(5, b"hello world")
    sockets[1]._publish(5, b"hello world")
    assert callbacks[5].log == 2 * [(sockets[5], 5, b"hello world")]

    sockets[5]._publish(8, b"hello world")
    assert callbacks[8].log == [(sockets[8], 8, b"hello world")]

    sockets[4]._publish(4, b"hello world")
    assert callbacks[4].log == []  # does not look at local subscribers
Example #5
0
def test_use_socket_one_location():
    """
    Verify that a provided socket can be used by a location
    """
    l = Location(1)
    s = LocalSocket.get_sockets(1)[0]
    l.use(s)
def test_publish():
    """
    Ensures that _publish method of hmap.sockets.AbstractSocket works in the
    most basic capacity
    """
    s = LocalSocket.get_sockets(1)[0]
    s._publish(2, b"message in a bottle")
def test_subscribe():
    """
    Ensures that _subscribe method of hmap.sockets.AbstractSocket works in the
    most basic capacity
    """
    s = LocalSocket.get_sockets(1)[0]
    s._subscribe(2, None)
Example #8
0
def test_use_socket_multiple_locations():
    """
    Verify that one socket can be used for multiple locations
    """
    locations = get_locations(10)
    s = LocalSocket.get_sockets(1)[0]
    for l in locations:
        l.use(s)
def test_use_sockets_one_destination():
    """
    Verify multiple sockets can be used by one destination
    """
    sockets = LocalSocket.get_sockets(10)
    d = Destination(1)
    for s in sockets:
        d.use(s)
def test_use_socket_multiple_destination():
    """
    Verify that one socket can be used for multiple destinations
    """
    destinations = get_destinations(10)
    s = LocalSocket.get_sockets(1)[0]
    for d in destinations:
        d.use(s)
Example #11
0
def test_use_sockets_one_location():
    """
    Verify multiple sockets can be used by one location
    """
    sockets = LocalSocket.get_sockets(10)
    l = Location(1)
    for s in sockets:
        l.use(s)
def test_use_sockets_multiple_destination_with_overlap():
    """
    Verify multiple sockets can be used by multiple destinations
    """
    destinations = get_destinations(10)
    sockets = LocalSocket.get_sockets(10)
    for d in destinations:
        for s in sockets:
            d.use(s)
Example #13
0
def test_use_sockets_multiple_location_with_overlap():
    """
    Verify multiple sockets can be used by multiple locations
    """
    locations = get_locations(10)
    sockets = LocalSocket.get_sockets(10)
    for l in locations:
        for s in sockets:
            l.use(s)
def test_use_socket_one_destination():
    """
    Verify that a provided socket can be used by a destination
    """
    d = Destination(1)
    s = LocalSocket.get_sockets(1)[0]
    d.use(s)
    try:
        d.use(s)
    except ValueError:
        assert True
    else:
        assert False
def test_publish_subscribe_2_socket_network():
    """
    Create two sockets and have one publish data and the other receive that
    data
    """
    cb = get_callback()

    sockets = LocalSocket.get_sockets(2)
    sockets[1]._subscribe(2, cb)
    p_count = 10
    for i in range(p_count):
        sockets[0]._publish(2, b"hello world")
    assert cb.log == [(sockets[1], 2, b"hello world")] * 10
def test_subscribe_value_error():
    """
    Ensures that _subscribe method of hmap.sockets.AbstractSocket raises a value
    error when more than one subscriber is being added for one id...
    """
    s = LocalSocket.get_sockets(1)[0]
    s._subscribe(2, None)
    try:
        s._subscribe(2, None)
    except ValueError:
        assert True
    else:
        assert False
def test_use_socket_multiple_times_on_destination():
    """
    Verify that one socket used more than once on a destination raises an error
    """
    d = Destination(1)
    s = LocalSocket.get_sockets(1)[0]
    d.use(s)
    try:
        d.use(s)
    except ValueError:
        assert True
    else:
        assert False
Example #18
0
def test_use_socket_one_location_over_use():
    """
    Verify that a provided socket can be used by a location
    """
    l = Location(1)
    s = LocalSocket.get_sockets(1)[0]
    l.use(s)
    try:
        l.use(s)
    except ValueError:
        assert True
    else:
        assert False
Example #19
0
def test_use_socket_one_duplicate_location():
    """
    Verify that a value error is raised when two location instances with the
    same id (not inherently bad) use the same socket
    """
    l_1 = Location(1)
    l_2 = Location(1)
    s = LocalSocket.get_sockets(1)[0]
    l_1.use(s)
    try:
        l_2.use(s)
    except ValueError:
        assert True
    else:
        assert False
def test_initialization():
    """
    Ensures that local sockets can be created
    """
    sockets = LocalSocket.get_sockets(10)
Example #21
0
def test_one_location_five_interconnected_destinations():
    """
    Confirms that destinations work toegther in a more complicated capacity,
    destinations will have to send messages through another destination. The
    end node may receive more than one message
    l----d1--------d3------d5
       |        /  |
       ---d2-------d4
    """
    l = Location(1)
    cb = get_callback()
    l.subscribe(cb)

    d1 = Destination(1)
    d2 = Destination(1)
    d3 = Destination(1)
    d4 = Destination(1)
    d5 = Destination(1)

    LocalSocket.connect(l, d1)
    LocalSocket.connect(l, d2)
    LocalSocket.connect(d1, d3)
    LocalSocket.connect(d2, d3)
    LocalSocket.connect(d2, d4)
    LocalSocket.connect(d3, d4)
    LocalSocket.connect(d3, d5)

    # need to start with closest ones
    def publish_messages(d):
        for i in range(100):
            msgs = {b"hello world", b"4", b"gerg waz here"}
            for m in msgs:
                d.publish(m)
            assert cb.log == msgs, "sent messages should be received"
            cb.log = set()

    publish_messages(d1)
    publish_messages(d2)
    publish_messages(d3)
    publish_messages(d4)
    publish_messages(d5)
    print(d1.reliability)
    print(d2.reliability)
    print(d3.reliability)
    print(d4._distances_table)
    print(d5.reliability)
    assert d1.distance == 1
    assert d2.distance == 1
    assert d3.distance == 2
    assert d4.distance == 2
    assert d5.distance == 3