def start_redis(*args, **kwargs): ''' Start redis server using specified arguments and return tuple: (subprocess.Popen object, redis.Redis object, port). Arguments are the same as redis-server command-line arguments, except two hyphens are automatically prepended to all keys in kwargs. Example usage: >>> (p, r, port) = start_redis(logfile='/dev/null') >>> stop_redis(p, r) ''' cmd = ['redis-server'] if 'port' in kwargs: port = int(kwargs['port']) else: port = find_port() cmd.extend(['--port', str(port)]) for (k, v) in kwargs.items(): cmd.extend(['--%s' % k, str(v)]) cmd.extend(args) p = subprocess.Popen(cmd) r = redis.Redis(port=port) while True: time.sleep(0.1) try: ret = r.ping() except redis.ConnectionError: pass else: if ret: break return (p, r, port)
def main(): parser = argparse.ArgumentParser(description='Annotation service for ' 'testing') parser.add_argument('--host', dest='host', action='store', type=str, default='localhost', help='Host on which to listen') parser.add_argument('-p', '--port', dest='port', action='store', type=int, default=None, help='Port on which to listen (default: auto)') args = parser.parse_args() host = args.host if args.port is None: port = find_port() else: port = args.port print('Serving on %s:%d...' % (host, port)) handler = NoopAnnotateCommunicationService() server = AnnotateCommunicationServiceWrapper(handler) server.serve(host, port)
def test_annotate(self): impl = NoopAnnotator() host = 'localhost' port = find_port() timeout = 5 comm_id = '1-2-3-4' comm = create_comm(comm_id) comm_uuid_uuidString = comm.uuid.uuidString comm_metadata_tool = comm.metadata.tool comm_metadata_timestamp = comm.metadata.timestamp with SubprocessAnnotatorServiceWrapper(impl, host, port, timeout=timeout): transport = TSocket.TSocket(host, port) transport = TTransport.TFramedTransport(transport) protocol = TCompactProtocol.TCompactProtocol(transport) cli = Annotator.Client(protocol) transport.open() res = cli.annotate(comm) transport.close() self.assertEqual(res.id, comm_id) self.assertEqual(res.uuid.uuidString, comm_uuid_uuidString) self.assertEqual(res.metadata.tool, comm_metadata_tool) self.assertEqual(res.metadata.timestamp, comm_metadata_timestamp)
def test_search_communications(): impl = FooSearch() host = "localhost" port = find_port() timeout = 5 terms = ["foo", "bar"] query = SearchQuery(type=SearchType.COMMUNICATIONS, terms=[t for t in terms]) with SubprocessSearchServiceWrapper(impl, host, port, timeout=timeout): transport = TSocket.TSocket(host, port) transport = TTransport.TFramedTransport(transport) protocol = TCompactProtocol.TCompactProtocolAccelerated(transport) cli = SearchService.Client(protocol) transport.open() res = cli.search(query) transport.close() assert res.uuid.uuidString == "12345678-1234-5678-1234-567812345678" assert len(res.searchResultItems) == 2 assert res.searchResultItems[0].communicationId == "foo" assert res.searchResultItems[0].score == 42.0 assert res.searchResultItems[1].communicationId == "bar" assert res.searchResultItems[1].score == 42.0 assert res.metadata.tool == "Foo Search"
def test_search_communications(): impl = FooSearch() host = 'localhost' port = find_port() timeout = 5 terms = ['foo', 'bar'] query = SearchQuery(type=SearchType.COMMUNICATIONS, terms=[t for t in terms]) with SubprocessSearchServiceWrapper(impl, host, port, timeout=timeout): transport = TSocket.TSocket(host, port) transport = TTransport.TFramedTransport(transport) protocol = TCompactProtocol.TCompactProtocol(transport) cli = Search.Client(protocol) transport.open() res = cli.search(query) transport.close() assert res.uuid.uuidString == '12345678-1234-5678-1234-567812345678' assert len(res.searchResults) == 2 assert res.searchResults[0].communicationId == 'foo' assert res.searchResults[0].score == 42. assert res.searchResults[1].communicationId == 'bar' assert res.searchResults[1].score == 42. assert res.metadata.tool == 'Foo Search'
def test_annotate(self): impl = NoopAnnotateCommunicationService() host = 'localhost' port = find_port() timeout = 5 comm_id = '1-2-3-4' comm = create_comm(comm_id) comm_uuid_uuidString = comm.uuid.uuidString comm_metadata_tool = comm.metadata.tool comm_metadata_timestamp = comm.metadata.timestamp with SubprocessAnnotateCommunicationServiceWrapper(impl, host, port, timeout=timeout): transport = TSocket.TSocket(host, port) transport = TTransport.TFramedTransport(transport) protocol = TCompactProtocol.TCompactProtocolAccelerated(transport) cli = AnnotateCommunicationService.Client(protocol) transport.open() res = cli.annotate(comm) transport.close() self.assertEqual(res.id, comm_id) self.assertEqual(res.uuid.uuidString, comm_uuid_uuidString) self.assertEqual(res.metadata.tool, comm_metadata_tool) self.assertEqual(res.metadata.timestamp, comm_metadata_timestamp)
def test_get_metadata(self): impl = NoopAnnotateCommunicationService() host = 'localhost' port = find_port() timeout = 5 with SubprocessAnnotateCommunicationServiceWrapper(impl, host, port, timeout=timeout): transport = TSocket.TSocket(host, port) transport = TTransport.TFramedTransport(transport) protocol = TCompactProtocol.TCompactProtocolAccelerated(transport) cli = AnnotateCommunicationService.Client(protocol) transport.open() metadata = cli.getMetadata() transport.close() self.assertEqual(NoopAnnotateCommunicationService.METADATA_TOOL, metadata.tool)
def test_get_metadata(self): impl = NoopAnnotator() host = 'localhost' port = find_port() timeout = 5 with SubprocessAnnotatorServiceWrapper(impl, host, port, timeout=timeout): transport = TSocket.TSocket(host, port) transport = TTransport.TFramedTransport(transport) protocol = TCompactProtocol.TCompactProtocol(transport) cli = Annotator.Client(protocol) transport.open() metadata = cli.getMetadata() transport.close() self.assertEqual(NoopAnnotator.METADATA_TOOL, metadata.tool)
def main(): parser = argparse.ArgumentParser(description='Annotation service for ' 'testing') parser.add_argument('--host', dest='host', action='store', type=str, default='localhost', help='Host on which to listen') parser.add_argument('-p', '--port', dest='port', action='store', type=int, default=None, help='Port on which to listen (default: auto)') args = parser.parse_args() host = args.host if args.port is None: port = find_port() else: port = args.port print 'Serving on %s:%d...' % (host, port) handler = NoopAnnotateCommunicationService() server = AnnotateCommunicationServiceWrapper(handler) server.serve(host, port)