public void TestWriteDataWithPortable()
        {
            var ss = new SerializationServiceBuilder()
                     .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID,
                                         new PortableFactoryFunc(i => new NamedPortableV2()))
                     .AddDataSerializableFactory(TestSerializationConstants.DATA_SERIALIZABLE_FACTORY_ID,
                                                 GetDataSerializableFactory())
                     .Build();

            var ss2 = new SerializationServiceBuilder()
                      .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID,
                                          new PortableFactoryFunc(i => new NamedPortable()))
                      .AddDataSerializableFactory(TestSerializationConstants.DATA_SERIALIZABLE_FACTORY_ID,
                                                  GetDataSerializableFactory())
                      .SetPortableVersion(5)
                      .Build();

            IPortable p1 = new NamedPortableV2("test", 456);
            object    o1 = new DataDataSerializable(ss.ToData(p1));

            var data = ss.ToData(o1);

            var o2 = ss2.ToObject <DataDataSerializable>(data);

            Assert.AreEqual(o1, o2);

            var p2 = ss2.ToObject <IPortable>(o2.Data);

            Assert.AreEqual(p1, p2);
        }
Esempio n. 2
0
        public void TestClassDefinitionConfig()
        {
            int portableVersion     = 1;
            var serializationConfig = new SerializationOptions();

            serializationConfig.AddPortableFactory(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                                   new TestPortableFactory());
            serializationConfig.PortableVersion = portableVersion;
            serializationConfig
            .ClassDefinitions.Add(
                new ClassDefinitionBuilder(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                           SerializationTestsConstants.RAW_DATA_PORTABLE, portableVersion)
                .AddLongField("l")
                .AddCharArrayField("c")
                .AddPortableField("p", CreateNamedPortableClassDefinition(portableVersion))
                .Build());
            serializationConfig
            .ClassDefinitions.Add(
                new ClassDefinitionBuilder(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                           SerializationTestsConstants.NAMED_PORTABLE, portableVersion)
                .AddStringField("name").AddIntField("myint").Build());

            var serializationService = new SerializationServiceBuilder(new NullLoggerFactory())
                                       .SetConfig(serializationConfig)
                                       .AddDataSerializableFactory(SerializationTestsConstants.DATA_SERIALIZABLE_FACTORY_ID,
                                                                   GetDataSerializableFactory())
                                       .Build();
            var p = new RawDataPortable(DateTime.Now.ToFileTime(), "test chars".ToCharArray(),
                                        new NamedPortable("named portable", 34567),
                                        9876, "Testing raw portable", new ByteArrayDataSerializable(Encoding.UTF8.GetBytes("test bytes")));

            var data = serializationService.ToData(p);

            Assert.AreEqual(p, serializationService.ToObject <RawDataPortable>(data));
        }
        public void TestGlobalSerializerOverride()
        {
            var config = new SerializationOptions();
            var globalListSerializer = new GlobalListSerializer();

            config.DefaultSerializer = new SerializerOptions
            {
                Creator     = () => globalListSerializer,
                OverrideClr = true
            };

            var ss = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();


            var list = new List <string> {
                "foo", "bar"
            };

            var d     = ss.ToData(list);
            var input = new ByteArrayObjectDataInput(d.ToByteArray(), HeapData.DataOffset, ss, Endianness.BigEndian);

            var actual = (List <string>)globalListSerializer.Read(input);

            Assert.AreEqual(list, actual);
        }
Esempio n. 4
0
        public void TestWriteObjectWithCustomSerializable()
        {
            var config = new SerializationOptions();
            var sc     = new SerializerOptions
            {
                SerializedType = typeof(CustomSerializableType),
                Creator        = () => new CustomSerializer()
            };

            config.Serializers.Add(sc);
            var serializationService =
                new SerializationServiceBuilder(new NullLoggerFactory()).SetPortableVersion(1)
                .AddPortableFactory(SerializationTestsConstants.PORTABLE_FACTORY_ID, new TestPortableFactory())
                .SetConfig(config).Build();

            var foo = new CustomSerializableType {
                Value = "foo"
            };

            var objectCarryingPortable1 = new ObjectCarryingPortable(foo);
            var data = serializationService.ToData(objectCarryingPortable1);
            var objectCarryingPortable2 = serializationService.ToObject <ObjectCarryingPortable>(data);

            Assert.AreEqual(objectCarryingPortable1, objectCarryingPortable2);
        }
