Exemplo n.º 1
0
    async def test_run_when_outside(self):
        # normal function, simple test
        var = False

        @run_when_outside(self, 'sleep')
        def outside_test():
            nonlocal var
            var = True

        outside_test()
        self.assertIs(var, True)

        var = False

        # test 3 situations for coroutine functions

        ## abandon
        var1 = 0

        @run_when_outside(self, 'sleep')
        async def outside_test():
            await asyncio.create_task(asyncio.sleep(0.1))
            nonlocal var1
            var1 += 1

        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(asyncio.sleep(0.35))
        self.assertEqual(var1, 3)

        await asyncio.create_task(Gear(self).set_period('sleep'))
Exemplo n.º 2
0
 async def worker(name: str):
     '''工人等self的test2时期,就工作,工作完再等'''
     while True:
         await asyncio.create_task(Gear(name).set_period('waiting'))
         await asyncio.create_task(Gear(self).wait_enter_period('test2'))
         q.put_nowait({"time": asyncio.get_running_loop().time(), 'msg': 'start to work'})
         await asyncio.create_task(Gear(name).set_period('working'))
         await asyncio.create_task(asyncio.sleep(1))  # simulate work
         q.put_nowait({"time": asyncio.get_running_loop().time(), 'msg': 'end working'})  # 测试间隔1s
Exemplo n.º 3
0
 async def test_wait_inside_period(self):
     time1 = asyncio.get_running_loop().time()
     await asyncio.create_task(Gear(self).wait_inside_period('test1'))
     time2 = asyncio.get_running_loop().time()
     self.assertLessThan(time2 - time1, 0.2)
     asyncio.create_task(self._wait_then_set_period(self, 0.3, 'test3'))
     await asyncio.create_task(Gear(self).wait_inside_period('test3'))
     time3 = asyncio.get_running_loop().time()
     self.assertGreaterThan(time3 - time2, 0.2)
Exemplo n.º 4
0
 async def wait_worker_accomplish_working_then_set_period_test1():
     '''等待工人干完活就设置self到周期test1'''
     while True:
         wait_worker1_exit_working_task = asyncio.create_task(
             Gear('worker1').wait_exit_period('working'))
         wait_worker2_exit_working_task = asyncio.create_task(
             Gear('worker2').wait_exit_period('working'))
         await asyncio.ensure_future(
             asyncio.gather(wait_worker1_exit_working_task, wait_worker2_exit_working_task))
         await asyncio.create_task(Gear(self).set_period('test1'))
