def test_fact_internal(self): fname = os.path.join(self._td, 'd.sock') ss = sample_server_cli self.assert_server_alive() with dispatch.open('', fname) as conn: dispatch.msg_write_type(conn, ss.COMP_FACT) dispatch.msg_write_int32(conn, 5) t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_RESULT) v = dispatch.msg_read_int32(conn) self.assertEqual(v, 120) with dispatch.open('', fname) as conn: dispatch.msg_write_type(conn, ss.COMP_FACT) dispatch.msg_write_int32(conn, 10) t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_RESULT) v = dispatch.msg_read_int32(conn) self.assertEqual(v, 3628800) with dispatch.open('', fname) as conn: # the result will be too big dispatch.msg_write_type(conn, ss.COMP_FACT) dispatch.msg_write_int32(conn, 15) t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_ERROR) v = dispatch.msg_read_uint16(conn) self.assertEqual(v, 1) with dispatch.open('', fname) as conn: # negative inputs are invalid dispatch.msg_write_type(conn, ss.COMP_FACT) dispatch.msg_write_int32(conn, -7) t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_ERROR) v = dispatch.msg_read_uint16(conn) self.assertEqual(v, 1)
def test_round_trip_reverse(self): fname = os.path.join(self._td, 'd.sock') ss = sample_server_cli self.assert_server_alive() with dispatch.open('', fname) as conn: dispatch.msg_write_type(conn, ss.ROUND_TRIP) dispatch.msg_write_type(conn, 2500) dispatch.msg_write_uint64(conn, 18446743967598447505) dispatch.msg_write_int64(conn, -9223372036854775807) dispatch.msg_write_uint32(conn, 2147477648) dispatch.msg_write_int32(conn, -6000) dispatch.msg_write_uint16(conn, 512) dispatch.msg_write_uint8(conn, 255) dispatch.msg_write_string(conn, "Hello Earth!") t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_RESULT) values = [] values.append(dispatch.msg_read_string(conn)) values.append(dispatch.msg_read_uint8(conn)) values.append(dispatch.msg_read_uint16(conn)) values.append(dispatch.msg_read_int32(conn)) values.append(dispatch.msg_read_uint32(conn)) values.append(dispatch.msg_read_int64(conn)) values.append(dispatch.msg_read_uint64(conn)) values.append(dispatch.msg_read_type(conn)) self.assertEqual(values, [ "Hello Earth!", 255, 512, -6000, 2147477648, -9223372036854775807, 18446743967598447505, 2500 ])
def test_open_close(self): conn = dispatch.open('', self.SOCKF) self.assertFalse(conn.closed) self.assertIsInstance(conn.fileno(), int) self.assertTrue(conn.fileno() > 0) conn.close() self.assertTrue(conn.closed) self.assertRaises(ValueError, conn.fileno)
def test_hello(self): self.assert_server_alive() fname = os.path.join(self._td, 'd.sock') with dispatch.open('', fname) as conn: dispatch.msg_write_type(conn, echo_server.ECHO_HELLO) s = dispatch.msg_read_string(conn) e = dispatch.msg_read_uint16(conn) self.assertEqual(s, "Hello, World") self.assertEqual(e, 0)
def rq_fact(sockfn, value): with dispatch.open('', sockfn) as conn: dispatch.msg_write_type(conn, COMP_FACT) dispatch.msg_write_int32(conn, value) t = dispatch.msg_read_type(conn) if t == T_ERROR: e = dispatch.msg_read_uint16(conn) sys.exit(e) v = dispatch.msg_read_int32(conn) print v
def test_single_operation2(self): with dispatch.open('', self.SOCKF) as conn: dispatch.msg_write_type(conn, MSG_ECHO) dispatch.msg_write_string(conn, 'Hello World') dispatch.msg_write_int64(conn, 5050505) dispatch.msg_write_uint8(conn, 0xA) self.assertEqual(dispatch.msg_read_type(conn), MSG_REPLY) r1 = dispatch.msg_read_string(conn) r2 = dispatch.msg_read_int64(conn) r3 = dispatch.msg_read_uint8(conn) self.assertEqual(r1, 'Hello World') self.assertEqual(r2, 5050505) self.assertEqual(r3, 0xA)
def test_foobar(self): self.assert_server_alive() fname = os.path.join(self._td, 'd.sock') conn = dispatch.open('', fname) with conn: dispatch.msg_write_type(conn, echo_server.ECHO_STRING) dispatch.msg_write_string(conn, "foobar") s = dispatch.msg_read_string(conn) # mimic the common error number style e = dispatch.msg_read_uint16(conn) self.assertEqual(s, 'foobar') self.assertEqual(e, 0)
def test_multiple_rounds(self): self.assert_server_alive() fname = os.path.join(self._td, 'd.sock') for ii in range(0, 16): self.assert_server_alive() with dispatch.open('', fname) as conn: tostr = chr(0x41 + ii) * (ii + 1) dispatch.msg_write_type(conn, echo_server.ECHO_STRING) dispatch.msg_write_string(conn, tostr) fromstr = dispatch.msg_read_string(conn) err = dispatch.msg_read_uint16(conn) self.assertEqual(err, 0) self.assertEqual(tostr, fromstr)
def text_ctx_manager(self): conn = dispatch.open('', self.SOCKF) self.assertFalse(conn.closed) with conn: self.assertFalse(conn.closed) self.assertTrue(conn.fileno() > 0) self.assertTrue(conn.closed) try: with conn: conn.fileno() failed2 = False except ValueError: failed2 = True self.assertTrue(failed2)
def test_eof_reads(self): fname = os.path.join(self._td, 'd.sock') ss = sample_server_cli self.assert_server_alive() with dispatch.open('', fname) as conn: dispatch.msg_write_type(conn, ss.COMP_FACT) dispatch.msg_write_int32(conn, 5) t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_RESULT) v = dispatch.msg_read_int32(conn) self.assertEqual(v, 120) self.assertRaises(IOError, dispatch.msg_read_uint16, conn) self.assertRaises(IOError, dispatch.msg_read_uint32, conn) self.assertRaises(IOError, dispatch.msg_read_int32, conn)
def test_open_zero_source(self): fname = os.path.join(self._td, 'd.sock') ss = sample_server_cli self.assert_server_alive() with dispatch.open('', fname) as conn: dispatch.msg_write_type(conn, ss.OPEN_ZERO) t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_RESULT) fh = dispatch.msg_read_file(conn) with fh: buf = fh.read(8) self.assertEqual(len(buf), 8) self.assertEqual(buf, '\0' * 8) self.assertRaises((ValueError, OSError, IOError), fh.read, 8)
def test_open_rand_source(self): fname = os.path.join(self._td, 'd.sock') ss = sample_server_cli self.assert_server_alive() with dispatch.open('', fname) as conn: dispatch.msg_write_type(conn, ss.OPEN_RAND) t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_RESULT) fd = dispatch.msg_read_fd(conn) self.assertTrue(fd > 0) try: buf = os.read(fd, 8) self.assertEqual(len(buf), 8) finally: os.close(fd) self.assertRaises((OSError, IOError), os.read, fd, 8)
def test_bytes_type(self): import hashlib d1 = b'my\0binary\0data\0' d2 = hashlib.sha1('Goodbye Moon').digest() d3 = b'\x00\x00\x08abc' fname = os.path.join(self._td, 'd.sock') ss = sample_server_cli self.assert_server_alive() with dispatch.open('', fname) as conn: dispatch.msg_write_type(conn, ss.A_FEW_BYTES) dispatch.msg_write_bytes(conn, d1) dispatch.msg_write_bytes(conn, d2) dispatch.msg_write_bytes(conn, d3) t = dispatch.msg_read_type(conn) self.assertEqual(t, ss.T_RESULT) r0 = dispatch.msg_read_uint32(conn) self.assertEqual(r0, len(d1) + len(d2) + len(d3)) r1 = dispatch.msg_read_bytes(conn) r2 = dispatch.msg_read_bytes(conn) r3 = dispatch.msg_read_bytes(conn) self.assertEqual(r1, d1) self.assertEqual(r2, d2) self.assertEqual(r3, d3)