예제 #1
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 <ObjWCtorMixedParams>(json, options);

            ObjWCtorMixedParams testObj = ObjWCtorMixedParams.GetInstance();

            testObj.Verify();

            json = await Serializer.SerializeWrapper(testObj, options);

            testObj = await Serializer.DeserializeWrapper <ObjWCtorMixedParams>(json, options);

            testObj.Verify();
        }
예제 #2
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(ObjWCtorMixedParams).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);
            };

            async Task SerializeObject(Type type, JsonSerializerOptions options)
            {
                var obj = ObjWCtorMixedParams.GetInstance();
                await Serializer.SerializeWrapper(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(ObjWCtorMixedParams));
            await RunTestAsync(typeof(Person_Class));
            await RunTestAsync(typeof(Parameterized_Class_With_ComplexTuple));
        }