Ejemplo n.º 1
0
def list_modules(root_package = 'vlcp'):
    '''
    Walk through all the sub modules, find subclasses of vlcp.server.module.Module,
    list their apis through apidefs
    '''
    pkg = __import__(root_package, fromlist=['_'])
    module_dict = OrderedDict()
    _server = Server()
    for imp, module, _ in walk_packages(pkg.__path__, root_package + '.'):
        m = __import__(module, fromlist = ['_'])
        for name, v in vars(m).items():
            if v is not None and isinstance(v, type) and issubclass(v, Module) \
                    and v is not Module \
                    and not isinstance(v, _ProxyModule) \
                    and hasattr(v, '__dict__') and 'configkey' in v.__dict__ \
                    and v.__module__ == module:
                module_name = v.__name__.lower()
                if module_name not in module_dict:
                    _inst = v(_server)
                    module_info = OrderedDict((('class', v.__module__ + '.' + v.__name__),
                                                ('dependencies', [d.__name__.lower()
                                                                  for d in v.depends]),
                                                ('classdescription', getdoc(v)),
                                                ('apis', [])))
                    if hasattr(_inst, 'apiHandler'):
                        apidefs = _inst.apiHandler.apidefs
                        module_info['apis'] = [(d[0], d[3])
                                               for d in apidefs
                                               if len(d) > 3 and \
                                               not d[0].startswith('public/')]
                    module_dict[module_name] = module_info
    return module_dict
Ejemplo n.º 2
0
 def setUp(self):
     self.server = Server()
