Exemplo n.º 1
0
def test_switch_multiple():
    def t1():
        yield f2
        yield f2

    def t2():
        arr.append(1)
        yield f1
        arr.append(2)

    arr = []
    f1 = Fiber(t1)
    f2 = Fiber(t2)
    f1.switch()
    assert arr == [1,2]
    assert f2.done
Exemplo n.º 2
0
def test_switch_done():
    def task():
        yield

    fiber = Fiber(task)
    fiber.switch()
    fiber.switch()
    assert fiber.done
    with raises(FiberFinished):
        fiber.switch()
Exemplo n.º 3
0
def test_throw_finished():
    def task():
        try:
            yield
        except IndexError:
            pass

    fiber = Fiber(task)
    fiber.switch()
    with raises(StopIteration):
        fiber.throw(IndexError)
Exemplo n.º 4
0
def test_switch_from_different_thread():
    def task():
        yield
        yield

    def assertion():
        with raises(WrongThread):
            fiber.switch()

    fiber = Fiber(task)
    thread = ExThread(assertion)
    thread.start()
    thread.join()
Exemplo n.º 5
0
def test_switch():
    def task():
        arr.append(1)
        yield
        arr.append(2)

    arr = []
    fiber = Fiber(task)
    fiber.switch()
    assert arr == [1]
    assert not fiber.done

    fiber.switch()
    assert arr == [1, 2]
    assert fiber.done
Exemplo n.º 6
0
def test_throw_unfinished():
    def task():
        try:
            yield
            array.append(1)
        except IndexError:
            array.append(2)
            yield

    array = []
    fiber = Fiber(task)
    fiber.switch()
    fiber.throw(IndexError)
    assert array == [2]
    assert not fiber.done
Exemplo n.º 7
0
def test_fiber_init():
    fiber = Fiber(lambda: [])
    assert fiber.thread is current_thread()
    assert not fiber.done