Exemplo n.º 5
0
    async def test_bind_obj_has_dict_property(self):
        class C:
            a = {}

        Gear(C).add_periods('1')
        Gear(C).add_periods('2')
        self.assertEqual('1', Gear(C).get_present_period())
        waiter = asyncio.create_task(Gear(C).wait_enter_period('2'))
        await asyncio.create_task(Gear(C).set_period('2'))
        await waiter
    async def test_wait_change_period(self):
        waiter_task = asyncio.create_task(Gear(self).wait_change_period())
        asyncio.create_task(Gear(self).set_period('test2'))
        waited = None
        try:
            await asyncio.wait_for(waiter_task, 0.1)
            waited = True
        except asyncio.TimeoutError:
            waited = False
        self.assertIs(waited, True)

        waiter_task = asyncio.create_task(Gear(self).wait_change_period())
        asyncio.get_running_loop().call_later(0.05,
                                              Gear(self).sync_set_period,
                                              'test3')
        waited = None
        try:
            await asyncio.wait_for(waiter_task, 0.1)
            waited = True
        except asyncio.TimeoutError:
            waited = False
        self.assertIs(waited, True)

        Gear(self).add_periods('test4')
        waiter_task = asyncio.create_task(Gear(self).wait_change_period())
        asyncio.get_running_loop().call_later(0.05,
                                              Gear(self).sync_set_period,
                                              'test4')
        waited = None
        try:
            await asyncio.wait_for(waiter_task, 0.1)
            waited = True
        except asyncio.TimeoutError:
            waited = False
        self.assertIs(waited, True)

        n = 1000

        async def waiter():
            for _ in range(n):
                await Gear(self).wait_change_period()

        waiter_task = asyncio.create_task(waiter())

        for _ in range(n):
            if Gear(self).get_present_period() != 'test2':
                await (Gear(self).set_period('test2'))
            else:
                await (Gear(self).set_period('test1'))

        await asyncio.wait_for(waiter_task, 5)
    async def test_class_method_when_outside(self):
        class C:
            awaken_count = 0

            @when_outside('sleep')
            @classmethod
            async def f1(cls):
                await asyncio.create_task(asyncio.sleep(0.1))
                cls.awaken_count += 1

        Gear(C).add_periods('sleep', 'awaken')
        await asyncio.create_task(Gear(C).set_period('awaken'))
        await asyncio.create_task(asyncio.sleep(0.25))
        await asyncio.create_task(Gear(C).set_period('sleep'))
        self.assertEqual(C.awaken_count, 2)
    async def test_when_ins_outside(self):
        class C:
            def __init__(self):
                Gear(self).add_periods('sleep', 'awaken')
                self.awaken_count = 0

            @when_outside('sleep')
            async def f1(self):
                await asyncio.create_task(asyncio.sleep(0.1))
                self.awaken_count += 1

        c = C()
        await asyncio.create_task(Gear(c).set_period('awaken'))
        await asyncio.create_task(asyncio.sleep(0.299))
        await asyncio.create_task(Gear(c).set_period('sleep'))
        self.assertEqual(c.awaken_count, 2)
Exemplo n.º 9
0
        async def noticing_in_test2():
            '''提醒工人干活的时期'''
            while True:
                await asyncio.create_task(Gear(self).wait_inside_period('test2'))
                q.put_nowait(
                    {"time": asyncio.get_running_loop().time(), 'msg': 'In test2, the workers should be working.'})

                await asyncio.create_task(asyncio.sleep(0.05))  # 测试是处于工作时期内