Esempio n. 5
0
        public void TestWriteObjectWithPortable()
        {
            var ss = new SerializationServiceBuilder(new NullLoggerFactory())
                     .AddPortableFactory(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                         new PortableFactoryFunc(i => new NamedPortableV2()))
                     .AddDataSerializableFactory(SerializationTestsConstants.DATA_SERIALIZABLE_FACTORY_ID,
                                                 GetDataSerializableFactory())
                     .Build();

            var ss2 = new SerializationServiceBuilder(new NullLoggerFactory())
                      .AddPortableFactory(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                          new PortableFactoryFunc(i => new NamedPortable()))
                      .AddDataSerializableFactory(SerializationTestsConstants.DATA_SERIALIZABLE_FACTORY_ID,
                                                  GetDataSerializableFactory())
                      .SetPortableVersion(5)
                      .Build();

            var o1 = new ComplexDataSerializable(new NamedPortableV2("test", 123, 500),
                                                 new ByteArrayDataSerializable(new byte[3]), null);
            var data = ss.ToData(o1);

            object o2 = ss2.ToObject <ComplexDataSerializable>(data);

            Assert.AreEqual(o1, o2);
        }
Esempio n. 6
0
        public void TestWriteReadWithNullPortableArray()
        {
            int portableVersion = 1;
            var builder0        = new ClassDefinitionBuilder(SerializationTestsConstants.PORTABLE_FACTORY_ID, 1, portableVersion);
            var builder1        = new ClassDefinitionBuilder(SerializationTestsConstants.PORTABLE_FACTORY_ID, 2, portableVersion);

            builder0.AddPortableArrayField("list", builder1.Build());

            var ss = new SerializationServiceBuilder(new NullLoggerFactory())
                     .SetPortableVersion(portableVersion)
                     .AddClassDefinition(builder0.Build())
                     .AddClassDefinition(builder1.Build())
                     .Build();

            var data = ss.ToData(new TestObject1());

            var ss2 = new SerializationServiceBuilder(new NullLoggerFactory())
                      .AddPortableFactory(1, new PortableFactoryFunc(classId =>
            {
                switch (classId)
                {
                case 1:
                    return(new TestObject1());

                case 2:
                    return(new TestObject2());
                }
                return(null);
            })).Build();
            var obj = ss2.ToObject <object>(data);

            Assert.IsNotNull(obj);
            Assert.IsInstanceOf <TestObject1>(obj);
        }
Esempio n. 7
0
        public void TestSerialization()
        {
            var ss   = new SerializationServiceBuilder().Build();
            var data = ss.ToData(predicate);
            var partitionPredicate = ss.ToObject <PartitionPredicate>(data);

            Assert.AreEqual(partitionKey, partitionPredicate.GetPartitionKey());
            Assert.AreEqual(Predicates.True(), partitionPredicate.GetTarget());
        }
Esempio n. 8
0
        public void TestAddDataSerializableFactory()
        {
            var service1 = new SerializationServiceBuilder(new NullLoggerFactory()).Build();
            var data     = service1.ToData(new DataSerializableBasicType());

            var config = new SerializationOptions();

            config.AddDataSerializableFactory(1, new MyDataSerializableFactory());
            var service = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();

            var obj = service.ToObject <object>(data);

            Assert.IsInstanceOf <DataSerializableBasicType>(obj);
        }
