Ejemplo n.º 1
0
 def testBlock(self):
     scheduler = Scheduler()
     scheduler.queue.addSubQueue(10, RoutineControlEvent.createMatcher())
     scheduler.queue.addSubQueue(1, TestConsumerEvent.createMatcher(), 'consumer', 5, 5)
     rA = RoutineContainer(scheduler)
     output = bytearray()
     async def mainA():
         rA.subroutine(mainB(), daemon = True)
         for _ in range(0,10):
             await rA.wait_for_send(TestConsumerEvent(rA.mainroutine))
             output.extend(b'A')
     async def mainB():
         await rA.do_events()
         matcher = TestConsumerEvent.createMatcher(producer=rA.mainroutine)
         while True:
             ev = await matcher
             ev.canignore = True
             output.extend(b'B')
     async def mainC():
         await rA.do_events()
         output.extend(b'C')
     rA.main = mainA
     rA.start()
     rA.subroutine(mainC())
     scheduler.main()
     self.assertEqual(output, b'AAAAACBABABABABABBBBB')
Ejemplo n.º 2
0
 def setUp(self):
     #self.scheduler 
     logging.basicConfig()
     self.scheduler = Scheduler(DefaultPolling())
     #self.scheduler.debugging = True
     #self.scheduler.logger.setLevel('DEBUG')
     #Client.logger.setLevel('DEBUG')
     import tests
     import os.path
     rootpath, _ = os.path.split(tests.__path__[0])
     if rootpath:
         os.chdir(rootpath)
     self.scheduler.queue.addSubQueue(3, PollEvent.createMatcher(category=PollEvent.WRITE_READY), 'write', None, None, CBQueue.AutoClassQueue.initHelper('fileno'))
     self.scheduler.queue.addSubQueue(1, PollEvent.createMatcher(category=PollEvent.READ_READY), 'read', None, None, CBQueue.AutoClassQueue.initHelper('fileno'))
     self.scheduler.queue.addSubQueue(5, PollEvent.createMatcher(category=PollEvent.ERROR), 'error')
     self.scheduler.queue.addSubQueue(2, ConnectionControlEvent.createMatcher(), 'control')
     self.scheduler.queue.addSubQueue(4, ConnectionWriteEvent.createMatcher(), 'connectionwrite', 40, 40, CBQueue.AutoClassQueue.initHelper('connection'))
     self.scheduler.queue.addSubQueue(10, RoutineControlEvent.createMatcher(), 'routine')
     self.scheduler.queue.addSubQueue(9, TimerEvent.createMatcher(), 'timer')
     self.scheduler.queue.addSubQueue(8, ResolveResponseEvent.createMatcher(), 'resolve')
     self.scheduler.queue.addSubQueue(8, ResolveRequestEvent.createMatcher(), 'resolvereq')
     self.scheduler.queue.addSubQueue(20, SystemControlEvent.createMatcher(), 'sysctl')
     self.scheduler.queue.addSubQueue(0, SystemControlLowPriorityEvent.createMatcher(), 'sysctllow')
     #Client.logger.setLevel('DEBUG')
     #TcpServer.logger.setLevel('DEBUG')
     self.protocolServer = TestProtocol(True)
     self.protocolClient = TestProtocol(False)
     self.resolver = Resolver(scheduler=self.scheduler)
     self.resolver.start()
Ejemplo n.º 3
0
 def _test_limiter(limit, expected_result, *numbers):
     scheduler = Scheduler()
     rA = RoutineContainer(scheduler)
     rB = RoutineContainer(scheduler)
     limiter = RateLimiter(limit, rA)
     counter = [0, 0]
     result = []
     async def _record():
         first = True
         while True:
             await rA.do_events()
             if counter[0] == 0:
                 break
             result.append((counter[0], counter[1]))
             counter[0] = 0
             counter[1] = 0
     async def _limited(use = 1):
         await limiter.limit(use)
         counter[0] += 1
         counter[1] += use
     async def _starter():
         for t in numbers:
             if isinstance(t, tuple):
                 for use in t:
                     rB.subroutine(_limited(use))
             else:
                 for _ in range(t):
                     rB.subroutine(_limited())
             await rB.do_events()
     rA.subroutine(_record(), False)
     rB.subroutine(_starter())
     scheduler.main()
     self.assertEqual(result, expected_result)
