#pragma warning restore SYSLIB0011 // Type or member is obsolete private object SerializationLoop(object original) { var pipe = new Pipe(); using var writerSession = _sessionPool.GetSession(); var writer = Writer.Create(pipe.Writer, writerSession); _serializer.Serialize(original, ref writer); _ = pipe.Writer.FlushAsync().AsTask().GetAwaiter().GetResult(); pipe.Writer.Complete(); _ = pipe.Reader.TryRead(out var readResult); { using var readerSession = _sessionPool.GetSession(); var reader = Reader.Create(readResult.Buffer, readerSession); var output = BitStreamFormatter.Format(ref reader); _log.WriteLine(output); } { using var readerSession = _sessionPool.GetSession(); var reader = Reader.Create(readResult.Buffer, readerSession); var deserialized = _serializer.Deserialize(ref reader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); //Assert.True(Equals(original, deserialized), $"Deserialized value \"{deserialized}\" must equal original value \"{original}\""); Assert.Equal(writer.Position, reader.Position); Assert.Equal(writerSession.ReferencedObjects.CurrentReferenceId, readerSession.ReferencedObjects.CurrentReferenceId); return(deserialized); } }
private object RoundTripThroughUntypedSerializer(object original, out string formattedBitStream) { var pipe = new Pipe(); object result; using (var readerSession = _sessionPool.GetSession()) using (var writeSession = _sessionPool.GetSession()) { var writer = Writer.Create(pipe.Writer, writeSession); var serializer = _serviceProvider.GetService <Serializer <object> >(); serializer.Serialize(original, ref writer); _ = pipe.Writer.FlushAsync().AsTask().GetAwaiter().GetResult(); pipe.Writer.Complete(); _ = pipe.Reader.TryRead(out var readResult); using var analyzerSession = _sessionPool.GetSession(); formattedBitStream = BitStreamFormatter.Format(readResult.Buffer, analyzerSession); var reader = Reader.Create(readResult.Buffer, readerSession); result = serializer.Deserialize(ref reader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); } return(result); }
public void ProducesValidBitStream() { var serializer = _serviceProvider.GetRequiredService <Serializer <TValue> >(); foreach (var value in TestValues) { Test(value); } if (ValueProvider is { } valueProvider) { valueProvider(Test); } void Test(TValue value) { var array = serializer.SerializeToArray(value); var session = _sessionPool.GetSession(); var reader = Reader.Create(array, session); var formatted = new StringBuilder(); try { BitStreamFormatter.Format(ref reader, formatted); Assert.True(formatted.ToString() is string { Length: > 0 }); } catch (Exception exception) { Assert.True(false, $"Formatting failed with exception: {exception} and partial result: \"{formatted}\""); } }
private TActual RoundTripToExpectedType <TBase, TActual>(TActual original) where TActual : TBase { var serializer = _serviceProvider.GetService <Serializer <TBase> >(); var array = serializer.SerializeToArray(original); string formatted; { using var session = _serviceProvider.GetRequiredService <SerializerSessionPool>().GetSession(); formatted = BitStreamFormatter.Format(array, session); } return((TActual)serializer.Deserialize(array)); }
public void ProducesValidBitStream() { var serializer = _serviceProvider.GetRequiredService <Serializer <TValue> >(); foreach (var value in TestValues) { var array = serializer.SerializeToArray(value); using var session = _sessionPool.GetSession(); var reader = Reader.Create(array, session); var formatted = new StringBuilder(); try { BitStreamFormatter.Format(ref reader, formatted); Assert.True(formatted.ToString() is string { Length: > 0 }); }
public static void TestOne() { Console.WriteLine("Hello World!"); var serviceProvider = new ServiceCollection() .AddHagar(builder => builder.AddAssembly(typeof(SomeClassWithSerialzers).Assembly)) .BuildServiceProvider(); var codecs = serviceProvider.GetRequiredService <ITypedCodecProvider>(); var codec = codecs.GetCodec <SomeClassWithSerialzers>(); var sessionPool = serviceProvider.GetRequiredService <SerializerSessionPool>(); var writeSession = sessionPool.GetSession(); var pipe = new Pipe(); var writer = Writer.Create(pipe.Writer, writeSession); codec.WriteField(ref writer, 0, typeof(SomeClassWithSerialzers), new SomeClassWithSerialzers { IntField = 2, IntProperty = 30 }); writer.Commit(); _ = pipe.Writer.FlushAsync().AsTask().GetAwaiter().GetResult(); pipe.Writer.Complete(); _ = pipe.Reader.TryRead(out var readResult); { using var readerSession = sessionPool.GetSession(); var reader = Reader.Create(readResult.Buffer, readerSession); var result = BitStreamFormatter.Format(ref reader); Console.WriteLine(result); } { using var readerSession = sessionPool.GetSession(); var reader = Reader.Create(readResult.Buffer, readerSession); var initialHeader = reader.ReadFieldHeader(); var result = codec.ReadValue(ref reader, initialHeader); Console.WriteLine(result); } }
private static void Test <T>(Func <SerializerSession> getSession, IFieldCodec <T> serializer, T expected) { using var writerSession = getSession(); var pipe = new Pipe(); var writer = Writer.Create(pipe.Writer, writerSession); serializer.WriteField(ref writer, 0, typeof(T), expected); writer.Commit(); Console.WriteLine($"Size: {writer.Position} bytes."); Console.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 result = BitStreamFormatter.Format(ref reader); Console.WriteLine(result); } { using var readerSesssion = getSession(); var reader = Reader.Create(readResult.Buffer, readerSesssion); var initialHeader = reader.ReadFieldHeader(); Console.WriteLine("Header:"); Console.WriteLine(initialHeader.ToString()); var actual = serializer.ReadValue(ref reader, initialHeader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); Console.WriteLine($"Expect: {expected}\nActual: {actual}"); var references = GetReadReferenceTable(reader.Session); Console.WriteLine($"Read references:\n{references}"); } }
public void Serialize_UnserializableException() { const string message = "This is a test message"; var serializer = _serviceProvider.GetRequiredService <Serializer>(); // Throw the exception so that stack trace is populated Exception source = Assert.Throws <UnserializableNonConformingException>((Action)(() => { throw new UnserializableNonConformingException(message); })); var serialized = serializer.SerializeToArray(source); using var formatterSession = _sessionPool.GetSession(); var formatted = BitStreamFormatter.Format(serialized, formatterSession); object deserialized = serializer.Deserialize <Exception>(serialized); // Type is wrong after round trip of unserializable exception var result = Assert.IsAssignableFrom <UnavailableExceptionFallbackException>(deserialized); // Exception message is correct after round trip of unserializable exception Assert.Contains(message, result.Message); Assert.Equal(RuntimeTypeNameFormatter.Format(source.GetType()), result.ExceptionType); // Throw the exception so that stack trace is populated source = Assert.Throws <UnserializableConformingException>((Action)(() => { Exception inner; try { throw new InvalidOperationException("invalid"); } catch (Exception exception) { inner = exception; } throw new UnserializableConformingException(message, inner) { SomeObject = new object(), SubClassField = "hoppo", BaseField = new SimpleISerializableObject() { Payload = "payload" } }; })); deserialized = serializer.Deserialize <Exception>(serializer.SerializeToArray(source)); // Type is wrong after round trip of unserializable exception result = Assert.IsAssignableFrom <UnavailableExceptionFallbackException>(deserialized); // Exception message is correct after round trip of unserializable exception Assert.Contains(message, result.Message); Assert.Equal(RuntimeTypeNameFormatter.Format(source.GetType()), result.ExceptionType); var inner = Assert.IsType <InvalidOperationException>(result.InnerException); Assert.Equal("invalid", inner.Message); Assert.True(result.Properties.ContainsKey("SomeObject")); var baseField = Assert.IsType <SimpleISerializableObject>(result.Properties["BaseField"]); Assert.Equal("payload", baseField.Payload); }