Ejemplo n.º 3
0
class Test(unittest.TestCase):
    def setUp(self):
        self.server = Server()

    def tearDown(self):
        pass

    def testLock(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        async def routineLock(key):
            l = Lock(key, rc.scheduler)
            await l.lock(rc)
            t = obj[0]
            await rc.do_events()
            obj[0] = t + 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)

    def testWith(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        async def routineLock(key):
            l = Lock(key, rc.scheduler)
            await l.lock(rc)
            with l:
                t = obj[0]
                await rc.do_events()
                obj[0] = t + 1

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)

    def testAsyncWith(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        async def routineLock(key):
            l = Lock(key, rc.scheduler)
            async with l:
                t = obj[0]
                await rc.do_events()
                obj[0] = t + 1

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)

    def testLock2(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        async def routineLock(key):
            l = Lock(key, rc.scheduler)
            await l.lock(rc)
            t = obj[0]
            await rc.do_events()
            obj[0] = t + 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj2'))
        self.server.serve()
        self.assertEqual(obj[0], 1)

    def testTrylock(self):
        rc = RoutineContainer(self.server.scheduler)
        result = []

        async def routineTrylock(key):
            l = Lock(key, rc.scheduler)
            locked = l.trylock()
            result.append(locked)
            await rc.do_events()
            l.unlock()

        rc.subroutine(routineTrylock('testobj'))
        rc.subroutine(routineTrylock('testobj'))
        self.server.serve()
        self.assertEqual(result, [True, False])

    def testBeginlock(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        async def routineLock(key):
            l = Lock(key, rc.scheduler)
            locked = l.beginlock(rc)
            if not locked:
                await rc.wait_with_timeout(0.2)
                locked = l.trylock()
                if not locked:
                    raise ValueError('Not locked')
            t = obj[0]
            await rc.do_events()
            obj[0] = t + 1
            l.unlock()
            await rc.do_events()
            await l.lock(rc)
            t = obj[0]
            await rc.do_events()
            obj[0] = t + 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 4)

    def testBeginlock2(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        async def routineLock(key):
            l = Lock(key, rc.scheduler)
            locked = l.beginlock(rc)
            if not locked:
                await rc.wait_with_timeout(0.2)
                await l.lock(rc)
            t = obj[0]
            await rc.do_events()
            obj[0] = t + 1
            l.unlock()
            await rc.do_events()
            await l.lock(rc)
            t = obj[0]
            if t != 2:
                obj[0] = t - 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)

    def testSemaphore(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        async def routineLock(key):
            l = Lock(key, rc.scheduler)
            await l.lock(rc)
            t = obj[0]
            await rc.do_events()
            obj[0] = t + 1
            l.unlock()

        async def main_routine():
            smp = Semaphore('testobj', 2, rc.scheduler)
            smp.create()
            await rc.execute_all([
                routineLock('testobj'),
                routineLock('testobj'),
                routineLock('testobj'),
                routineLock('testobj')
            ])
            await smp.destroy(rc)

        rc.subroutine(main_routine())
        self.server.serve()
        self.assertEqual(obj[0], 2)

    def testManyLocks(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0] * 1

        async def routineLock(key, i):
            l = Lock(key, rc.scheduler)
            async with l:
                t = obj[i]
                await rc.do_events()
                obj[i] = t + 1

        for i in range(1):
            for _ in range(10000):
                rc.subroutine(routineLock('testobj' + str(i), i))
        begin = time()
        self.server.serve()
        self.assertListEqual(obj, [10000] * 1)
        print("10000 locks in %r seconds" % (time() - begin, ))
Ejemplo n.º 4
0
class Test(unittest.TestCase):
    def setUp(self):
        self.server = Server()

    def tearDown(self):
        pass

    def testLock(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        def routineLock(key):
            l = Lock(key, rc.scheduler)
            for m in l.lock(rc):
                yield m
            t = obj[0]
            for m in rc.waitWithTimeout(0.5):
                yield m
            obj[0] = t + 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)

    def testWith(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        def routineLock(key):
            l = Lock(key, rc.scheduler)
            for m in l.lock(rc):
                yield m
            with l:
                t = obj[0]
                for m in rc.waitWithTimeout(0.5):
                    yield m
                obj[0] = t + 1

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)

    def testLock2(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        def routineLock(key):
            l = Lock(key, rc.scheduler)
            for m in l.lock(rc):
                yield m
            t = obj[0]
            for m in rc.waitWithTimeout(0.5):
                yield m
            obj[0] = t + 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj2'))
        self.server.serve()
        self.assertEqual(obj[0], 1)

    def testTrylock(self):
        rc = RoutineContainer(self.server.scheduler)
        result = []

        def routineTrylock(key):
            l = Lock(key, rc.scheduler)
            locked = l.trylock()
            result.append(locked)
            for m in rc.waitWithTimeout(0.5):
                yield m
            l.unlock()

        rc.subroutine(routineTrylock('testobj'))
        rc.subroutine(routineTrylock('testobj'))
        self.server.serve()
        self.assertEqual(result, [True, False])

    def testBeginlock(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        def routineLock(key):
            l = Lock(key, rc.scheduler)
            locked = l.beginlock(rc)
            if not locked:
                for m in rc.waitWithTimeout(1.0):
                    yield m
                locked = l.trylock()
                if not locked:
                    raise ValueError('Not locked')
            t = obj[0]
            for m in rc.waitWithTimeout(0.5):
                yield m
            obj[0] = t + 1
            l.unlock()
            for m in rc.doEvents():
                yield m
            for m in l.lock(rc):
                yield m
            t = obj[0]
            for m in rc.waitWithTimeout(1.0):
                yield m
            obj[0] = t + 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 4)

    def testBeginlock2(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]

        def routineLock(key):
            l = Lock(key, rc.scheduler)
            locked = l.beginlock(rc)
            if not locked:
                for m in rc.waitWithTimeout(0.5):
                    yield m
                for m in l.lock(rc):
                    yield m
            t = obj[0]
            for m in rc.waitWithTimeout(1.0):
                yield m
            obj[0] = t + 1
            l.unlock()
            for m in rc.doEvents():
                yield m
            for m in l.lock(rc):
                yield m
            t = obj[0]
            if t != 2:
                obj[0] = t - 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)

    def testSemaphore(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]
        smp = Semaphore('testobj', 2, rc.scheduler)
        smp.create()

        def routineLock(key):
            l = Lock(key, rc.scheduler)
            for m in l.lock(rc):
                yield m
            t = obj[0]
            for m in rc.waitWithTimeout(0.5):
                yield m
            obj[0] = t + 1
            l.unlock()

        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)
Ejemplo n.º 5
0
 def setUp(self):
     self.server = Server()
     self.container = RoutineContainer(self.server.scheduler)
     self.taskpool = TaskPool(self.server.scheduler)
     self.taskpool.start()
Ejemplo n.º 6
0
class Test(unittest.TestCase):


    def setUp(self):
        self.server = Server()
        self.container = RoutineContainer(self.server.scheduler)
        self.taskpool = TaskPool(self.server.scheduler)
        self.taskpool.start()

    def tearDown(self):
        pass


    def testTask(self):
        result = []
        def routine():
            def t():
                return 1
            for m in self.taskpool.runTask(self.container, t):
                yield m
            result.append(self.container.retvalue)
        self.container.subroutine(routine())
        self.server.serve()
        self.assertEqual(result, [1])
    def testGenTask(self):
        result = []
        def routine():
            def t():
                for i in range(0,10):
                    yield (TaskTestEvent(result = i, eof = False),)
                yield (TaskTestEvent(eof = True),)
            for m in self.taskpool.runGenTask(self.container, t):
                yield m
        def routine2():
            tte = TaskTestEvent.createMatcher()
            while True:
                yield (tte,)
                if self.container.event.eof:
                    break
                else:
                    result.append(self.container.event.result)
        self.container.subroutine(routine())
        self.container.subroutine(routine2())
        self.server.serve()
        self.assertEqual(result, [0,1,2,3,4,5,6,7,8,9])
    def testAsyncTask(self):
        result = []
        result2 = []
        def routine():
            def t(sender):
                for i in range(0,10):
                    sender((TaskTestEvent(result = i, eof = False),))
                sender((TaskTestEvent(eof = True),))
                return 1
            for m in self.taskpool.runAsyncTask(self.container, t):
                yield m
            result2.append(self.container.retvalue)
        def routine2():
            tte = TaskTestEvent.createMatcher()
            while True:
                yield (tte,)
                if self.container.event.eof:
                    break
                else:
                    result.append(self.container.event.result)
        self.container.subroutine(routine())
        self.container.subroutine(routine2())
        self.server.serve()
        self.assertEqual(result, [0,1,2,3,4,5,6,7,8,9])
        self.assertEqual(result2, [1])
Ejemplo n.º 7
0
 def setUp(self):
     self.server = Server()
     self.container = RoutineContainer(self.server.scheduler)
     self.taskpool = TaskPool(self.server.scheduler)
     self.taskpool.start()
Ejemplo n.º 8
0
class Test(unittest.TestCase):
    def setUp(self):
        self.server = Server()
        self.container = RoutineContainer(self.server.scheduler)
        self.taskpool = TaskPool(self.server.scheduler)
        self.taskpool.start()

    def tearDown(self):
        pass

    def testTask(self):
        result = []

        def routine():
            def t():
                return 1

            for m in self.taskpool.runTask(self.container, t):
                yield m
            result.append(self.container.retvalue)

        self.container.subroutine(routine())
        self.server.serve()
        self.assertEqual(result, [1])

    def testGenTask(self):
        result = []

        def routine():
            def t():
                for i in range(0, 10):
                    yield (TaskTestEvent(result=i, eof=False), )
                yield (TaskTestEvent(eof=True), )

            for m in self.taskpool.runGenTask(self.container, t):
                yield m

        def routine2():
            tte = TaskTestEvent.createMatcher()
            while True:
                yield (tte, )
                if self.container.event.eof:
                    break
                else:
                    result.append(self.container.event.result)

        self.container.subroutine(routine())
        self.container.subroutine(routine2())
        self.server.serve()
        self.assertEqual(result, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    def testAsyncTask(self):
        result = []
        result2 = []

        def routine():
            def t(sender):
                for i in range(0, 10):
                    sender((TaskTestEvent(result=i, eof=False), ))
                sender((TaskTestEvent(eof=True), ))
                return 1

            for m in self.taskpool.runAsyncTask(self.container, t):
                yield m
            result2.append(self.container.retvalue)

        def routine2():
            tte = TaskTestEvent.createMatcher()
            while True:
                yield (tte, )
                if self.container.event.eof:
                    break
                else:
                    result.append(self.container.event.result)

        self.container.subroutine(routine())
        self.container.subroutine(routine2())
        self.server.serve()
        self.assertEqual(result, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(result2, [1])
Ejemplo n.º 9
0
 def setUp(self):
     self.server = Server()
     self.rc = RoutineContainer(self.server.scheduler)
Ejemplo n.º 10
0
class Test(unittest.TestCase):
    def setUp(self):
        self.server = Server()
        self.rc = RoutineContainer(self.server.scheduler)
    def tearDown(self):
        pass
    
    def test_future1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        def routine_sender():
            future.set_result('abc')
            if False:
                yield
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send after wait
        obj = [0]
        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            future.set_result('abc')
        def routine_receiver():
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_nowait1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]
        def routine_sender():
            future.set_result('abc')
            if False:
                yield
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            obj[0] = future.done()
            obj[1] = future.result()
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [True, 'abc'])

    def test_future_nowait2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]
        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            future.set_result('abc')
        def routine_receiver():
            obj[0] = future.done()
            obj[1] = future.result()
            if False:
                yield
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [False, None])

    def test_future_exception1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        def routine_sender():
            future.set_exception(ValueError('abc'))
            if False:
                yield
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_exception2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            future.set_exception(ValueError('abc'))
        def routine_receiver():
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_nowait_exception1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]
        def routine_sender():
            future.set_exception(ValueError('abc'))
            if False:
                yield
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                obj[0] = future.done()
                obj[1] = future.result()
            except ValueError as exc:
                obj[1] = str(exc)
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [True, 'abc'])

    def test_future_nowait_exception2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]
        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            future.set_exception(ValueError('abc'))
        def routine_receiver():
            try:
                obj[0] = future.done()
                obj[1] = future.result()
            except ValueError as exc:
                obj[1] = str(exc)
            if False:
                yield
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [False, None])
    
    def test_ensure_result(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        def routine_sender():
            with future.ensure_result():
                future.set_result('abc')
            if False:
                yield
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_ensure_result2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]
        def routine_sender():
            try:
                with future.ensure_result():
                    raise ValueError('abc')
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False
            if False:
                yield
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], True)

    def test_ensure_result3(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]
        def routine_sender():
            try:
                with future.ensure_result(True):
                    raise ValueError('abc')
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False
            if False:
                yield
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], False)

    def test_ensure_result4(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]
        def routine_sender():
            try:
                with future.ensure_result(False, 'abc'):
                    pass
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False
            if False:
                yield
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue
        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], False)

    def test_routine1(self):
        rc = self.rc
        # Test send before wait
        obj = [0]
        def routine_sender():
            rc.retvalue = 'abc'
            if False:
                yield
        future = RoutineFuture(routine_sender(), rc)
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine2(self):
        rc = self.rc
        # Test send before wait
        obj = [0]
        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            rc.retvalue = 'abc'
        future = RoutineFuture(routine_sender(), rc)
        def routine_receiver():
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine_exception1(self):
        rc = self.rc
        # Test send before wait
        obj = [0]
        def routine_sender():
            raise ValueError('abc')
            if False:
                yield
        future = RoutineFuture(routine_sender(), rc)
        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine_exception2(self):
        rc = self.rc
        # Test send before wait
        obj = [0]
        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            raise ValueError('abc')
        future = RoutineFuture(routine_sender(), rc)
        def routine_receiver():
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
Ejemplo n.º 11
0
 def setUp(self):
     self.server = Server()
     self.rc = RoutineContainer(self.server.scheduler)