Esempio n. 9
0
        public void TestWritePortableArray()
        {
            var ss           = new SerializationServiceBuilder(new NullLoggerFactory()).SetInitialOutputBufferSize(16).Build();
            var testObject2s = new TestObject2[100];

            for (var i = 0; i < testObject2s.Length; i++)
            {
                testObject2s[i] = new TestObject2();
            }

            var testObject1 = new TestObject1(testObject2s);

            ss.ToData(testObject1);
        }
Esempio n. 10
0
        public void TestAddDataSerializableFactoryClass()
        {
            var service1 = new SerializationServiceBuilder().Build();
            var data     = service1.ToData(new DataSerializableBasicType());

            var config = new SerializationConfig();

            config.AddDataSerializableFactoryClass(1, typeof(MyDataSerializableFactory));
            var service = new SerializationServiceBuilder().SetConfig(config).Build();

            var obj = service.ToObject <object>(data);

            Assert.IsInstanceOf <DataSerializableBasicType>(obj);
        }
Esempio n. 11
0
        public void TestAddPortableFactoryClass()
        {
            var service1 = new SerializationServiceBuilder().Build();
            var data     = service1.ToData(new KitchenSinkPortable());

            var config = new SerializationConfig();

            config.AddPortableFactoryClass(1, typeof(KitchenSinkPortableFactory));

            var service = new SerializationServiceBuilder().SetConfig(config).Build();

            var obj = service.ToObject <object>(data);

            Assert.IsInstanceOf <KitchenSinkPortable>(obj);
        }
        public virtual void Test()
        {
            var portableFactory = new PortableFactoryFunc(i =>
            {
                if (i == 1)
                {
                    return(new PortablePerson());
                }
                if (i == 2)
                {
                    return(new PortableAddress());
                }
                throw new ArgumentException();
            });

            var ss = new SerializationServiceBuilder()
                     .AddPortableFactory(FactoryId, portableFactory)
                     .AddDataSerializableFactory(FactoryId, new ArrayDataSerializableFactory(new Func <IIdentifiedDataSerializable>[]
            {
                () => new Address(),
                () => new Person(),
            }))
                     .Build();
            var k     = 10;
            var tasks = new Task[k];

            for (var i = 0; i < k; i++)
            {
                tasks[i] = Task.Factory.StartNew(() =>
                {
                    for (var j = 0; j < 10000; j++)
                    {
                        var key     = "key" + Rnd();
                        var dataKey = ss.ToData(key);
                        Assert.AreEqual(key, ss.ToObject <string>(dataKey));
                        var value     = 123L + Rnd();
                        var dataValue = ss.ToData(value);
                        Assert.AreEqual(value, ss.ToObject <long>(dataValue));
                        var address     = new Address("here here" + Rnd(), 13131 + Rnd());
                        var dataAddress = ss.ToData(address);
                        Assert.AreEqual(address, ss.ToObject <Address>(dataAddress));
                        var person     = new Person(13 + Rnd(), 199L + Rnd(), 56.89d, "mehmet", address);
                        var dataPerson = ss.ToData(person);
                        Assert.AreEqual(person, ss.ToObject <Person>(dataPerson));
                        var portableAddress     = new PortableAddress("there there " + Rnd(), 90909 + Rnd());
                        var dataPortableAddress = ss.ToData(portableAddress);
                        Assert.AreEqual(portableAddress, ss.ToObject <PortableAddress>(dataPortableAddress));
                        var portablePerson     = new PortablePerson(63 + Rnd(), 167L + Rnd(), "ahmet", portableAddress);
                        var dataPortablePerson = ss.ToData(portablePerson);
                        Assert.AreEqual(portablePerson, ss.ToObject <PortablePerson>(dataPortablePerson));
                    }
                });
            }
            Task.WaitAll(tasks, new TimeSpan(0, 0, 0, 30));
        }
