def test_send_error(self): sock = FakeSocket() sock.close() # Make it raise an error conn = simplerpc.Connection(sock) self.assertRaises(socket.error, conn.send, 'FOO', 'Nobody', 'inspects', 'the', 'spammish', 'repetition') self.assertEqual(conn._sock, None)
def test_recv_closed(self): self.stub_recvbuf_pop() sock = FakeSocket('foo') conn = simplerpc.Connection(sock) self.assertRaises(simplerpc.ConnectionClosed, conn.recv) self.assertEqual(conn._sock, None) self.assertEqual(conn._recvbuf, []) self.assertEqual(conn._recvbuf_partial, '')
def test_recv_recvbuf_filled(self): self.stub_recvbuf_pop() conn = simplerpc.Connection(None) conn._recvbuf = ['foo', 'bar'] result = conn.recv() self.assertEqual(result, 'foo') self.assertEqual(conn._recvbuf, ['bar'])
def test_close_double(self): conn = simplerpc.Connection(None) conn._recvbuf.append('foo') conn._recvbuf_partial = 'bar' conn.close() self.assertEqual(conn._sock, None) self.assertEqual(conn._recvbuf, []) self.assertEqual(conn._recvbuf_partial, '')
def test_send_open(self): sock = FakeSocket() conn = simplerpc.Connection(sock) conn.send('FOO', 'Nobody', 'inspects', 'the', 'spammish', 'repetition') self.assertEqual(sock._send_data, [ '{"cmd": "FOO", "payload": ["Nobody", "inspects", "the", ' '"spammish", "repetition"]}\n' ])
def test_recv_error(self): self.stub_recvbuf_pop() sock = FakeSocket() sock.close() # Make it raise an error conn = simplerpc.Connection(sock) self.assertRaises(socket.error, conn.recv) self.assertEqual(conn._sock, None) self.assertEqual(conn._recvbuf, []) self.assertEqual(conn._recvbuf_partial, '')
def test_recv_longmsg(self): self.stub_recvbuf_pop() sock = FakeSocket('foo', 'bar', 'baz\n') conn = simplerpc.Connection(sock) result = conn.recv() self.assertEqual(result, 'foobarbaz') self.assertEqual(conn._sock, sock) self.assertEqual(conn._recvbuf, []) self.assertEqual(conn._recvbuf_partial, '')
def test_recv_onemsg_onepartial(self): self.stub_recvbuf_pop() sock = FakeSocket('bar\nfoo') conn = simplerpc.Connection(sock) result = conn.recv() self.assertEqual(result, 'bar') self.assertEqual(conn._sock, sock) self.assertEqual(conn._recvbuf, []) self.assertEqual(conn._recvbuf_partial, 'foo')
def test_close(self): sock = FakeSocket() conn = simplerpc.Connection(sock) conn._recvbuf.append('foo') conn._recvbuf_partial = 'bar' conn.close() self.assertTrue(sock._closed) self.assertEqual(conn._sock, None) self.assertEqual(conn._recvbuf, []) self.assertEqual(conn._recvbuf_partial, '')
def test_recvbuf_pop_msg(self): conn = simplerpc.Connection(None) conn._recvbuf.append( dict(cmd='FOO', payload=[ 'Nobody', 'inspects', 'the', 'spammish', 'repetition' ])) result = conn._recvbuf_pop() self.assertEqual( result, ('FOO', ['Nobody', 'inspects', 'the', 'spammish', 'repetition']))
def test_recv_recvbuf_empty_closed(self): self.stub_recvbuf_pop() conn = simplerpc.Connection(None) self.assertRaises(simplerpc.ConnectionClosed, conn.recv)
def test_recvbuf_pop_exc(self): conn = simplerpc.Connection(None) conn._recvbuf.append(Exception()) self.assertRaises(Exception, conn._recvbuf_pop)
def test_send_closed(self): conn = simplerpc.Connection(None) self.assertRaises(simplerpc.ConnectionClosed, conn.send, 'FOO', 'Nobody', 'inspects', 'the', 'spammish', 'repetition')