Beispiel #1
0
        public void MultipleThreads()
        {
            // Use local options to avoid obtaining already cached metadata from the default options.
            var options = new JsonSerializerOptions();

            // Verify the test class has >32 properties since that is a threshold for using the fallback dictionary.
            Assert.True(typeof(ClassWithConstructor_SimpleAndComplexParameters).GetProperties(BindingFlags.Instance | BindingFlags.Public).Length > 32);

            void DeserializeObjectMinimal()
            {
                var obj = Serializer.Deserialize <ClassWithConstructor_SimpleAndComplexParameters>(@"{""MyDecimal"" : 3.3}", options);
            };

            void DeserializeObjectFlipped()
            {
                var obj = Serializer.Deserialize <ClassWithConstructor_SimpleAndComplexParameters>(
                    ClassWithConstructor_SimpleAndComplexParameters.s_json_flipped, options);

                obj.Verify();
            };

            void DeserializeObjectNormal()
            {
                var obj = Serializer.Deserialize <ClassWithConstructor_SimpleAndComplexParameters>(
                    ClassWithConstructor_SimpleAndComplexParameters.s_json, options);

                obj.Verify();
            };

            void SerializeObject()
            {
                var obj = ClassWithConstructor_SimpleAndComplexParameters.GetInstance();

                JsonSerializer.Serialize(obj, options);
            };

            const int ThreadCount          = 8;
            const int ConcurrentTestsCount = 4;

            Task[] tasks = new Task[ThreadCount * ConcurrentTestsCount];

            for (int i = 0; i < tasks.Length; i += ConcurrentTestsCount)
            {
                // Create race condition to populate the sorted property cache with different json ordering.
                tasks[i + 0] = Task.Run(() => DeserializeObjectMinimal());
                tasks[i + 1] = Task.Run(() => DeserializeObjectFlipped());
                tasks[i + 2] = Task.Run(() => DeserializeObjectNormal());

                // Ensure no exceptions on serialization
                tasks[i + 3] = Task.Run(() => SerializeObject());
            }
            ;

            Task.WaitAll(tasks);
        }
Beispiel #2
0
        public async Task PropertyCacheWithMinInputsFirst()
        {
            // Use local options to avoid obtaining already cached metadata from the default options.
            var options = new JsonSerializerOptions();

            string json = "{}";
            await Serializer.DeserializeWrapper <ClassWithConstructor_SimpleAndComplexParameters>(json, options);

            ClassWithConstructor_SimpleAndComplexParameters testObj = ClassWithConstructor_SimpleAndComplexParameters.GetInstance();

            testObj.Verify();

            json    = JsonSerializer.Serialize(testObj, options);
            testObj = await Serializer.DeserializeWrapper <ClassWithConstructor_SimpleAndComplexParameters>(json, options);

            testObj.Verify();
        }
Beispiel #3
0
        public async Task MultipleThreads()
        {
            // Verify the test class has >32 properties since that is a threshold for using the fallback dictionary.
            Assert.True(typeof(ClassWithConstructor_SimpleAndComplexParameters).GetProperties(BindingFlags.Instance | BindingFlags.Public).Length > 32);

            async Task DeserializeObjectAsync(string json, Type type, JsonSerializerOptions options)
            {
                var obj = await Serializer.DeserializeWrapper(json, type, options);

                ((ITestClassWithParameterizedCtor)obj).Verify();
            }

            async Task DeserializeObjectMinimalAsync(Type type, JsonSerializerOptions options)
            {
                string json = (string)type.GetProperty("s_json_minimal").GetValue(null);
                var    obj  = await Serializer.DeserializeWrapper(json, type, options);

                ((ITestClassWithParameterizedCtor)obj).VerifyMinimal();
            };

            async Task DeserializeObjectFlippedAsync(Type type, JsonSerializerOptions options)
            {
                string json = (string)type.GetProperty("s_json_flipped").GetValue(null);

                await DeserializeObjectAsync(json, type, options);
            };

            async Task DeserializeObjectNormalAsync(Type type, JsonSerializerOptions options)
            {
                string json = (string)type.GetProperty("s_json").GetValue(null);

                await DeserializeObjectAsync(json, type, options);
            };

            void SerializeObject(Type type, JsonSerializerOptions options)
            {
                var obj = ClassWithConstructor_SimpleAndComplexParameters.GetInstance();

                JsonSerializer.Serialize(obj, options);
            };

            async Task RunTestAsync(Type type)
            {
                // Use local options to avoid obtaining already cached metadata from the default options.
                var options = new JsonSerializerOptions();

                const int ThreadCount          = 8;
                const int ConcurrentTestsCount = 4;

                Task[] tasks = new Task[ThreadCount * ConcurrentTestsCount];

                for (int i = 0; i < tasks.Length; i += ConcurrentTestsCount)
                {
                    // Create race condition to populate the sorted property cache with different json ordering.
                    tasks[i + 0] = Task.Run(() => DeserializeObjectMinimalAsync(type, options));
                    tasks[i + 1] = Task.Run(() => DeserializeObjectFlippedAsync(type, options));
                    tasks[i + 2] = Task.Run(() => DeserializeObjectNormalAsync(type, options));

                    // Ensure no exceptions on serialization
                    tasks[i + 3] = Task.Run(() => SerializeObject(type, options));
                }
                ;

                await Task.WhenAll(tasks);
            }

            await RunTestAsync(typeof(ClassWithConstructor_SimpleAndComplexParameters));
            await RunTestAsync(typeof(Person_Class));
            await RunTestAsync(typeof(Parameterized_Class_With_ComplexTuple));
        }