Esempio n. 13
0
        public void Init()
        {
            var _ss      = new SerializationServiceBuilder().Build();
            var dataList = new ConcurrentQueue <KeyValuePair <IData, object> >();

            dataList.Enqueue(new KeyValuePair <IData, object>(_ss.ToData(0), "0"));
            dataList.Enqueue(new KeyValuePair <IData, object>(_ss.ToData(1), _ss.ToData("1")));
            dataList.Enqueue(new KeyValuePair <IData, object>(_ss.ToData(2), _ss.ToData("2")));
            dataList.Enqueue(new KeyValuePair <IData, object>(_ss.ToData(3), "3"));
            _testCollection = new ReadOnlyLazyEntrySet <int, string>(dataList, _ss);
        }
        public void Init()
        {
            var _ss      = new SerializationServiceBuilder().Build();
            var dataList = new List <KeyValuePair <IData, object> >();

            dataList.Add(new KeyValuePair <IData, object>(_ss.ToData(0), "0"));
            dataList.Add(new KeyValuePair <IData, object>(_ss.ToData(1), _ss.ToData("1")));
            dataList.Add(new KeyValuePair <IData, object>(_ss.ToData(2), _ss.ToData("2")));
            dataList.Add(new KeyValuePair <IData, object>(_ss.ToData(3), "3"));
            _testCollection = new ReadOnlyLazyDictionary <int, string, object>(dataList, _ss);
        }
        public void TestGlobalSerializer()
        {
            var config = new SerializationConfig();

            config.ConfigureGlobalSerializer(gs =>
            {
                gs.TypeName = typeof(GlobalSerializer).AssemblyQualifiedName;
            });
            var ss  = new SerializationServiceBuilder().SetConfig(config).Build();
            var foo = new CustomSerializableType {
                Value = "fooooo"
            };

            var d      = ss.ToData(foo);
            var newFoo = ss.ToObject <CustomSerializableType>(d);

            Assert.AreEqual(newFoo.Value, foo.Value);
        }
Esempio n. 16
0
        public void TestSerializationService_CreatePortableReader()
        {
            var serializationService = new SerializationServiceBuilder(new NullLoggerFactory()).Build();

            var timestamp1  = TestUtils.RandomLong();
            var child       = new ChildPortableObject(timestamp1);
            var timestamp2  = TestUtils.RandomLong();
            var parent      = new ParentPortableObject(timestamp2, child);
            var timestamp3  = timestamp1 + timestamp2;
            var grandParent = new GrandParentPortableObject(timestamp3, parent);

            var data   = serializationService.ToData(grandParent);
            var reader = serializationService.CreatePortableReader(data);

            Assert.AreEqual(grandParent.timestamp, reader.ReadLong("timestamp"));
            Assert.AreEqual(parent.timestamp, reader.ReadLong("child.timestamp"));
            Assert.AreEqual(child.timestamp, reader.ReadLong("child.child.timestamp"));
        }
Esempio n. 17
0
        public virtual void TestInputOutputWithPortableReader(Endianness endianness)
        {
            var portable = KitchenSinkPortable.Generate();

            var config = new SerializationOptions();

            config.AddPortableFactory(KitchenSinkPortableFactory.FactoryId, typeof(KitchenSinkPortableFactory));

            using var ss = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config)
                           .SetEndianness(endianness).Build();

            var data   = ss.ToData(portable);
            var reader = ss.CreatePortableReader(data);

            var actual = new KitchenSinkPortable();

            actual.ReadPortable(reader);

            Assert.AreEqual(portable, actual);
        }
        public virtual void TestCustomSerialize()
        {
            var config = new SerializationConfig();

            var sc = new SerializerConfig()
                     .SetImplementation(new CustomSerializer())
                     .SetTypeClass(typeof(CustomSerializableType));

            config.SerializerConfigs.Add(sc);
            var ss = new SerializationServiceBuilder().SetConfig(config).Build();

            var foo = new CustomSerializableType {
                Value = "fooooo"
            };

            var d      = ss.ToData(foo);
            var newFoo = ss.ToObject <CustomSerializableType>(d);

            Assert.AreEqual(newFoo.Value, foo.Value);
        }
        public void TestGlobalSerializer()
        {
            var config = new SerializationOptions();

            config.DefaultSerializer = new SerializerOptions
            {
                Creator = () => (ISerializer)ServiceFactory.CreateInstance(typeof(GlobalSerializer).AssemblyQualifiedName)
            };

            var ss = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();

            var foo = new CustomSerializableType {
                Value = "fooooo"
            };

            var d      = ss.ToData(foo);
            var newFoo = ss.ToObject <CustomSerializableType>(d);

            Assert.AreEqual(newFoo.Value, foo.Value);
        }
        public virtual void TestCustomSerialize()
        {
            var config = new SerializationOptions();

            var sc = new SerializerOptions
            {
                SerializedType = typeof(CustomSerializableType),
                Creator        = () => new CustomSerializer()
            };

            config.Serializers.Add(sc);
            var ss = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();

            var foo = new CustomSerializableType {
                Value = "fooooo"
            };

            var d      = ss.ToData(foo);
            var newFoo = ss.ToObject <CustomSerializableType>(d);

            Assert.AreEqual(newFoo.Value, foo.Value);
        }
