def run(self, result=None): """Override the TestCase run method to launch the test server. This seems to be the cleanest solution for effectively using a context manager as part of a test fixture. This method then calls the usual test fixture setUp and tearDown within this context. """ with syncRunServer(TestServer()): super(ClientTests, self).run(result)
def test_refresh(): with labrad.connect() as cxn: assert not hasattr(cxn, 'refresh_test_server') with util.syncRunServer(RefreshServer1()): cxn.refresh() assert hasattr(cxn, 'refresh_test_server') rts = cxn.refresh_test_server assert hasattr(rts, 'greet') assert rts.greet.accepts == ['_'] assert rts.greet.returns == ['s'] assert hasattr(rts, 'go_away') assert rts.go_away.accepts == ['_'] assert rts.go_away.returns == ['_'] assert rts.greet() == 'hello!' cxn.refresh() assert not hasattr(cxn, 'refresh_test_server') with util.syncRunServer(RefreshServer2()): cxn.refresh() assert hasattr(cxn, 'refresh_test_server') # check the old rts we got earlier, as well as the cxn attribute for srv in [rts, cxn.refresh_test_server]: assert hasattr(srv, 'greet') assert srv.greet.accepts == ['s'] assert srv.greet.returns == ['s'] assert hasattr(srv, 'add') assert srv.add.accepts == ['(ii)'] assert srv.add.returns == ['i'] assert not hasattr(srv, 'go_away') assert srv.greet('tester') == 'hello, tester!' assert srv.add(100, 101) == 201
def test_server_init_failure_is_propagated(): class InitError(Exception): pass class DyingServer(LabradServer): name = "Dying Server" def initServer(self): raise InitError() with pytest.raises(InitError): with util.syncRunServer(DyingServer()): pass
def test_threaded_server_client_can_be_spawned(): class TestServer(ThreadedServer): name = "TestServer" @setting(100) def spawn_call(self, c, server, setting, data): with self.client.spawn() as cxn: return cxn[server][setting](data) with util.syncRunServer(TestServer()): with labrad.connect() as cxn: result = cxn.testserver.spawn_call('Manager', 'Echo', 'woot!') assert result == 'woot!'
def test_sync_run_server(): """Ensure that syncRunServer runs the stopServer method on shutdown.""" event = threading.Event() class TestServer(LabradServer): name = "TestServer" def stopServer(self): event.set() server = TestServer() with util.syncRunServer(server): pass if not event.wait(0.5): raise Exception('event not set in stopServer method')
def test_server_expire_context_method_is_called(): """Ensure that server's expireContext method is called when client disconnects.""" queue = Queue.Queue() class TestServer(LabradServer): name = "TestServer" def expireContext(self, c): queue.put(c.ID) @setting(100) def echo(self, c, data): return data with util.syncRunServer(TestServer()): with labrad.connect() as cxn: # create a random context owned by this connection request_context = (cxn.ID, random.randint(0, 0xFFFFFFFFL)) cxn.testserver.echo('hello, world!', context=request_context) expired_context = queue.get(block=True, timeout=1) assert expired_context == request_context
def test_refresh(): with labrad.connect() as cxn: assert not hasattr(cxn, 'refresh_test_server') with util.syncRunServer(RefreshServer1()): cxn.refresh() assert hasattr(cxn, 'refresh_test_server') rts = cxn.refresh_test_server def type_list(strs): return [T.parseTypeTag(s) for s in strs] def same_types(a, b): return type_list(a) == type_list(b) assert hasattr(rts, 'greet') assert same_types(rts.greet.accepts, ['_']) assert same_types(rts.greet.returns, ['s']) assert hasattr(rts, 'go_away') assert same_types(rts.go_away.accepts, ['_']) assert same_types(rts.go_away.returns, ['_']) assert rts.greet() == 'hello!' # pause before refreshing after server disconnect time.sleep(1) cxn.refresh() assert not hasattr(cxn, 'refresh_test_server') with util.syncRunServer(RefreshServer2()): cxn.refresh() assert hasattr(cxn, 'refresh_test_server') # check the old rts we got earlier, as well as the cxn attribute for srv in [rts, cxn.refresh_test_server]: assert hasattr(srv, 'greet') assert same_types(srv.greet.accepts, ['s']) assert same_types(srv.greet.returns, ['s']) assert hasattr(srv, 'add') assert same_types(srv.add.accepts, ['(ii)']) assert same_types(srv.add.returns, ['i']) assert not hasattr(srv, 'go_away') assert srv.greet('tester') == 'hello, tester!' assert srv.add(100, 101) == 201 # pause before refreshing after server disconnect time.sleep(1) cxn.refresh() assert not hasattr(cxn, 'refresh_test_server') with util.syncRunServer(RefreshServer3()): cxn.refresh() assert hasattr(cxn, 'refresh_test_server') # check the old rts we got earlier, as well as the cxn attribute for srv in [rts, cxn.refresh_test_server]: assert hasattr(rts, 'greet') assert same_types(rts.greet.accepts, ['_']) assert same_types(rts.greet.returns, ['s']) assert hasattr(rts, 'go_away') assert same_types(rts.go_away.accepts, ['_']) assert same_types(rts.go_away.returns, ['_']) assert rts.greet() == 'hello!'