Ejemplo n.º 4
0
    def testBlock(self):
        scheduler = Scheduler()
        scheduler.queue.addSubQueue(10, RoutineControlEvent.createMatcher())
        scheduler.queue.addSubQueue(1, TestConsumerEvent.createMatcher(),
                                    'consumer', 5, 5)
        rA = RoutineContainer(scheduler)
        output = bytearray()

        def mainA():
            rA.subroutine(mainB(), daemon=True)
            for i in range(0, 10):
                for ms in rA.waitForSend(TestConsumerEvent(rA.mainroutine)):
                    yield ms
                output.extend(b'A')

        def mainB():
            for m in rA.doEvents():
                yield m
            matcher = TestConsumerEvent.createMatcher(producer=rA.mainroutine)
            while True:
                yield (matcher, )
                rA.event.canignore = True
                output.extend(b'B')

        def mainC():
            for m in rA.doEvents():
                yield m
            output.extend(b'C')

        rA.main = mainA
        rA.start()
        rA.subroutine(mainC())
        scheduler.main()
        self.assertEqual(output, b'AAAAACBABABABABABBBBB')
Ejemplo n.º 5
0
    def testLoopConsumer(self):
        scheduler = Scheduler()
        scheduler.queue.addSubQueue(10, RoutineControlEvent.createMatcher())
        scheduler.queue.addSubQueue(1, TestConsumerEvent.createMatcher(),
                                    'consumer', 5, 5)
        rA = RoutineContainer(scheduler)
        output = bytearray()

        def mainA():
            rA.subroutine(mainB(), True, 'mainB', True)
            matcher = TestConsumerEvent.createMatcher(rA.mainB)
            for i in range(0, 10):
                for ms in rA.waitForSend(TestConsumerEvent(rA.mainroutine)):
                    yield ms
                output.extend(b'A')
                yield (matcher, )

        def mainB():
            matcher = TestConsumerEvent.createMatcher(producer=rA.mainroutine)
            while True:
                yield (matcher, )
                rA.event.canignore = True
                output.extend(b'B')
                for ms in rA.waitForSend(
                        TestConsumerEvent(rA.mainB, canignore=True)):
                    yield ms

        rA.main = mainA
        rA.start()
        scheduler.main()
        self.assertEqual(output, b'ABABABABABABABABABAB')
Ejemplo n.º 6
0
 def testTimer(self):
     scheduler = Scheduler()
     rA = RoutineContainer(scheduler)
     output = bytearray()
     async def wait(timeout, append):
         await rA.wait_with_timeout(timeout)
         output.extend(append)
     rA.subroutine(wait(0.1, b'B'))
     rA.subroutine(wait(0.5, b'D'))
     rA.subroutine(wait(0, b'A'))
     rA.subroutine(wait(0.2, b'C'))
     curr_time = time()
     scheduler.main()
     end_time = time()
     self.assertEqual(output, b'ABCD')
     self.assertTrue(0.4 < end_time - curr_time < 0.6)
Ejemplo n.º 7
0
 def testWaitForAll(self):
     scheduler = Scheduler()
     rA = RoutineContainer(scheduler)
     async def sender(num):
         await rA.wait_for_send(TestEvent(num))
     result = []
     async def test():
         try:
             for i in range(10):
                 rA.subroutine(sender(i), False)
             matchers = [TestEvent.createMatcher(i) for i in range(10)]
             _, eventdict = await rA.wait_for_all(*matchers)
             for m in matchers:
                 self.assertIn(m, eventdict)
                 self.assertTrue(m.isMatch(eventdict[m]))
         except Exception as e:
             result.append(e)
             raise
         else:
             result.append(False)
     rA.main = test
     rA.start()
     scheduler.main()
     self.assertIs(result[0], False)