Ejemplo n.º 12
0
class Test(unittest.TestCase):
    def setUp(self):
        self.server = Server()
        self.rc = RoutineContainer(self.server.scheduler)

    def tearDown(self):
        pass

    def test_future1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        async def routine_sender():
            future.set_result('abc')

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            obj[0] = await future.wait()

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send after wait
        obj = [0]

        async def routine_sender():
            await rc.wait_with_timeout(0.1)
            future.set_result('abc')

        async def routine_receiver():
            obj[0] = await future.wait()

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_await1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        async def routine_sender():
            future.set_result('abc')

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            obj[0] = await future

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_await2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send after wait
        obj = [0]

        async def routine_sender():
            await rc.wait_with_timeout(0.1)
            future.set_result('abc')

        async def routine_receiver():
            obj[0] = await future

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_nowait1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]

        async def routine_sender():
            future.set_result('abc')

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            obj[0] = future.done()
            obj[1] = future.result()

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [True, 'abc'])

    def test_future_nowait2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]

        async def routine_sender():
            await rc.wait_with_timeout(0.1)
            future.set_result('abc')

        async def routine_receiver():
            obj[0] = future.done()
            obj[1] = future.result()

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [False, None])

    def test_future_exception1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        async def routine_sender():
            future.set_exception(ValueError('abc'))

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            try:
                await future.wait()
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_exception2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        async def routine_sender():
            await rc.wait_with_timeout(0.1)
            future.set_exception(ValueError('abc'))

        async def routine_receiver():
            try:
                await future.wait()
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_nowait_exception1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]

        async def routine_sender():
            future.set_exception(ValueError('abc'))

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            try:
                obj[0] = future.done()
                obj[1] = future.result()
            except ValueError as exc:
                obj[1] = str(exc)

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [True, 'abc'])

    def test_future_nowait_exception2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]

        async def routine_sender():
            await rc.wait_with_timeout(0.1)
            future.set_exception(ValueError('abc'))

        async def routine_receiver():
            try:
                obj[0] = future.done()
                obj[1] = future.result()
            except ValueError as exc:
                obj[1] = str(exc)

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [False, None])

    def test_ensure_result(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        async def routine_sender():
            with future.ensure_result():
                future.set_result('abc')

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            obj[0] = await future.wait()

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_ensure_result2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]

        async def routine_sender():
            try:
                with future.ensure_result():
                    raise ValueError('abc')
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            try:
                await future.wait()
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], True)

    def test_ensure_result3(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]

        async def routine_sender():
            try:
                with future.ensure_result(True):
                    raise ValueError('abc')
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            try:
                await future.wait()
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], False)

    def test_ensure_result4(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]

        async def routine_sender():
            try:
                with future.ensure_result(False, 'abc'):
                    pass
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            obj[0] = await future.wait()

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], False)

    def test_routine1(self):
        rc = self.rc
        # Test send before wait
        obj = [0]

        async def routine_sender():
            return 'abc'

        future = RoutineFuture(routine_sender(), rc)

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            obj[0] = await future.wait()

        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine2(self):
        rc = self.rc
        # Test send before wait
        obj = [0]

        async def routine_sender():
            await rc.wait_with_timeout(0.1)
            return 'abc'

        future = RoutineFuture(routine_sender(), rc)

        async def routine_receiver():
            obj[0] = await future.wait()

        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine_exception1(self):
        rc = self.rc
        # Test send before wait
        obj = [0]

        async def routine_sender():
            raise ValueError('abc')

        future = RoutineFuture(routine_sender(), rc)

        async def routine_receiver():
            await rc.wait_with_timeout(0.1)
            try:
                await future.wait()
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine_exception2(self):
        rc = self.rc
        # Test send before wait
        obj = [0]

        async def routine_sender():
            await rc.wait_with_timeout(0.1)
            raise ValueError('abc')

        future = RoutineFuture(routine_sender(), rc)

        async def routine_receiver():
            try:
                await future.wait()
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
Ejemplo n.º 13
0
class Test(unittest.TestCase):
    def setUp(self):
        self.server = Server()
        self.rc = RoutineContainer(self.server.scheduler)

    def tearDown(self):
        pass

    def test_future1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        def routine_sender():
            future.set_result('abc')
            if False:
                yield

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send after wait
        obj = [0]

        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            future.set_result('abc')

        def routine_receiver():
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_nowait1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]

        def routine_sender():
            future.set_result('abc')
            if False:
                yield

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            obj[0] = future.done()
            obj[1] = future.result()

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [True, 'abc'])

    def test_future_nowait2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]

        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            future.set_result('abc')

        def routine_receiver():
            obj[0] = future.done()
            obj[1] = future.result()
            if False:
                yield

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [False, None])

    def test_future_exception1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        def routine_sender():
            future.set_exception(ValueError('abc'))
            if False:
                yield

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_exception2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            future.set_exception(ValueError('abc'))

        def routine_receiver():
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_future_nowait_exception1(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]

        def routine_sender():
            future.set_exception(ValueError('abc'))
            if False:
                yield

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                obj[0] = future.done()
                obj[1] = future.result()
            except ValueError as exc:
                obj[1] = str(exc)

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [True, 'abc'])

    def test_future_nowait_exception2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [None, None]

        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            future.set_exception(ValueError('abc'))

        def routine_receiver():
            try:
                obj[0] = future.done()
                obj[1] = future.result()
            except ValueError as exc:
                obj[1] = str(exc)
            if False:
                yield

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj, [False, None])

    def test_ensure_result(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]

        def routine_sender():
            with future.ensure_result():
                future.set_result('abc')
            if False:
                yield

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_ensure_result2(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]

        def routine_sender():
            try:
                with future.ensure_result():
                    raise ValueError('abc')
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False
            if False:
                yield

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], True)

    def test_ensure_result3(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]

        def routine_sender():
            try:
                with future.ensure_result(True):
                    raise ValueError('abc')
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False
            if False:
                yield

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], False)

    def test_ensure_result4(self):
        rc = self.rc
        future = Future(self.server.scheduler)
        # Test send before wait
        obj = [0]
        obj2 = [0]

        def routine_sender():
            try:
                with future.ensure_result(False, 'abc'):
                    pass
            except ValueError:
                obj2[0] = True
            else:
                obj2[0] = False
            if False:
                yield

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue

        rc.subroutine(routine_sender())
        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
        self.assertEqual(obj2[0], False)

    def test_routine1(self):
        rc = self.rc
        # Test send before wait
        obj = [0]

        def routine_sender():
            rc.retvalue = 'abc'
            if False:
                yield

        future = RoutineFuture(routine_sender(), rc)

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue

        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine2(self):
        rc = self.rc
        # Test send before wait
        obj = [0]

        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            rc.retvalue = 'abc'

        future = RoutineFuture(routine_sender(), rc)

        def routine_receiver():
            for m in future.wait(rc):
                yield m
            obj[0] = rc.retvalue

        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine_exception1(self):
        rc = self.rc
        # Test send before wait
        obj = [0]

        def routine_sender():
            raise ValueError('abc')
            if False:
                yield

        future = RoutineFuture(routine_sender(), rc)

        def routine_receiver():
            for m in rc.waitWithTimeout(0.1):
                yield m
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')

    def test_routine_exception2(self):
        rc = self.rc
        # Test send before wait
        obj = [0]

        def routine_sender():
            for m in rc.waitWithTimeout(0.1):
                yield m
            raise ValueError('abc')

        future = RoutineFuture(routine_sender(), rc)

        def routine_receiver():
            try:
                for m in future.wait(rc):
                    yield m
            except ValueError as exc:
                obj[0] = str(exc)
            else:
                obj[0] = None

        rc.subroutine(routine_receiver())
        self.server.serve()
        self.assertEqual(obj[0], 'abc')
