private void TestSkip(SubType expected) { using var writerSession = GetSession(); var pipe = new Pipe(); var writer = Writer.Create(pipe.Writer, writerSession); _serializer.WriteField(ref writer, 0, typeof(SubType), expected); writer.Commit(); _ = pipe.Writer.FlushAsync().AsTask().GetAwaiter().GetResult(); var objectWithNewFieldSerializer = _codecProvider.GetCodec <ObjectWithNewField>(); var objectWithoutNewFieldSerializer = _codecProvider.GetCodec <ObjectWithoutNewField>(); pipe.Writer.Complete(); _ = pipe.Reader.TryRead(out var readResult); using var readerSession = GetSession(); var reader = Reader.Create(readResult.Buffer, readerSession); var initialHeader = reader.ReadFieldHeader(); var skipCodec = new SkipFieldCodec(); _ = skipCodec.ReadValue(ref reader, initialHeader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); Assert.Equal(writer.Session.ReferencedObjects.CurrentReferenceId, reader.Session.ReferencedObjects.CurrentReferenceId); _log.WriteLine($"Skipped {reader.Position} bytes."); }
private void RoundTripTest(SubType expected, bool assertRef = true) { using var writerSession = GetSession(); var pipe = new Pipe(); var writer = Writer.Create(pipe.Writer, writerSession); _serializer.WriteField(ref writer, 0, typeof(SubType), expected); writer.Commit(); _log.WriteLine($"Size: {writer.Position} bytes."); _log.WriteLine($"Wrote References:\n{GetWriteReferenceTable(writerSession)}"); _ = pipe.Writer.FlushAsync().AsTask().GetAwaiter().GetResult(); pipe.Writer.Complete(); _ = pipe.Reader.TryRead(out var readResult); using var readerSesssion = GetSession(); var reader = Reader.Create(readResult.Buffer, readerSesssion); var initialHeader = reader.ReadFieldHeader(); _log.WriteLine("Header:"); _log.WriteLine(initialHeader.ToString()); var actual = _serializer.ReadValue(ref reader, initialHeader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); _log.WriteLine($"Expect: {expected}\nActual: {actual}"); Assert.Equal(expected.BaseTypeString, actual.BaseTypeString); Assert.Null(actual.AddedLaterString); // The deserializer isn't 'aware' of this field which was added later - version tolerance. Assert.Equal(expected.String, actual.String); Assert.Equal(expected.Int, actual.Int); if (assertRef) { Assert.Equal(expected.Ref, actual.Ref); } Assert.Equal(writer.Position, reader.Position); Assert.Equal(writer.Session.ReferencedObjects.CurrentReferenceId, reader.Session.ReferencedObjects.CurrentReferenceId); var references = GetReadReferenceTable(reader.Session); _log.WriteLine($"Read references:\n{references}"); }
public void VersionTolerance_RoundTrip_Tests() { RoundTripTest( new SubType { BaseTypeString = "HOHOHO", AddedLaterString = TestString, String = null, Int = 1, Ref = TestString }); RoundTripTest( new SubType { BaseTypeString = "base", String = "sub", Int = 2, }); RoundTripTest( new SubType { BaseTypeString = "base", String = "sub", Int = int.MinValue, }); RoundTripTest( new SubType { BaseTypeString = TestString, String = TestString, Int = 10 }); RoundTripTest( new SubType { BaseTypeString = TestString, String = null, Int = 1 }); RoundTripTest( new SubType { BaseTypeString = TestString, String = null, Int = 1 }); TestSkip( new SubType { BaseTypeString = TestString, String = null, Int = 1 }); var self = new SubType { BaseTypeString = "HOHOHO", AddedLaterString = TestString, String = null, Int = 1 }; self.Ref = self; RoundTripTest(self, assertRef: false); self.Ref = Guid.NewGuid(); RoundTripTest(self, assertRef: false); }