Exemplo n.º 10
0
    async def test_multi_callback(self):
        var1 = False

        @run_when_enter(self, 'awaken', 'abandon')
        async def enter_test():
            await asyncio.create_task(asyncio.sleep(0.1))
            nonlocal var1
            if var1:
                var1 = False
            else:
                var1 = True

        @run_when_enter(self, 'awaken', 'abandon')
        async def enter_test2():
            await asyncio.create_task(asyncio.sleep(0.1))
            nonlocal var1
            if var1:
                var1 = False
            else:
                var1 = True

        await asyncio.create_task(Gear(self).set_period('awaken'))
        self.assertIs(var1, False)
    async def test_class_method_when_exit(self):
        class C:
            awaken_count = 0
            sleep_count = 0

            @when_exit('sleep')
            @classmethod
            def f(cls):
                cls.awaken_count += 1

            @when_exit('sleep')
            @classmethod
            async def f1(cls):
                cls.awaken_count += 1

            @when_exit('awaken')
            @classmethod
            def f2(cls):
                cls.sleep_count += 1

        Gear(C).add_periods('sleep', 'awaken')
        await asyncio.create_task(Gear(C).set_period('awaken'))
        self.assertEqual(C.awaken_count, 2)
        self.assertEqual(C.sleep_count, 0)
        await asyncio.create_task(Gear(C).set_period('sleep'))
        self.assertEqual(C.awaken_count, 2)
        self.assertEqual(C.sleep_count, 1)
        await asyncio.create_task(Gear(C).set_period('awaken'))
        self.assertEqual(C.awaken_count, 4)
        self.assertEqual(C.sleep_count, 1)

        # queue_blocking test
        ## abandon
        class C:
            awaken_count = 0

            @when_exit('sleep', 'abandon')
            @classmethod
            async def f1(cls):
                await asyncio.create_task(asyncio.sleep(0.1))
                cls.awaken_count += 1

        Gear(C).add_periods('sleep', 'awaken')
        await asyncio.create_task(Gear(C).set_period('awaken'))
        await asyncio.create_task(Gear(C).set_period('sleep'))
        await asyncio.create_task(Gear(C).set_period('awaken'))
        await asyncio.create_task(Gear(C).set_period('sleep'))
        self.assertEqual(C.awaken_count, 0)
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertEqual(C.awaken_count, 1)
        await asyncio.create_task(asyncio.sleep(0.25))
        self.assertEqual(C.awaken_count, 1)

        ## non_block
        class C:
            awaken_count = 0

            @when_exit('sleep', 'non_block')
            @classmethod
            async def f1(cls):
                await asyncio.create_task(asyncio.sleep(0.1))
                cls.awaken_count += 1

        Gear(C).add_periods('sleep', 'awaken')
        await asyncio.create_task(Gear(C).set_period('awaken'))
        await asyncio.create_task(Gear(C).set_period('sleep'))
        await asyncio.create_task(Gear(C).set_period('awaken'))
        await asyncio.create_task(Gear(C).set_period('sleep'))
        self.assertEqual(C.awaken_count, 0)
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertEqual(C.awaken_count, 2)

        ## queue
        class C:
            awaken_count = 0

            @when_exit('sleep', 'queue')
            @classmethod
            async def f1(cls):
                await asyncio.create_task(asyncio.sleep(0.1))
                cls.awaken_count += 1

        Gear(C).add_periods('sleep', 'awaken')
        await asyncio.create_task(Gear(C).set_period('awaken'))
        await asyncio.create_task(Gear(C).set_period('sleep'))
        await asyncio.create_task(Gear(C).set_period('awaken'))
        await asyncio.create_task(Gear(C).set_period('sleep'))
        self.assertEqual(C.awaken_count, 0)
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertEqual(C.awaken_count, 1)
        await asyncio.create_task(asyncio.sleep(0.25))
        self.assertEqual(C.awaken_count, 2)
    async def test_when_ins_exit(self):
        class C:
            def __init__(self):
                Gear(self).add_periods('sleep', 'awaken')
                self.awaken_exit_count = 0
                self.sleep_exit_count = 0

            @when_exit('awaken')
            def f(self):
                self.awaken_exit_count += 1

            @when_exit('awaken')
            async def f1(self):
                self.awaken_exit_count += 1

            @when_exit('sleep')
            def f2(self):
                self.sleep_exit_count += 1

        c = C()
        await asyncio.create_task(Gear(c).set_period('awaken'))
        self.assertEqual(c.sleep_exit_count, 1)
        self.assertEqual(c.awaken_exit_count, 0)
        await asyncio.create_task(Gear(c).set_period('sleep'))
        self.assertEqual(c.sleep_exit_count, 1)
        self.assertEqual(c.awaken_exit_count, 2)
        await asyncio.create_task(Gear(c).set_period('awaken'))
        self.assertEqual(c.sleep_exit_count, 2)
        self.assertEqual(c.awaken_exit_count, 2)

        # queue_blocking test
        ## abandon
        class C:
            def __init__(self):
                Gear(self).add_periods('sleep', 'awaken')
                self.awaken_count = 0

            @when_exit('sleep', 'abandon')
            async def f1(self):
                await asyncio.create_task(asyncio.sleep(0.1))
                self.awaken_count += 1

        c = C()
        await asyncio.create_task(Gear(c).set_period('awaken'))
        await asyncio.create_task(Gear(c).set_period('sleep'))
        await asyncio.create_task(Gear(c).set_period('awaken'))
        await asyncio.create_task(Gear(c).set_period('sleep'))
        self.assertEqual(c.awaken_count, 0)
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertEqual(c.awaken_count, 1)
        await asyncio.create_task(asyncio.sleep(0.25))
        self.assertEqual(c.awaken_count, 1)

        ## non_block
        class C:
            def __init__(self):
                Gear(self).add_periods('sleep', 'awaken')
                self.awaken_count = 0

            @when_exit('sleep', 'non_block')
            async def f1(self):
                await asyncio.create_task(asyncio.sleep(0.1))
                self.awaken_count += 1

        c = C()
        await asyncio.create_task(Gear(c).set_period('awaken'))
        await asyncio.create_task(Gear(c).set_period('sleep'))
        await asyncio.create_task(Gear(c).set_period('awaken'))
        await asyncio.create_task(Gear(c).set_period('sleep'))
        self.assertEqual(c.awaken_count, 0)
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertEqual(c.awaken_count, 2)

        ## queue
        class C:
            def __init__(self):
                Gear(self).add_periods('sleep', 'awaken')
                self.awaken_count = 0

            @when_exit('sleep', 'queue')
            async def f1(self):
                await asyncio.create_task(asyncio.sleep(0.1))
                self.awaken_count += 1

        c = C()
        await asyncio.create_task(Gear(c).set_period('awaken'))
        await asyncio.create_task(Gear(c).set_period('sleep'))
        await asyncio.create_task(Gear(c).set_period('awaken'))
        await asyncio.create_task(Gear(c).set_period('sleep'))
        self.assertEqual(c.awaken_count, 0)
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertEqual(c.awaken_count, 1)
        await asyncio.create_task(asyncio.sleep(0.25))
        self.assertEqual(c.awaken_count, 2)
