示例#1
0
def test_switch_done():
    def task():
        yield

    fiber = Fiber(task)
    fiber.switch()
    fiber.switch()
    assert fiber.done
    with raises(FiberFinished):
        fiber.switch()
示例#2
0
def test_throw_finished():
    def task():
        try:
            yield
        except IndexError:
            pass

    fiber = Fiber(task)
    fiber.switch()
    with raises(StopIteration):
        fiber.throw(IndexError)
示例#3
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
示例#4
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
示例#5
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