def test_portable_read_without_factory(self): config = _Config() config.portable_factories = {FACTORY_ID: the_factory} service = SerializationServiceV1(config) service2 = SerializationServiceV1(_Config()) obj = create_portable() self.assertTrue(obj.inner_portable) data = service.to_data(obj) with self.assertRaises(HazelcastSerializationError): service2.to_object(data)
def setUp(self): config1 = _Config() config1.portable_factories = {FACTORY_ID: the_factory_1} config2 = _Config() config2.portable_factories = {FACTORY_ID: the_factory_2} self.service1 = SerializationServiceV1(config1) self.service2 = SerializationServiceV1(config2) base_portable = create_portable() data = self.service1.to_data(base_portable) inp = self.service2._create_data_input(data) portable_serializer = self.service2._registry._portable_serializer self.reader = portable_serializer.create_morphing_reader(inp)
def _create_serialization_service(is_big_endian, int_type): config = _Config() config.custom_serializers = { CustomStreamSerializable: CustomStreamSerializer, CustomByteArraySerializable: CustomByteArraySerializer } config.is_big_endian = is_big_endian cdb = ClassDefinitionBuilder(PORTABLE_FACTORY_ID, INNER_PORTABLE_CLASS_ID) cdb.add_int_field("i") cdb.add_float_field("f") cd = cdb.build() config.class_definitions = [cd] config.portable_factories = { PORTABLE_FACTORY_ID: { PORTABLE_CLASS_ID: APortable, INNER_PORTABLE_CLASS_ID: AnInnerPortable } } config.data_serializable_factories = { IDENTIFIED_DATA_SERIALIZABLE_FACTORY_ID: { DATA_SERIALIZABLE_CLASS_ID: AnIdentifiedDataSerializable } } config.default_int_type = int_type return SerializationServiceV1(config)
def test_unisocket_mode_and_enabled_backups(self): config = _Config() config.smart_routing = False client, service = self._start_service(config) self.assertIsNotNone(service._clean_resources_timer) listener_service = client._listener_service listener_service.register_listener.assert_not_called()
def _start_service(self, config=_Config()): c = MagicMock(config=config) invocation_service = InvocationService(c, c._reactor) self.service = invocation_service invocation_service.init(c._internal_partition_service, c._connection_manager, c._listener_service) invocation_service.start() return c, invocation_service
def setUp(self): config1 = _Config() config1.portable_factories = {FACTORY_ID: the_factory_1} config2 = _Config() config2.portable_factories = {FACTORY_ID: the_factory_2} self.service1 = SerializationServiceV1(config1) self.service2 = SerializationServiceV1(config2) base_portable = create_portable() data = self.service1.to_data(base_portable) inp = _ObjectDataInput(data.buffer, DATA_OFFSET, self.service2, self.service2._is_big_endian) portable_serializer = self.service2._registry._portable_serializer self.reader = portable_serializer.create_morphing_reader(inp)
def test_auth_failure(self): cfg = _Config() with self.assertRaises(TypeError): cfg.creds_username = 1 with self.assertRaises(TypeError): cfg.creds_password = 2 with self.assertRaises(TypeError): cfg.token_provider = object()
def test_double_register_custom_serializer(self): config = _Config() config.custom_serializers = {CustomClass: CustomSerializer} service = SerializationServiceV1(config) with self.assertRaises(ValueError): service._registry.safe_register_serializer( TheOtherCustomSerializer, CustomClass)
def test_encode_decode(self): config = _Config() config.data_serializable_factories = {FACTORY_ID: the_factory} service = SerializationServiceV1(config) obj = create_identified() data = service.to_data(obj) obj2 = service.to_object(data) self.assertTrue(obj == obj2)
def test_duplicate_class_definition(self): config = _Config() class_def1 = ClassDefinitionBuilder(1, 1).add_string_field("str_field").build() class_def2 = ClassDefinitionBuilder(1, 1).add_int_field("int_field").build() config.class_definitions = [class_def1, class_def2] with self.assertRaises(HazelcastSerializationError): SerializationServiceV1(config)
def test_auth_fromdict(self): tp = BasicTokenProvider("tok") cfg = _Config().from_dict({ "creds_username": "******", "creds_password": "******", "token_provider": tp, }) self.assertEqual("user", cfg.creds_username) self.assertEqual("pass", cfg.creds_password) self.assertEqual(tp, cfg.token_provider)
def test_constructor_with_unreachable_addresses(self): addr = Address("192.168.0.1", 5701) config = _Config() start = time.time() conn = AsyncoreConnection(MagicMock(map=dict()), MagicMock(), None, addr, config, None) try: # Server is unreachable, but this call should return # before connection timeout self.assertLess(time.time() - start, config.connection_timeout) finally: conn.close(None, None)
def test_custom_serializer(self): config = _Config() config.custom_serializers = {CustomClass: CustomSerializer} service = SerializationServiceV1(config) obj = CustomClass("uid", "some name", "description text") data = service.to_data(obj) obj2 = service.to_object(data) self.assertEqual(obj, obj2) self.assertEqual("CUSTOM", obj2.source)
def test_global_encode_decode(self): config = _Config() config.global_serializer = TestGlobalSerializer service = SerializationServiceV1(config) obj = CustomClass("uid", "some name", "description text") data = service.to_data(obj) obj2 = service.to_object(data) self.assertEqual(obj, obj2) self.assertEqual("GLOBAL", obj2.source)
def test_encode_decode_2(self): config = _Config() config.portable_factories = {FACTORY_ID: the_factory} service = SerializationServiceV1(config) service2 = SerializationServiceV1(config) obj = create_portable() self.assertTrue(obj.inner_portable) data = service.to_data(obj) obj2 = service2.to_object(data) self.assertTrue(obj == obj2)
def test_encode_decode(self): config = _Config() config.portable_factories = {FACTORY_ID: the_factory} service = SerializationServiceV1(config) obj = create_portable() self.assertTrue(obj.inner_portable) data = service.to_data(obj) obj2 = service.to_object(data) self.assertTrue(obj == obj2) self.assertEqual(obj.inner_portable.param_int, obj2.nested_field)
def test_portable_null_fields(self): config = _Config() config.portable_factories = {FACTORY_ID: the_factory} service = SerializationServiceV1(config) service.to_data(create_portable()) service2 = SerializationServiceV1(config) obj = SerializationV1Portable() data = service.to_data(obj) obj2 = service2.to_object(data) self.assertTrue(obj == obj2)
def test_socket_options(self): config = _Config() config.socket_options = [ (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ] conn = AsyncoreConnection(MagicMock(map=dict()), None, None, self.member.address, config, None) try: # By default this is set to 0 self.assertEqual(1, conn.socket.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)) finally: conn._inner_close()
def test_byte_case(self): config = _Config() config.default_int_type = IntType.BYTE service = SerializationServiceV1(config) d1 = service.to_data(byte_val) v1 = service.to_object(d1) self.assertEqual(d1.get_type(), CONSTANT_TYPE_BYTE) self.assertEqual(v1, byte_val) with self.assertRaises(HazelcastSerializationError): service.to_data(big_int)
def test_portable_context(self): config = _Config() config.portable_factories = {FACTORY_ID: the_factory} service = SerializationServiceV1(config) obj = create_portable() self.assertTrue(obj.inner_portable) service.to_data(obj) class_definition = service._portable_context.lookup_class_definition( FACTORY_ID, InnerPortable.CLASS_ID, 0) self.assertTrue(class_definition is not None)
def _start_service(self, config=_Config()): c = MagicMock() invocation_service = InvocationService(c, config, c._reactor) self.service = invocation_service invocation_service.init( c._internal_partition_service, c._connection_manager, c._listener_service, c.compact_schema_service, ) invocation_service.start() invocation_service.add_backup_listener() return c, invocation_service
def test_send_buffer_size(self): # When the SO_SNDBUF option is set, we should try # to use that value while trying to write something. config = _Config() size = 64 * 1024 config.socket_options = [(socket.SOL_SOCKET, socket.SO_SNDBUF, size)] conn = AsyncoreConnection(MagicMock(map=dict()), None, None, self.member.address, config, None) try: # By default this is set to 128000 self.assertEqual(size, conn.send_buffer_size) finally: conn._inner_close()
def test_resources_cleaned_up_after_immediate_failure(self): addr = Address("invalid-address", 5701) config = _Config() mock_reactor = MagicMock(map={}) try: AsyncoreConnection(mock_reactor, MagicMock(), None, addr, config, None) self.fail( "Connection attempt to an invalid address should fail immediately" ) except socket.error: # Constructor of the connection should remove itself from the # dispatchers map of the reactor. self.assertEqual(0, len(mock_reactor.map))
def test_receive_buffer_size(self): # When the SO_RCVBUF option is set, we should try # to use that value while trying to read something. self.server = MockServer() config = _Config() size = 64 * 1024 config.socket_options = [(socket.SOL_SOCKET, socket.SO_RCVBUF, size)] conn = AsyncoreConnection(MagicMock(map=dict()), None, None, self.server.get_address(), config, None) try: # By default this is set to 128000 self.assertEqual(size, conn.receive_buffer_size) finally: conn._inner_close()
def test_short_case(self): config = _Config() config.default_int_type = IntType.SHORT service = SerializationServiceV1(config) d1 = service.to_data(byte_val) d2 = service.to_data(short_val) v1 = service.to_object(d1) v2 = service.to_object(d2) self.assertEqual(d1.get_type(), CONSTANT_TYPE_SHORT) self.assertEqual(d2.get_type(), CONSTANT_TYPE_SHORT) self.assertEqual(v1, byte_val) self.assertEqual(v2, short_val) with self.assertRaises(HazelcastSerializationError): service.to_data(big_int)
def test_portable_class_def(self): builder_inner = ClassDefinitionBuilder(FACTORY_ID, InnerPortable.CLASS_ID) builder_inner.add_utf_field("param_str") builder_inner.add_int_field("param_int") class_def_inner = builder_inner.build() builder = ClassDefinitionBuilder(FACTORY_ID, SerializationV1Portable.CLASS_ID) builder.add_byte_field("1") builder.add_boolean_field("2") builder.add_char_field("3") builder.add_short_field("4") builder.add_int_field("5") builder.add_long_field("6") builder.add_float_field("7") builder.add_double_field("8") builder.add_utf_field("9") builder.add_byte_array_field("a1") builder.add_boolean_array_field("a2") builder.add_char_array_field("a3") builder.add_short_array_field("a4") builder.add_int_array_field("a5") builder.add_long_array_field("a6") builder.add_float_array_field("a7") builder.add_double_array_field("a8") builder.add_utf_array_field("a9") builder.add_portable_field("p", class_def_inner) builder.add_portable_array_field("ap", class_def_inner) class_def = builder.build() config = _Config() config.portable_factories = {FACTORY_ID: the_factory} config.class_definitions = [ class_def, class_def_inner, ] service = SerializationServiceV1(config) service2 = SerializationServiceV1(config) obj = SerializationV1Portable() data = service.to_data(obj) obj2 = service2.to_object(data) self.assertTrue(obj == obj2)
def test_classes_with_same_class_id_in_different_factories(self): config = _Config() config.portable_factories = {1: {1: MyPortable1}, 2: {1: MyPortable2}} class_def1 = ClassDefinitionBuilder(1, 1).add_string_field("str_field").build() class_def2 = ClassDefinitionBuilder(2, 1).add_int_field("int_field").build() config.class_definitions = [class_def1, class_def2] ss = SerializationServiceV1(config) portable1 = MyPortable1("test") data1 = ss.to_data(portable1) self.assertEqual(portable1, ss.to_object(data1)) portable2 = MyPortable2(1) data2 = ss.to_data(portable2) self.assertEqual(portable2, ss.to_object(data2))
def test_nested_null_portable_serialization(self): config = _Config() config.portable_factories = {1: {1: Parent, 2: Child}} child_class_def = ClassDefinitionBuilder( FACTORY_ID, 2).add_string_field("name").build() parent_class_def = (ClassDefinitionBuilder( FACTORY_ID, 1).add_portable_field("child", child_class_def).build()) config.class_definitions = [child_class_def, parent_class_def] ss = SerializationServiceV1(config) p = Parent(None) data = ss.to_data(p) self.assertEqual(p, ss.to_object(data))
def test_nested_portable_serialization(self): config = _Config() config.portable_version = 6 config.portable_factories = { 1: { 1: Parent, 2: Child, } } ss1 = SerializationServiceV1(config) ss2 = SerializationServiceV1(config) ss2.to_data(Child("Joe")) p = Parent(Child("Joe")) data = ss1.to_data(p) self.assertEqual(p, ss2.to_object(data))
def test_dynamic_case(self): config = _Config() config.default_int_type = IntType.VAR service = SerializationServiceV1(config) d1 = service.to_data(byte_val) d2 = service.to_data(short_val) d3 = service.to_data(int_val) d4 = service.to_data(long_val) v1 = service.to_object(d1) v2 = service.to_object(d2) v3 = service.to_object(d3) v4 = service.to_object(d4) self.assertEqual(d1.get_type(), CONSTANT_TYPE_BYTE) self.assertEqual(d2.get_type(), CONSTANT_TYPE_SHORT) self.assertEqual(d3.get_type(), CONSTANT_TYPE_INTEGER) self.assertEqual(d4.get_type(), CONSTANT_TYPE_LONG) self.assertEqual(v1, byte_val) self.assertEqual(v2, short_val) self.assertEqual(v3, int_val) self.assertEqual(v4, long_val)