Exemplo n.º 13
0
 async def test_in_test1_period_after_end_working():
     await asyncio.create_task(asyncio.sleep(0.5))
     self.assertEqual('test1', Gear(self).get_present_period())
Exemplo n.º 14
0
 async def set_test2():
     '''只要离开test2时期就1s后设置到test2时期让工人干活'''
     while True:
         await asyncio.create_task(Gear(self).wait_outside_period('test2'))
         await asyncio.create_task(asyncio.sleep(1))
         await asyncio.create_task(Gear(self).set_period('test2'))  # 测试休息1s
Exemplo n.º 15
0
    async def test_set_get_obj_present_period(self):
        await asyncio.create_task(Gear(self).set_period('test2'))
        self.assertEqual('test2', Gear(self).get_present_period())

        await asyncio.create_task(Gear(self).set_period('test3'))
        self.assertEqual('test3', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test1'))

        # idempotent test
        var = True

        @run_when_enter(self, 'test2')
        def enter_test():
            nonlocal var
            var = not var

        # slot_num test
        await asyncio.create_task(Gear(self).set_period('test2'))
        self.assertEqual(False, var)
        await asyncio.create_task(Gear(self).set_period('test2'))
        self.assertEqual(False, var)
        await asyncio.create_task(Gear(self).set_period('test1'))

        ## idempotent test
        await asyncio.create_task(Gear(self).set_period('test1', slot_num=2))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test1', slot_num=2))
        self.assertEqual('test1', Gear(self).get_present_period())
        ## halfway test
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test2', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test1'))

        ## interrput test
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=2))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=2))
        self.assertEqual('test2', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test1'))
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test2', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test1'))

        ## cross set test
        await asyncio.create_task(Gear(self).set_period('test3', slot_num=3))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test3', slot_num=2))
        self.assertEqual('test1', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test3', slot_num=2))
        self.assertEqual('test3', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test3', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test2', slot_num=3))
        self.assertEqual('test2', Gear(self).get_present_period())
        await asyncio.create_task(Gear(self).set_period('test1'))
