def test_write_read(self): thing = Thing() p = Pipe() p.put(thing) read = p.reader.get() p.close() self.assertEqual(read, thing)
def test_multiple_write_read(self): things = [ Thing(), Thing(), Thing() ] p = Pipe() for t in things: p.put(t) read = [] for _ in things: read.append(p.reader.get()) p.close() self.assertEqual(read, things)
def test_call_pipe_broken(self): def method(): pass pipe = Pipe() call = Call(method) call(pipe)
def test_init(self, _open): _open.return_value = Reader(0), Writer(1) p = Pipe() self.assertTrue(isinstance(p.reader, Reader)) self.assertTrue(isinstance(p.writer, Writer)) self.assertEqual(p.reader.fd, 0) self.assertEqual(p.writer.fd, 1)
def test_call_exception(self): method = Mock(side_effect=ValueError) pipe = Pipe() pipe.reader.close = Mock() call = Call(method) # test call(pipe) reply = protocol.Reply.read(pipe.reader) # validation self.assertEqual(reply.code, protocol.Raised.CODE)
def __call__(self): """ Invoke the RMI as follows: - Fork - Start the monitor. - Read and dispatch reply messages. :return: Whatever method returned. """ pipe = Pipe() target = Target(self.method, *self.args, **self.kwargs) child = Process(target, pipe) monitor = Monitor(Context.current(), child) try: child.start() monitor.start() pipe.writer.close() retval = self.read(pipe.reader) return retval finally: pipe.close() monitor.stop() child.wait()
def test_multiple_write_read(self): things = [Thing(), Thing(), Thing()] p = Pipe() for t in things: p.put(t) read = [] for _ in things: read.append(p.reader.get()) p.close() self.assertEqual(read, things)
def test_report(self): pipe = Pipe() p = Progress(pipe.writer) p.total = 1 p.completed = 2 p.details = 'hello' # test p.report() # validation reply = protocol.Reply.read(pipe.reader) self.assertEqual(reply.code, protocol.Progress.CODE) self.assertEqual(reply.payload.total, p.total) self.assertEqual(reply.payload.completed, p.completed) self.assertEqual(reply.payload.details, p.details)
def test_call(self, context, monitor): method = Mock(return_value=18) pipe = Pipe() pipe.reader.close = Mock() call = Call(method, 1, 2, a=1, b=2) # test call(pipe) # validation pipe.reader.close.assert_called_once_with() monitor.assert_called_once_with(pipe.writer) monitor.return_value.start.assert_called_once_with() reply = protocol.Reply.read(pipe.reader) self.assertTrue(isinstance(context.return_value.progress, Progress)) self.assertEqual(reply.code, protocol.Result.CODE) self.assertEqual(reply.payload, method.return_value)
def test_eof(self): p = Pipe() p.writer.close() self.assertRaises(EOFError, p.reader.get) p.close()
def test_poll(self, _open): _open.return_value = Mock(), Mock() p = Pipe() p.poll() _open.return_value[0].poll.assert_called_once_with()
def test_close(self, _open): _open.return_value = Mock(), Mock() p = Pipe() p.close() _open.return_value[0].close.assert_called_once_with() _open.return_value[1].close.assert_called_once_with()
def test_enter(self, _open): _open.return_value = Mock(), Mock() p = Pipe() self.assertEqual(p.__enter__(), p)
def test_exit(self, _open, close): _open.return_value = Mock(), Mock() p = Pipe() p.__exit__() close.assert_called_once_with()
def test_get(self, _open): _open.return_value = Mock(), Mock() p = Pipe() thing = p.get() _open.return_value[0].get.assert_called_once_with() self.assertEqual(thing, _open.return_value[0].get.return_value)
def test_put(self, _open): _open.return_value = Mock(), Mock() thing = Thing() p = Pipe() p.put(thing) _open.return_value[1].put.assert_called_once_with(thing)
def test_open(self, pipe): pipe.return_value = 0, 1 reader, writer = Pipe._open() self.assertEqual(reader.fd, 0) self.assertEqual(writer.fd, 1)