def test_execute_process_handle_input(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident, msg = server.socket.recv() self.assertEqual(msg, process.ExecuteRequest('cat')) server.socket.send(ident, process.ExecuteReply(2345)) ident, msg = server.socket.recv() self.assertEqual( msg, process.StreamAction(2345, process.STDIN, b'asdf\n')) server.socket.send(ident, ack.Acknowledgement()) server.socket.send( ident, process.StreamUpdate(2345, process.STDOUT, b'asdf\n')) ident, msg = server.socket.recv() self.assertEqual(msg, process.StreamAction(2345, process.STDIN)) server.socket.send(ident, ack.Acknowledgement()) server.socket.send(ident, process.StreamUpdate(2345, process.STDOUT)) server.socket.send(ident, process.StreamUpdate(2345, process.STDERR)) server.socket.send(ident, process.ExitUpdate(2345, 0)) with HedgehogClient(ctx, 'inproc://controller') as client: self.assertEqual(client.execute_process('cat'), 2345) self.assertEqual(client.send_process_data(2345, b'asdf\n'), None) self.assertEqual(client.send_process_data(2345), None) thread.join()
def test_execute_process_handle_exit(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident, msg = server.socket.recv() self.assertEqual(msg, process.ExecuteRequest('echo', 'asdf')) server.socket.send(ident, process.ExecuteReply(2345)) server.socket.send( ident, process.StreamUpdate(2345, process.STDOUT, b'asdf\n')) server.socket.send(ident, process.StreamUpdate(2345, process.STDOUT)) server.socket.send(ident, process.StreamUpdate(2345, process.STDERR)) server.socket.send(ident, process.ExitUpdate(2345, 0)) exit_a, exit_b = pipe(ctx) with HedgehogClient(ctx, 'inproc://controller') as client: def on_exit(client, pid, exit_code): self.assertEqual(pid, 2345) self.assertEqual(exit_code, 0) exit_b.signal() self.assertEqual( client.execute_process('echo', 'asdf', on_exit=on_exit), 2345) exit_a.wait() thread.join()
def test_connect(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): pass with HedgehogClient(ctx, 'inproc://controller') as client: pass thread.join()
def test_set_servo(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident, msg = server.socket.recv() self.assertEqual(msg, servo.Action(0, False, 0)) server.socket.send(ident, ack.Acknowledgement()) with HedgehogClient(ctx, 'inproc://controller') as client: self.assertEqual(client.set_servo(0, False, 0), None) thread.join()
def test_get_motor(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident, msg = server.socket.recv() self.assertEqual(msg, motor.Request(0)) server.socket.send(ident, motor.Update(0, 0, 0)) with HedgehogClient(ctx, 'inproc://controller') as client: self.assertEqual(client.get_motor(0), (0, 0)) thread.join()
def test_set_digital_output(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident, msg = server.socket.recv() self.assertEqual(msg, io.StateAction(0, io.OUTPUT_OFF)) server.socket.send(ident, ack.Acknowledgement()) with HedgehogClient(ctx, 'inproc://controller') as client: self.assertEqual(client.set_digital_output(0, False), None) thread.join()
def test_shutdown(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): pass with HedgehogClient(ctx, 'inproc://controller') as client: client.shutdown() with self.assertRaises(errors.FailedCommandError): client.get_analog(0) thread.join()
def test_unsupported(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident, msg = server.socket.recv() self.assertEqual(msg, analog.Request(0)) server.socket.send(ident, ack.Acknowledgement(ack.UNSUPPORTED_COMMAND)) with HedgehogClient(ctx, 'inproc://controller') as client: with self.assertRaises(errors.UnsupportedCommandError): client.get_analog(0) thread.join()
def test_execute_process_handle_stream(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident, msg = server.socket.recv() self.assertEqual(msg, process.ExecuteRequest('echo', 'asdf')) server.socket.send(ident, process.ExecuteReply(2345)) server.socket.send( ident, process.StreamUpdate(2345, process.STDOUT, b'asdf\n')) server.socket.send(ident, process.StreamUpdate(2345, process.STDOUT)) server.socket.send(ident, process.StreamUpdate(2345, process.STDERR)) server.socket.send(ident, process.ExitUpdate(2345, 0)) exit_a, exit_b = pipe(ctx) with HedgehogClient(ctx, 'inproc://controller') as client: @coroutine def on_stdout(): client, pid, fileno, chunk = yield self.assertEqual(pid, 2345) self.assertEqual(fileno, process.STDOUT) self.assertEqual(chunk, b'asdf\n') client, pid, fileno, chunk = yield self.assertEqual(pid, 2345) self.assertEqual(fileno, process.STDOUT) self.assertEqual(chunk, b'') exit_b.signal() yield self.assertEqual( client.execute_process('echo', 'asdf', on_stdout=on_stdout()), 2345) exit_a.wait() thread.join()
def test_execute_process_handle_nothing(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident, msg = server.socket.recv() self.assertEqual(msg, process.ExecuteRequest('echo', 'asdf')) server.socket.send(ident, process.ExecuteReply(2345)) server.socket.send( ident, process.StreamUpdate(2345, process.STDOUT, b'asdf\n')) server.socket.send(ident, process.StreamUpdate(2345, process.STDOUT)) server.socket.send(ident, process.StreamUpdate(2345, process.STDERR)) server.socket.send(ident, process.ExitUpdate(2345, 0)) with HedgehogClient(ctx, 'inproc://controller') as client: self.assertEqual(client.execute_process('echo', 'asdf'), 2345) thread.join()
def test_multiple_client_threads(self): ctx = zmq.Context() @HedgehogServerDummy(self, ctx, 'inproc://controller') def thread(server): ident1, msg = server.socket.recv() self.assertEqual(msg, analog.Request(0)) server.socket.send(ident1, analog.Update(0, 0)) ident2, msg = server.socket.recv() self.assertEqual(msg, analog.Request(0)) server.socket.send(ident2, analog.Update(0, 0)) self.assertEqual(ident1[0], ident2[0]) with HedgehogClient(ctx, 'inproc://controller') as client1: self.assertEqual(client1.get_analog(0), 0) def spawned(client2): self.assertEqual(client2.get_analog(0), 0) client1.spawn(spawned) thread.join()