def test__propagates_errors_from_command(self): proto = JSONPerLineProtocol(callback=lambda obj: None) proto.connectionMade() reason = Failure(ProcessTerminated(1)) proto.processEnded(reason) with ExpectedException(ProcessTerminated): yield proto.done
def test__logs_only_full_lines_from_stderr(self): message = factory.make_name("message") callback = Mock() proto = JSONPerLineProtocol(callback=callback) proto.connectionMade() with TwistedLoggerFixture() as logger: proto.errReceived(message.encode("ascii")) self.assertThat(logger.output, Equals(""))
def test__logs_non_json_output(self): callback = Mock() proto = JSONPerLineProtocol(callback=callback) proto.connectionMade() with TwistedLoggerFixture() as logger: proto.outReceived(b"{\n") self.assertThat(logger.output, DocTestMatches("Failed to parse JSON: ..."))
def test__logs_stderr_at_process_end(self): message = factory.make_name("message") callback = Mock() proto = JSONPerLineProtocol(callback=callback) proto.connectionMade() with TwistedLoggerFixture() as logger: proto.errReceived(message.encode("ascii")) self.assertThat(logger.output, Equals("")) proto.processEnded(Failure(ProcessDone(0))) self.assertThat(logger.output, Equals(message))
def test__ignores_interspersed_zero_length_writes(self): callback = Mock() proto = JSONPerLineProtocol(callback=callback) proto.connectionMade() proto.outReceived(b"") self.expectThat(callback, MockCallsMatch()) proto.outReceived(b"{}\n") self.expectThat(callback, MockCallsMatch(call([{}]))) proto.outReceived(b"") self.expectThat(callback, MockCallsMatch(call([{}]))) proto.outReceived(b"{}\n") self.expectThat(callback, MockCallsMatch(call([{}]), call([{}])))
def test__parses_only_full_lines(self): callback = Mock() proto = JSONPerLineProtocol(callback=callback) proto.connectionMade() # Send an empty JSON dictionary using 3 separate writes. proto.outReceived(b"{") # No callback yet... self.expectThat(callback, MockCallsMatch()) proto.outReceived(b"}") # Still no callback... self.expectThat(callback, MockCallsMatch()) proto.outReceived(b"\n") # After a newline, we expect the JSON to be parsed and the callback # to receive an empty Python dictionary (which corresponds to the JSON # that was sent.) self.expectThat(callback, MockCallsMatch(call([{}])))