Exemplo n.º 16
0
    async def test_run_when_enter(self):
        # normal function, simple test
        var = False

        @run_when_enter(self, 'awaken')
        def enter_test():
            nonlocal var
            var = True

        enter_test()
        self.assertIs(var, True)

        var = False

        # await asyncio.create_task(AsyncGear.set_obj_period(self, 'awaken'))
        await asyncio.create_task(Gear(self).set_period('awaken'))

        # self.assertEqual('awaken', AsyncGear.get_obj_present_period(self))
        self.assertEqual('awaken', Gear(self).get_present_period())
        self.assertIs(var, True)
        var = False
        # await asyncio.create_task(AsyncGear.set_obj_period(self, 'sleep'))
        await asyncio.create_task(Gear(self).set_period('sleep'))

        # test 3 situations for coroutine functions

        var1 = False

        @run_when_enter(self, 'awaken', 'abandon')
        async def enter_test():
            await asyncio.create_task(asyncio.sleep(0.1))
            nonlocal var1
            if var1:
                var1 = False
            else:
                var1 = True

        await asyncio.create_task(enter_test())
        self.assertIs(var1, True)
        var1 = False

        ## abandon
        # await asyncio.create_task(AsyncGear.set_obj_period(self, 'awaken'))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        # await asyncio.create_task(AsyncGear.set_obj_period(self, 'sleep'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertIs(var1, True)
        var1 = False

        # await asyncio.create_task(AsyncGear.set_obj_period(self, 'awaken'))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        # await asyncio.create_task(AsyncGear.set_obj_period(self, 'sleep'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        # await asyncio.create_task(AsyncGear.set_obj_period(self, ))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertIs(var1, True)
        var1 = False

        ## non_block
        var2 = False

        @run_when_enter(self, 'awaken', 'non_block')
        async def enter_test():
            await asyncio.create_task(asyncio.sleep(0.1))
            nonlocal var2
            if var2:
                var2 = False
            else:
                var2 = True

        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertIs(var2, True)
        var2 = False

        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertIs(var2, False)
        var2 = False

        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(asyncio.sleep(0.15))
        self.assertIs(var2, True)
        var2 = False
        ## queue
        var3 = False

        fs = []

        @run_when_enter(self, 'awaken', 'queue')
        async def enter_test():
            f = asyncio.get_running_loop().create_future()
            fs.append(f)
            await asyncio.create_task(asyncio.sleep(0.1))
            nonlocal var3
            if var3:
                var3 = False
            else:
                var3 = True
            f.set_result(None)
            fs.remove(f)

        time = asyncio.get_running_loop().time()

        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(asyncio.sleep(0.15))
        [await asyncio.ensure_future(f) for f in fs]
        self.assertEqual(0.1 * 2, round(asyncio.get_running_loop().time() - time, 1))
        self.assertEqual(var3, False)
        time = asyncio.get_running_loop().time()
        var3 = False

        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(Gear(self).set_period('awaken'))
        await asyncio.create_task(Gear(self).set_period('sleep'))
        await asyncio.create_task(asyncio.sleep(0.25))
        [await asyncio.ensure_future(f) for f in fs]
        self.assertEqual(round(0.1 * 3, 1), round(asyncio.get_running_loop().time() - time, 1))
        self.assertEqual(var3, True)
        time = asyncio.get_running_loop().time()
        var3 = False