Esempio n. 21
0
        public virtual void TestInputOutputWithPortableReader(ByteOrder byteOrder)
        {
            var portable = KitchenSinkPortable.Generate();

            var config = new SerializationConfig();

            config.AddPortableFactoryClass(KitchenSinkPortableFactory.FactoryId, typeof(KitchenSinkPortableFactory));

            var ss = new SerializationServiceBuilder().SetConfig(config).
                     SetUseNativeByteOrder(false).SetByteOrder(byteOrder).Build();

            var data   = ss.ToData(portable);
            var reader = ss.CreatePortableReader(data);

            var actual = new KitchenSinkPortable();

            actual.ReadPortable(reader);

            Assert.AreEqual(portable, actual);

            ss.Destroy();
        }
        public void TestWriteObjectWithCustomSerializable()
        {
            var config = new SerializationConfig();
            var sc     = new SerializerConfig()
                         .SetImplementation(new CustomSerializer())
                         .SetTypeClass(typeof(CustomSerializableType));

            config.AddSerializerConfig(sc);
            var serializationService =
                new SerializationServiceBuilder().SetPortableVersion(1)
                .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID, new TestPortableFactory())
                .SetConfig(config).Build();

            var foo = new CustomSerializableType {
                Value = "foo"
            };

            var objectCarryingPortable1 = new ObjectCarryingPortable(foo);
            var data = serializationService.ToData(objectCarryingPortable1);
            var objectCarryingPortable2 = serializationService.ToObject <ObjectCarryingPortable>(data);

            Assert.AreEqual(objectCarryingPortable1, objectCarryingPortable2);
        }
        public void TestGlobalSerializerOverride()
        {
            var config = new SerializationOptions();
            var globalListSerializer = new GlobalListSerializer();

            config.GlobalSerializer = new GlobalSerializerOptions
            {
                Creator = () => globalListSerializer,
                OverrideClrSerialization = true
            };

            var ss = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();

            var list = new List <string> {
                "foo", "bar"
            };

            var d     = ss.ToData(list);
            var input = ss.CreateObjectDataInput(d);

            var actual = (List <string>)globalListSerializer.Read(input);

            Assert.AreEqual(list, actual);
        }
        public void TestGlobalSerializerOverride()
        {
            var config = new SerializationConfig();

            var globalListSerializer = new GlobalListSerializer();

            config.ConfigureGlobalSerializer(gs =>
            {
                gs.Implementation           = globalListSerializer;
                gs.OverrideClrSerialization = true;
            });
            var ss = new SerializationServiceBuilder().SetConfig(config).Build();

            var list = new List <string> {
                "foo", "bar"
            };

            var d     = ss.ToData(list);
            var input = new ByteArrayObjectDataInput(d.ToByteArray(), HeapData.DataOffset, ss, ByteOrder.BigEndian);

            var actual = (List <string>)globalListSerializer.Read(input);

            Assert.AreEqual(list, actual);
        }