Ejemplo n.º 14
0
 def setUp(self):
     self.server = Server()
Ejemplo n.º 15
0
class Test(unittest.TestCase):
    def setUp(self):
        self.server = Server()
    def tearDown(self):
        pass
    
    def testLock(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]
        def routineLock(key):
            l = Lock(key, rc.scheduler)
            for m in l.lock(rc):
                yield m
            t = obj[0]
            for m in rc.waitWithTimeout(0.5):
                yield m
            obj[0] = t + 1
            l.unlock()
        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)
    def testWith(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]
        def routineLock(key):
            l = Lock(key, rc.scheduler)
            for m in l.lock(rc):
                yield m
            with l:
                t = obj[0]
                for m in rc.waitWithTimeout(0.5):
                    yield m
                obj[0] = t + 1
        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)
    def testLock2(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]
        def routineLock(key):
            l = Lock(key, rc.scheduler)
            for m in l.lock(rc):
                yield m
            t = obj[0]
            for m in rc.waitWithTimeout(0.5):
                yield m
            obj[0] = t + 1
            l.unlock()
        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj2'))
        self.server.serve()
        self.assertEqual(obj[0], 1)
    def testTrylock(self):
        rc = RoutineContainer(self.server.scheduler)
        result = []
        def routineTrylock(key):
            l = Lock(key, rc.scheduler)
            locked = l.trylock()
            result.append(locked)
            for m in rc.waitWithTimeout(0.5):
                yield m
            l.unlock()
        rc.subroutine(routineTrylock('testobj'))
        rc.subroutine(routineTrylock('testobj'))
        self.server.serve()
        self.assertEqual(result, [True, False])
    def testBeginlock(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]
        def routineLock(key):
            l = Lock(key, rc.scheduler)
            locked = l.beginlock(rc)
            if not locked:
                for m in rc.waitWithTimeout(1.0):
                    yield m
                locked = l.trylock()
                if not locked:
                    raise ValueError('Not locked')
            t = obj[0]
            for m in rc.waitWithTimeout(0.5):
                yield m
            obj[0] = t + 1
            l.unlock()
            for m in rc.doEvents():
                yield m
            for m in l.lock(rc):
                yield m
            t = obj[0]
            for m in rc.waitWithTimeout(1.0):
                yield m
            obj[0] = t + 1
            l.unlock()
        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 4)
    def testBeginlock2(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]
        def routineLock(key):
            l = Lock(key, rc.scheduler)
            locked = l.beginlock(rc)
            if not locked:
                for m in rc.waitWithTimeout(0.5):
                    yield m
                for m in l.lock(rc):
                    yield m
            t = obj[0]
            for m in rc.waitWithTimeout(1.0):
                yield m
            obj[0] = t + 1
            l.unlock()
            for m in rc.doEvents():
                yield m
            for m in l.lock(rc):
                yield m
            t = obj[0]
            if t != 2:
                obj[0] = t - 1
            l.unlock()
        rc.subroutine(routineLock('testobj'))
        rc.subroutine(routineLock('testobj'))
        self.server.serve()
        self.assertEqual(obj[0], 2)
    def testSemaphore(self):
        rc = RoutineContainer(self.server.scheduler)
        obj = [0]
        def routineLock(key):
            l = Lock(key, rc.scheduler)
            for m in l.lock(rc):
                yield m
            t = obj[0]
            for m in rc.waitWithTimeout(0.5):
                yield m
            obj[0] = t + 1
            l.unlock()
        def main_routine():
            smp = Semaphore('testobj', 2, rc.scheduler)
            smp.create()
            for m in rc.executeAll([routineLock('testobj'),
                                    routineLock('testobj'),
                                    routineLock('testobj'),
                                    routineLock('testobj')], retnames = ()):
                yield m
            for m in smp.destroy(rc):
                yield m
        rc.subroutine(main_routine())
        self.server.serve()
        self.assertEqual(obj[0], 2)