Exemplo n.º 17
0
    async def test_handle_period_then_set_period(self):
        q = asyncio.Queue()
        # 两个工人
        Gear('worker1').add_periods('waiting', 'working')
        Gear('worker2').add_periods('waiting', 'working')

        async def worker(name: str):
            '''工人等self的test2时期,就工作,工作完再等'''
            while True:
                await asyncio.create_task(Gear(name).set_period('waiting'))
                await asyncio.create_task(Gear(self).wait_enter_period('test2'))
                q.put_nowait({"time": asyncio.get_running_loop().time(), 'msg': 'start to work'})
                await asyncio.create_task(Gear(name).set_period('working'))
                await asyncio.create_task(asyncio.sleep(1))  # simulate work
                q.put_nowait({"time": asyncio.get_running_loop().time(), 'msg': 'end working'})  # 测试间隔1s
                # 测试在0.5s之后是test1时期

        # 激活两工人
        worker1_task = asyncio.create_task(worker('worker1'))

        worker2_task = asyncio.create_task(worker('worker2'))

        async def wait_worker_accomplish_working_then_set_period_test1():
            '''等待工人干完活就设置self到周期test1'''
            while True:
                wait_worker1_exit_working_task = asyncio.create_task(
                    Gear('worker1').wait_exit_period('working'))
                wait_worker2_exit_working_task = asyncio.create_task(
                    Gear('worker2').wait_exit_period('working'))
                await asyncio.ensure_future(
                    asyncio.gather(wait_worker1_exit_working_task, wait_worker2_exit_working_task))
                await asyncio.create_task(Gear(self).set_period('test1'))

        wait_worker_accomplish_working_then_set_period_test1_task = \
            asyncio.create_task(wait_worker_accomplish_working_then_set_period_test1())

        async def noticing_in_test2():
            '''提醒工人干活的时期'''
            while True:
                await asyncio.create_task(Gear(self).wait_inside_period('test2'))
                q.put_nowait(
                    {"time": asyncio.get_running_loop().time(), 'msg': 'In test2, the workers should be working.'})

                await asyncio.create_task(asyncio.sleep(0.05))  # 测试是处于工作时期内

        noticing_in_test2_task = asyncio.create_task(noticing_in_test2())

        async def set_test2():
            '''只要离开test2时期就1s后设置到test2时期让工人干活'''
            while True:
                await asyncio.create_task(Gear(self).wait_outside_period('test2'))
                await asyncio.create_task(asyncio.sleep(1))
                await asyncio.create_task(Gear(self).set_period('test2'))  # 测试休息1s

        set_test2_task = asyncio.create_task(set_test2())

        work_start_time = None
        work_end_time = None
        work_end_time_count = 0
        tasks = []
        init_time = asyncio.get_running_loop().time()
        while asyncio.get_running_loop().time() - init_time <= 5:
            new_msg = await asyncio.create_task(q.get())
            if new_msg['msg'] == 'start to work':
                work_start_time = new_msg['time']
                if work_end_time:
                    self.assertEqual(round(work_start_time - work_end_time), 1)

            elif new_msg['msg'] == 'end working':
                work_end_time = new_msg['time']
                self.assertEqual(round(work_end_time - work_start_time), 1)

                async def test_in_test1_period_after_end_working():
                    await asyncio.create_task(asyncio.sleep(0.5))
                    self.assertEqual('test1', Gear(self).get_present_period())

                tasks.append(asyncio.create_task(test_in_test1_period_after_end_working()))

                # 俩工人第一次完工,复位
                work_end_time_count += 1
                if work_end_time_count % 2 == 0:
                    work_start_time = work_end_time = None
            elif new_msg['msg'] == 'In test2, the workers should be working.':
                if work_start_time:
                    self.assertLessThan(new_msg['time'] - work_start_time, 1)
            q.task_done()
        [await task for task in tasks]
Exemplo n.º 18
0
 async def setUp(self) -> None:
     Gear(self).add_periods('sleep', 'awaken')
     self.assertEqual('sleep', Gear(self).get_present_period())
Exemplo n.º 19
0
 async def setUp(self) -> None:
     Gear(self).add_periods('test1', 'test2', 'test3')
     self.assertEqual(Gear(self).get_present_period(), 'test1')
Exemplo n.º 20
0
    async def test_wait_outside_period(self):
        time1 = asyncio.get_running_loop().time()
        await asyncio.create_task(Gear(self).wait_outside_period('test2'))
        time2 = asyncio.get_running_loop().time()

        self.assertLessThan(time2 - time1, 0.2)
Exemplo n.º 21
0
 async def _wait_then_set_period(self, obj, wait_seconds: float, period_name: str):
     await asyncio.create_task(asyncio.sleep(wait_seconds))
     await asyncio.create_task(Gear(obj).set_period(period_name))
Exemplo n.º 22
0
 async def test_add_period(self):
     Gear(self).add_periods('test4')
     self.assertEqual(Gear(self).get_present_period(), 'test1')
     obj_period_names = Gear(self).get_period_names()
     self.assertEqual({'test1', 'test2', 'test3', 'test4'}, set(obj_period_names))
 def __init__(self):
     Gear(self).add_periods('sleep', 'awaken')
     self.awaken_count = 0
Exemplo n.º 24
0
 def test_get_obj_period_names(self):
     obj_period_names = Gear(self).get_period_names()
     self.assertEqual({'test1', 'test2', 'test3'}, set(obj_period_names))