public void ConcurrentObservableQueueSerializationTest() { // ARRANGE // ======= // A random generator (we'll test random values to avoid hard-codings) var r = new Random(); // The data contract serializer we'll use to serialize and deserialize var dcs = new DataContractSerializer(typeof(ConcurrentObservableQueue <DummyDataContract>)); // The dummy data var ddc1 = new DummyDataContract { RandomValue = r.Next() }; var ddc2 = new DummyDataContract { RandomValue = r.Next() }; var ddc3 = new DummyDataContract { RandomValue = r.Next() }; var ddc4 = new DummyDataContract { RandomValue = r.Next() }; // The original observable list using (var l1 = new ConcurrentObservableQueue <DummyDataContract> { ddc1, ddc2, ddc3, ddc4, }) { // The deserialized list ConcurrentObservableQueue <DummyDataContract> l2; // The serialization content string content; // ACT // === using (var ms = new MemoryStream()) { dcs.WriteObject( ms, l1); _ = ms.Seek( 0, SeekOrigin.Begin); using (var textReader = new StreamReader( ms, Encoding.UTF8, false, 32768, true)) { content = textReader.ReadToEnd(); } _ = ms.Seek( 0, SeekOrigin.Begin); l2 = dcs.ReadObject(ms) as ConcurrentObservableQueue <DummyDataContract>; } try { // ASSERT // ====== // Serialization content is OK Assert.False(string.IsNullOrWhiteSpace(content)); Assert.Equal( $@"<ConcurrentObservableDDCQueue xmlns=""http://ns.ixiancorp.com/IX/IX.Observable"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:a=""http://schemas.datacontract.org/2004/07/IX.UnitTests.Observable""><Item><a:RandomValue>{ddc1.RandomValue}</a:RandomValue></Item><Item><a:RandomValue>{ddc2.RandomValue}</a:RandomValue></Item><Item><a:RandomValue>{ddc3.RandomValue}</a:RandomValue></Item><Item><a:RandomValue>{ddc4.RandomValue}</a:RandomValue></Item></ConcurrentObservableDDCQueue>", content); // Deserialized object is OK Assert.NotNull(l2); Assert.Equal( l1.Count, l2.Count); Assert.True(l1.SequenceEquals(l2)); } finally { l2?.Dispose(); } } }
public void ConcurrentObservableDictionarySerializationTest() { // ARRANGE // ======= // A random generator (we'll test random values to avoid hard-codings) var r = new Random(); // The data contract serializer we'll use to serialize and deserialize var dcs = new DataContractSerializer(typeof(ConcurrentObservableDictionary <int, DummyDataContract>)); // The dummy data var ddc1 = new DummyDataContract { RandomValue = r.Next() }; var ddc2 = new DummyDataContract { RandomValue = r.Next() }; var ddc3 = new DummyDataContract { RandomValue = r.Next() }; var ddc4 = new DummyDataContract { RandomValue = r.Next() }; // The original observable list using (var l1 = new ConcurrentObservableDictionary <int, DummyDataContract> { [ddc1.RandomValue] = ddc1, [ddc2.RandomValue] = ddc2, [ddc3.RandomValue] = ddc3, [ddc4.RandomValue] = ddc4, }) { // The deserialized list ConcurrentObservableDictionary <int, DummyDataContract> l2; // The serialization content string content; // ACT // === using (var ms = new MemoryStream()) { dcs.WriteObject( ms, l1); _ = ms.Seek( 0, SeekOrigin.Begin); using (var textReader = new StreamReader( ms, Encoding.UTF8, false, 32768, true)) { content = textReader.ReadToEnd(); } _ = ms.Seek( 0, SeekOrigin.Begin); l2 = dcs.ReadObject(ms) as ConcurrentObservableDictionary <int, DummyDataContract>; } try { // ASSERT // ====== // Serialization content is OK Assert.False(string.IsNullOrWhiteSpace(content)); Assert.Equal( $@"<ConcurrentObservableDDCDictionaryByint xmlns=""http://ns.ixiancorp.com/IX/IX.Observable"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Entry><Key>{ddc1.RandomValue}</Key><Value xmlns:a=""http://schemas.datacontract.org/2004/07/IX.UnitTests.Observable""><a:RandomValue>{ddc1.RandomValue}</a:RandomValue></Value></Entry><Entry><Key>{ddc2.RandomValue}</Key><Value xmlns:a=""http://schemas.datacontract.org/2004/07/IX.UnitTests.Observable""><a:RandomValue>{ddc2.RandomValue}</a:RandomValue></Value></Entry><Entry><Key>{ddc3.RandomValue}</Key><Value xmlns:a=""http://schemas.datacontract.org/2004/07/IX.UnitTests.Observable""><a:RandomValue>{ddc3.RandomValue}</a:RandomValue></Value></Entry><Entry><Key>{ddc4.RandomValue}</Key><Value xmlns:a=""http://schemas.datacontract.org/2004/07/IX.UnitTests.Observable""><a:RandomValue>{ddc4.RandomValue}</a:RandomValue></Value></Entry></ConcurrentObservableDDCDictionaryByint>", content); // Deserialized object is OK Assert.NotNull(l2); Assert.Equal( l1.Count, l2.Count); #pragma warning disable HAA0401 // Possible allocation of reference type enumerator - Acceptable in this unit test foreach (var key in l1.Keys) #pragma warning restore HAA0401 // Possible allocation of reference type enumerator { Assert.True(l1[key].Equals(l2[key])); } } finally { l2?.Dispose(); } } }