def test_lookup_client_events(): # intiallize the lookupd server loop = pyuv.Loop.default_loop() r = Registry(loop) sock = bind_sockets(LOOKUPD_ADDR) io_loop = IOLoop(_loop=loop) server = http_server(io_loop, sock, registration_db=r) server.start() lookup_address = "http://%s" % LOOKUPD_ADDR client = LookupServer(lookup_address, loop) channel = client.lookup() emitted = [] def cb(event, msg): emitted.append((event, msg)) channel.bind_all(cb) channel.start() http_handler = HttpHandler( MockConfig(bind=GAFFERD_ADDR, lookupd_addresses=[lookup_address])) m = Manager(loop=loop) t = pyuv.Timer(loop) t0 = pyuv.Timer(loop) def do_stop(h): channel.close() server.stop() io_loop.close(True) def stop_server(m): t.start(do_stop, 0.4, 0.0) def wait(h): m.stop(stop_server) def on_manager(h): # start the manager with the HTTP API m.start(apps=[http_handler]) testfile, cmd, args, wdir = dummy_cmd() config = ProcessConfig("dummy", cmd, args=args, cwd=wdir) m.load(config) m.stop_process(1) m.unload("dummy") h.start(wait, 0.3, 0.0) t0.start(on_manager, 0.3, 0.0) loop.run() actions = [line[0] for line in emitted] assert len(emitted) == 7 assert list(actions) == [ 'add_node', 'identify', 'add_job', 'add_process', 'remove_process', 'remove_job', 'remove_node' ]
def get_server(loop, received): io_loop = IOLoop(_loop=loop) test_handlers = [(r'/([^/]+)', TestHandler)] app = Application(test_handlers, received=received) server = HTTPServer(app, io_loop=io_loop) [sock] = netutil.bind_sockets(TEST_PORT, address=TEST_HOST) server.add_socket(sock) return server
def test_basic(): loop = pyuv.Loop.default_loop() io_loop = IOLoop(_loop=loop) HelloRouter = sockjs.SockJSRouter(HelloConnection, '/echo', io_loop=io_loop) ws = HelloClient(loop, TEST_URL) app = tornado.web.Application(HelloRouter.urls) server = HTTPServer(app, io_loop=io_loop) [sock] = netutil.bind_sockets(TEST_PORT, address=TEST_HOST) server.add_socket(sock) server.start() ws.start() t = pyuv.Timer(loop) t1 = pyuv.Timer(loop) start = time.time() def do_stop(handle): handle.close() server.stop() ws.close() io_loop.close() def init(handle): handle.close() ws.write_message("hello") t1.start(do_stop, 0.2, 0.0) t.start(init, 0.2, 0.0) loop.run() assert h_opened == [True] assert c_opened == [True] assert h_messages == ["hello"] assert c_messages == ["hello"]
def on_close(self): # Remove client from the clients list and broadcast leave message self.participants.remove(self) self.broadcast(self.participants, "Someone left.") if __name__ == "__main__": import logging logging.getLogger().setLevel(logging.DEBUG) import pyuv from gaffer.tornado_pyuv import IOLoop loop = pyuv.Loop.default_loop() ioloop = IOLoop(_loop=loop) # 1. Create chat router ChatRouter = sockjs.SockJSRouter(ChatConnection, '/chat', io_loop=ioloop) # 2. Create Tornado application app = tornado.web.Application( [(r"/", IndexHandler)] + ChatRouter.urls ) # 3. Make Tornado app listen on port 8080 app.listen(8080, io_loop=ioloop) # 4. Start IOLoop while True:
def test_lookup_client(): # intiallize the lookupd server loop = pyuv.Loop.default_loop() r = Registry(loop) sock = bind_sockets(LOOKUPD_ADDR) io_loop = IOLoop(_loop=loop) server = http_server(io_loop, sock, registration_db=r) server.start() lookup_address = "http://%s" % LOOKUPD_ADDR client = LookupServer(lookup_address, loop) # start the manager with the HTTP API http_handler = HttpHandler( MockConfig(bind=GAFFERD_ADDR, lookupd_addresses=[lookup_address])) m = Manager(loop=loop) m.start(apps=[http_handler]) time.sleep(0.1) testfile, cmd, args, wdir = dummy_cmd() config = ProcessConfig("dummy", cmd, args=args, cwd=wdir) m.load(config) t = pyuv.Timer(loop) def do_stop(h): server.stop() io_loop.close(True) def stop_server(m): t.start(do_stop, 0.1, 0.0) results = [] def collect_info2(h): results.append(client.sessions()) results.append(client.jobs()) m.stop(stop_server) def collect_info(h): results.append(client.nodes()) results.append(client.sessions()) results.append(client.jobs()) results.append(client.find_job("default.dummy")) m.unload("dummy") h.start(collect_info2, 0.3, 0.0) t0 = pyuv.Timer(loop) t0.start(collect_info, 0.3, 0.0) loop.run() assert len(results) == 6 host = hostname() assert "nodes" in results[0] nodes = results[0]['nodes'] assert len(nodes) == 1 assert "name" in nodes[0] assert nodes[0]['name'] == host assert "sessions" in results[1] assert "nb_sessions" in results[1] assert results[1]["nb_sessions"] == 1 sessions = results[1]["sessions"] assert isinstance(sessions, list) assert len(sessions) == 1 session = sessions[0] assert session['sessionid'] == "default" assert "jobs" in session jobs = session["jobs"] assert isinstance(jobs, dict) assert "default.dummy" in jobs job = jobs["default.dummy"] assert isinstance(job, list) assert job[0]["name"] == host assert job[0]["pids"] == [1] node_info = job[0]["node_info"] assert node_info["name"] == host assert "jobs" in results[2] assert "nb_jobs" in results[2] assert results[2]["nb_jobs"] == 1 job1 = results[2]["jobs"][0] job1["name"] == "default.dummy" assert len(job1["sources"]) == 1 assert job1["sources"] == job assert "sources" in results[3] assert len(results[3]["sources"]) == 1 assert results[3]["sources"] == job assert results[4]["sessions"] == [] assert results[4]["nb_sessions"] == 0 assert results[5]["jobs"] == [] assert results[5]["nb_jobs"] == 0
def test_lookup_manager(): # intiallize the lookupd server loop = pyuv.Loop.default_loop() r = Registry(loop) sock = bind_sockets(LOOKUPD_ADDR) io_loop = IOLoop(_loop=loop) server = http_server(io_loop, sock, registration_db=r) server.start() # subscribe to events emitted = [] def cb(event, message): emitted.append((event, message)) r.bind_all(cb) # start the manager with the HTTP API http_handler = HttpHandler( MockConfig(bind=GAFFERD_ADDR, lookupd_addresses=["http://%s" % LOOKUPD_ADDR])) m = Manager(loop=loop) m.start(apps=[http_handler]) testfile, cmd, args, wdir = dummy_cmd() config = ProcessConfig("dummy", cmd, args=args, cwd=wdir) m.load(config) m.stop_process(1) m.unload("dummy") t = pyuv.Timer(loop) def do_stop(h): server.stop() io_loop.close(True) def stop_server(m): t.start(do_stop, 0.4, 0.0) m.stop(stop_server) loop.run() assert len(emitted) == 7 actions = [line[0] for line in emitted] assert list(actions) == [ 'add_node', 'identify', 'add_job', 'add_process', 'remove_process', 'remove_job', 'remove_node' ] assert isinstance(emitted[0][1], GafferNode) assert isinstance(emitted[1][1], GafferNode) assert isinstance(emitted[2][1], dict) assert "job_name" in emitted[2][1] assert emitted[2][1]['job_name'] == "default.dummy" assert isinstance(emitted[3][1], dict) assert "job_name" in emitted[3][1] assert emitted[3][1]['job_name'] == "default.dummy" assert "pid" in emitted[3][1] assert emitted[3][1]['pid'] == 1 assert isinstance(emitted[4][1], dict) assert "job_name" in emitted[4][1] assert emitted[4][1]['job_name'] == "default.dummy" assert "pid" in emitted[4][1] assert emitted[4][1]['pid'] == 1 assert isinstance(emitted[5][1], dict) assert emitted[5][1]['job_name'] == "default.dummy" assert isinstance(emitted[6][1], GafferNode) assert emitted[6][1].sessions == {}
def test_lookup_service(): loop = pyuv.Loop.default_loop() r = Registry(loop) sock = bind_sockets(LOOKUPD_ADDR) io_loop = IOLoop(_loop=loop) server = http_server(io_loop, sock, registration_db=r) server.start() emitted = [] def cb(event, message): emitted.append((event, message)) r.bind_all(cb) client = LookupClient(loop, "ws://%s/ws" % LOOKUPD_ADDR) client.start() messages = [] messages.append(client.identify("c1", "broadcast", 1.0)) messages.append(client.ping()) messages.append(client.add_job("a.job1")) messages.append(client.add_process("a.job1", 1)) messages.append(client.remove_process("a.job1", 1)) messages.append(client.remove_job("a.job1")) t0 = pyuv.Timer(loop) t0.start(lambda h: client.close(), 0.4, 0.0) def stop(h): h.close() server.stop() io_loop.close() t = pyuv.Timer(loop) t.start(stop, 0.6, 0.0) loop.run() assert len(messages) == 6 results = ["ok" in msg.result() for msg in messages] assert results == [True, True, True, True, True, True] assert len(emitted) == 7 actions = [line[0] for line in emitted] assert list(actions) == [ 'add_node', 'identify', 'add_job', 'add_process', 'remove_process', 'remove_job', 'remove_node' ] assert isinstance(emitted[0][1], GafferNode) assert isinstance(emitted[1][1], GafferNode) assert isinstance(emitted[2][1], dict) assert "job_name" in emitted[2][1] assert emitted[2][1]['job_name'] == "a.job1" assert isinstance(emitted[3][1], dict) assert "job_name" in emitted[3][1] assert emitted[3][1]['job_name'] == "a.job1" assert "pid" in emitted[3][1] assert emitted[3][1]['pid'] == 1 assert isinstance(emitted[4][1], dict) assert "job_name" in emitted[4][1] assert emitted[4][1]['job_name'] == "a.job1" assert "pid" in emitted[4][1] assert emitted[4][1]['pid'] == 1 assert isinstance(emitted[5][1], dict) assert emitted[5][1]['job_name'] == "a.job1" assert isinstance(emitted[6][1], GafferNode) assert emitted[6][1].sessions == {}