예제 #1
0
def test_concurrency():
    records = [None] * 200 * 3
    record_idx = [0]

    out_queue = mp.JoinableQueue()

    def save_record(event):
        records[record_idx[0]] = event.results
        record_idx[0] += 1

    with mp_event_loop.EventLoop(name="EL 1", consumer_queue=out_queue, output_handlers=save_record) as el1,\
            mp_event_loop.EventLoop(name='EL two', consumer_queue=out_queue, has_results=False) as el2,\
            mp_event_loop.EventLoop(name='EL three', consumer_queue=out_queue, has_results=False) as el3:

        # Fast iterator to quickly add all of the events.
        all((el1.add_event(return_print, args=('EL 1\t%d' % i, )),
             el2.add_event(return_print, args=('EL two\t%d' % i, )),
             el3.add_event(return_print, args=('EL three\t%d' % i, )))
            for i in range(200))

        print('exit the context')

    # Make sure all of the items did not get processed on after the other.
    assert ['EL 1\t%d' % i for i in range(200)] not in records
    assert ['EL two\t%d' % i for i in range(200)] not in records
    assert ['EL three\t%d' % i for i in range(200)] not in records
    print("Success! The event loops worked concurrently")
예제 #2
0
def test_proxy_alt_args():
    with mp_event_loop.EventLoop() as loop:
        p = PointProxy(2, 3, loop=loop)
        p.move(1, 2)
        assert p.x == 2
        assert p.y == 3

    assert p.x == 1
    assert p.y == 2
예제 #3
0
def test_proxy():
    with mp_event_loop.EventLoop() as loop:
        p = PointProxy(loop=loop)
        p.move(1, 2)
        assert p.x == 0
        assert p.y == 0

    assert p.x == 1
    assert p.y == 2
예제 #4
0
def test_proxy_class_loop():
    with mp_event_loop.EventLoop() as loop:
        PointProxy.__loop__ = loop

        p = PointProxy()
        p.move(1, 2)
        assert p.x == 0
        assert p.y == 0

    assert p.x == 1
    assert p.y == 2
예제 #5
0
def test_proxy_wait():
    with mp_event_loop.EventLoop() as loop:
        p = PointProxy(2, 3, loop=loop)
        p.move(1, 2)
        t = time.clock()
        p.mp_wait()
        print("Wait time:",
              time.clock() -
              t)  # Slow!!!!! I'm in a GUI, so I don't really care
        assert p.x == 1
        assert p.y == 2

    assert p.x == 1
    assert p.y == 2
예제 #6
0
def test_proxy_getter_property_conflict():
    """Show the difference between GETTER and PROPERTY. If you don't sync both there could be differing results."""
    with mp_event_loop.EventLoop() as loop:
        PointProxy.__loop__ = loop

        p = PointProxy()
        assert p._z == 0
        assert p.get_z() is None

        p.set_z(3)
        assert p._z == 0
        assert p.get_z() is None

    assert p._z == 0
    assert p.get_z() == 3
def test_proxy():
    results = []

    def save_results(event):
        results.append(event.results)

    with mp_event_loop.EventLoop() as loop:
        PointProxy.__loop__ = loop

        p = PointProxy(1, 1)
        p.move(2, 3)
        assert p.x != 2
        assert p.y != 3

    assert p.x == 2
    assert p.y == 3
예제 #8
0
def test_event_loop():
    CACHE = False
    records = []

    def save_record(event):
        records.append(event.results)

    el = mp_event_loop.EventLoop()
    el.add_output_handler(save_record)

    el.start()

    el.add_event(plus_one, args=(1, ), cache=CACHE)
    el.add_event(plus_one, args=(2, ), cache=CACHE)
    el.add_event(plus_one, args=(3, ), cache=CACHE)
    el.add_event(plus_one, args=(4, ), cache=CACHE)
    el.add_event(plus_one, args=(5, ), cache=CACHE)
    el.add_event(plus_one, args=(6, ), cache=CACHE)

    any((el.add_event(plus_one, args=(i, ), cache=CACHE)) for i in range(200))

    el.wait()
    el.stop()

    index = 0
    for expected in range(2, 8):
        try:
            assert records[
                index] == expected, 'Value is {0} expected {1}'.format(
                    records[index], expected)
        except IndexError as err:
            raise IndexError("Index " + repr(index) +
                             " does not exist") from err
        index += 1
    for expected in range(1, 201):
        try:
            assert records[index] == expected
        except IndexError as err:
            raise IndexError("Index " + repr(index) +
                             " does not exist") from err
        index += 1