public void AzureQueueBatchContainerV2_Serialize_IfNotNull() { var container = CreateAzureQueueBatchContainer(); var writer = new SerializationContext { StreamWriter = new BinaryTokenStreamWriter() }; AzureQueueBatchContainerV2.Serialize(container, writer, null); var reader = new DeserializationContext { StreamReader = new BinaryTokenStreamReader(writer.StreamWriter.ToByteArray()) }; var deserialized = AzureQueueBatchContainerV2.Deserialize(typeof(AzureQueueBatchContainer), reader) as AzureQueueBatchContainerV2; ValidateIdenticalQueueBatchContainerButNotSame(container, deserialized); var streamWriter = new BinaryTokenStreamWriter(); SerializationManager.Serialize(container, streamWriter); var streamReader = new BinaryTokenStreamReader(streamWriter.ToByteArray()); deserialized = SerializationManager.Deserialize <AzureQueueBatchContainerV2>(streamReader); ValidateIdenticalQueueBatchContainerButNotSame(container, deserialized); }
private static byte[] GetTokenBytes(StreamSequenceToken token) { var bodyStream = new BinaryTokenStreamWriter(); SerializationManager.Serialize(token, bodyStream); return(bodyStream.ToByteArray()); }
public static byte[] SerializeProperties(this IDictionary <string, object> properties) { var writeStream = new BinaryTokenStreamWriter(); SerializationManager.Serialize(properties, writeStream); return(writeStream.ToByteArray()); }
/// <summary> /// Serializes event data properties /// </summary> /// <param name="eventData"></param> /// <returns></returns> public static byte[] SerializeProperties(this EventData eventData) { var writeStream = new BinaryTokenStreamWriter(); SerializationManager.Serialize(eventData.Properties.Where(kvp => !SkipProperties.Contains(kvp.Key)).ToList(), writeStream); return(writeStream.ToByteArray()); }
internal byte[] ToByteArray() { var writer = new BinaryTokenStreamWriter(); writer.Write(this); return(writer.ToByteArray()); }
private ILExceptionSerializerTestException TestExceptionSerialization(ILExceptionSerializerTestException expected) { var writer = new BinaryTokenStreamWriter(); // Deep copies should be reference-equal. Assert.Equal( expected, SerializationManager.DeepCopyInner(expected, new SerializationContext(this.environment.SerializationManager)), ReferenceEqualsComparer.Instance); this.environment.SerializationManager.Serialize(expected, writer); var reader = new DeserializationContext(this.environment.SerializationManager) { StreamReader = new BinaryTokenStreamReader(writer.ToByteArray()) }; var actual = (ILExceptionSerializerTestException)this.environment.SerializationManager.Deserialize(null, reader.StreamReader); Assert.Equal(expected.BaseField.Value, actual.BaseField.Value, StringComparer.Ordinal); Assert.Equal(expected.SubClassField, actual.SubClassField, StringComparer.Ordinal); Assert.Equal(expected.OtherField.Value, actual.OtherField.Value, StringComparer.Ordinal); // Check for referential equality in the two fields which happened to be reference-equals. Assert.Equal(actual.BaseField, actual.OtherField, ReferenceEqualsComparer.Instance); return(actual); }
public void ExceptionSerializer_NestedReferenceCycle() { // Throw an exception so that is has a stack trace. var exception = GetNewException(); var expected = new Outer { SomeFunObject = exception.OtherField, Object = exception, }; // Create a reference cycle. exception.SomeObject = expected; var writer = new BinaryTokenStreamWriter(); this.environment.SerializationManager.Serialize(expected, writer); var reader = new DeserializationContext(this.environment.SerializationManager) { StreamReader = new BinaryTokenStreamReader(writer.ToByteArray()) }; var actual = (Outer)this.environment.SerializationManager.Deserialize(null, reader.StreamReader); Assert.Equal(expected.Object.BaseField.Value, actual.Object.BaseField.Value, StringComparer.Ordinal); Assert.Equal(expected.Object.SubClassField, actual.Object.SubClassField, StringComparer.Ordinal); Assert.Equal(expected.Object.OtherField.Value, actual.Object.OtherField.Value, StringComparer.Ordinal); // Check for referential equality in the fields which happened to be reference-equals. Assert.Equal(actual.Object.BaseField, actual.Object.OtherField, ReferenceEqualsComparer.Instance); Assert.Equal(actual, actual.Object.SomeObject, ReferenceEqualsComparer.Instance); Assert.Equal(actual.SomeFunObject, actual.Object.OtherField, ReferenceEqualsComparer.Instance); }
public void ILSerializer_AllowCopiedFieldsToDifferFromSerializedFields() { var input = new FieldTest { One = 1, Two = 2, Three = 3 }; var generator = new ILSerializerGenerator(); var serializers = generator.GenerateSerializer(input.GetType(), f => f.Name != "One", f => f.Name != "Three"); var copy = (FieldTest)serializers.DeepCopy(input); Assert.Equal(1, copy.One); Assert.Equal(2, copy.Two); Assert.Equal(0, copy.Three); var writer = new BinaryTokenStreamWriter(); serializers.Serialize(input, writer, input.GetType()); var reader = new BinaryTokenStreamReader(writer.ToByteArray()); var deserialized = (FieldTest)serializers.Deserialize(input.GetType(), reader); Assert.Equal(0, deserialized.One); Assert.Equal(2, deserialized.Two); Assert.Equal(3, deserialized.Three); }
public void AzureQueueBatchContainer_VerifyBothMessageTypesCanBeDeserialized() { var container = new AzureQueueBatchContainer(Guid.NewGuid(), "namespace", new List <object> { "item" }, new Dictionary <string, object>() { { "key", "value" } }, new EventSequenceToken(long.MaxValue, int.MaxValue)); var writer = new BinaryTokenStreamWriter(); SerializationManager.Serialize(container, writer); var bytes = writer.ToByteArray(); writer = new BinaryTokenStreamWriter(); var container2 = new AzureQueueBatchContainerV2(Guid.NewGuid(), "namespace", new List <object> { "item" }, new Dictionary <string, object>() { { "key", "value" } }, new EventSequenceTokenV2(long.MaxValue, int.MaxValue)); SerializationManager.Serialize(container2, writer); var bytes2 = writer.ToByteArray(); var msg = new CloudQueueMessage(bytes); var msg2 = new CloudQueueMessage(bytes2); var bc1 = (IBatchContainer)AzureQueueBatchContainer.FromCloudQueueMessage(msg, 0); var bc2 = (IBatchContainer)AzureQueueBatchContainer.FromCloudQueueMessage(msg2, 0); Assert.NotNull(bc1); Assert.NotNull(bc2); }
internal uint GetUniformHashCode() { // Disabling this ReSharper warning; hashCache is a logically read-only variable, so accessing them in GetHashCode is safe. // ReSharper disable NonReadonlyFieldInGetHashCode if (uniformHashCache == 0) { JenkinsHash jenkinsHash = JenkinsHash.Factory.GetHashGenerator(); uint n; if (HasKeyExt && KeyExt != null) { var writer = new BinaryTokenStreamWriter(); writer.Write(this); byte[] bytes = writer.ToByteArray(); n = jenkinsHash.ComputeHash(bytes); } else { n = jenkinsHash.ComputeHash(TypeCodeData, N0, N1); } // Unchecked is required because the Jenkins hash is an unsigned 32-bit integer, // which we need to convert to a signed 32-bit integer. uniformHashCache = n; } return(uniformHashCache); // ReSharper restore NonReadonlyFieldInGetHashCode }
public static byte[] SerializeProperties(this IDictionary <string, object> properties) { var writeStream = new BinaryTokenStreamWriter(); SerializationManager.Serialize(properties.Where(kvp => !SkipProperties.Contains(kvp.Key)).ToList(), writeStream); return(writeStream.ToByteArray()); }
private static byte[] GetTokenBytes(SerializationManager serializationManager, StreamSequenceToken token) { var bodyStream = new BinaryTokenStreamWriter(); serializationManager.Serialize(token, bodyStream); var result = bodyStream.ToByteArray(); bodyStream.ReleaseBuffers(); return result; }
private uint GetUniformHashCode(JenkinsHash jenkinsHash, int extraBit) { var writer = new BinaryTokenStreamWriter(); writer.Write(this); writer.Write(extraBit); byte[] bytes = writer.ToByteArray(); return(jenkinsHash.ComputeHash(bytes)); }
public void SiloAddressGetUniformHashCodes() { int numberofHash = 3; var siloAddress = SiloAddress.New(new IPEndPoint(IPAddress.Loopback, 8080), 26); var result = siloAddress.GetUniformHashCodes(numberofHash); for (int i = 0; i < numberofHash; i++) { var sw = new BinaryTokenStreamWriter(); sw.Write(siloAddress); sw.Write(i); var tmp = sw.ToByteArray(); var expected = JenkinsHash.ComputeHash(sw.ToByteArray()); Assert.Equal(expected, result[i]); } }
public static byte[] ToByteArray(this GrainId @this) { var writer = new BinaryTokenStreamWriter(); writer.Write(@this); var result = writer.ToByteArray(); writer.ReleaseBuffers(); return(result); }
internal byte[] ToByteArray() { var writer = new BinaryTokenStreamWriter(); writer.Write(this); var result = writer.ToByteArray(); writer.ReleaseBuffers(); return(result); }
/// <summary> /// Serializes event data properties /// </summary> /// <param name="eventData"></param> /// <param name="serializationManager"></param> /// <returns></returns> public static byte[] SerializeProperties(this EventData eventData, SerializationManager serializationManager) { var writeStream = new BinaryTokenStreamWriter(); serializationManager.Serialize(eventData.Properties.Where(kvp => !string.Equals(kvp.Key, EventDataPropertyStreamNamespaceKey, StringComparison.Ordinal)).ToList(), writeStream); var result = writeStream.ToByteArray(); writeStream.ReleaseBuffers(); return(result); }
/// <summary> /// Serializes event data properties /// </summary> /// <param name="eventData"></param> /// <param name="serializationManager"></param> /// <returns></returns> public static byte[] SerializeProperties(this EventData eventData, SerializationManager serializationManager) { var writeStream = new BinaryTokenStreamWriter(); serializationManager.Serialize(eventData.Properties.Where(kvp => !SkipProperties.Contains(kvp.Key)).ToList(), writeStream); var result = writeStream.ToByteArray(); writeStream.ReleaseBuffers(); return(result); }
public void RecordException(Exception e, SerializationManager sm) { if (OriginalException == null) { var exception = (e as OrleansTransactionAbortedException) ?? new OrleansTransactionAbortedException(TransactionId.ToString(), e); var writer = new BinaryTokenStreamWriter(); sm.Serialize(exception, writer); OriginalException = writer.ToByteArray(); } }
public static void VerifyUsingFallbackSerializer(object ob) { var writer = new BinaryTokenStreamWriter(); SerializationManager.FallbackSerializer(ob, writer, ob.GetType()); var bytes = writer.ToByteArray(); var reader = new BinaryTokenStreamReader(bytes); var serToken = reader.ReadToken(); Assert.Equal(SerializationTokenType.Fallback, serToken); }
public void EventHubSequenceTokenV2_Serialize_IfNotNull() { var writer = new BinaryTokenStreamWriter(); var token = new EventHubSequenceTokenV2("name", long.MaxValue, int.MaxValue); EventHubSequenceTokenV2.Serialize(token, writer, null); var reader = new BinaryTokenStreamReader(writer.ToByteArray()); var deserialized = EventHubSequenceTokenV2.Deserialize(typeof(EventHubSequenceTokenV2), reader) as EventHubSequenceTokenV2; Assert.NotNull(deserialized); Assert.NotSame(token, deserialized); Assert.Equal(token.EventIndex, deserialized.EventIndex); Assert.Equal(token.SequenceNumber, deserialized.SequenceNumber); }
private byte[] SerializeCommitRecords(List <CommitRecord> commitRecords) { var serializableList = new List <Tuple <long, long, HashSet <ITransactionalResource> > >(commitRecords.Count); foreach (var commitRecord in commitRecords) { serializableList.Add(new Tuple <long, long, HashSet <ITransactionalResource> >(commitRecord.LSN, commitRecord.TransactionId, commitRecord.Resources)); } var streamWriter = new BinaryTokenStreamWriter(); serializationManager.Serialize(serializableList, streamWriter); return(streamWriter.ToByteArray()); }
internal static object Reserialize(IMessageSerializer serializer, object message) { if (serializer == null) { return(message); } var writer = new BinaryTokenStreamWriter(); serializer.Serialize(message, writer); var bytes = writer.ToByteArray(); var reader = new BinaryTokenStreamReader(bytes); return(serializer.Deserialize(reader)); }
public void UniqueKeyToByteArrayWithoutKeyExt() { var key = UniqueKey.NewKey(Guid.NewGuid(), category: UniqueKey.Category.Client); var result = key.ToByteArray(); var sw = new BinaryTokenStreamWriter(); sw.Write(key); var expected = sw.ToByteArray(); Assert.Equal(expected.Length, result.Length); for (int i = 0; i < expected.Length; i++) { Assert.Equal(expected[i], result[i]); } }
public void ILSerializer_LifecycleHooksAreCalled() { var input = new FieldTest(); var generator = new ILSerializerGenerator(); var serializers = generator.GenerateSerializer(input.GetType()); var writer = new BinaryTokenStreamWriter(); var context = new SerializationContext(this.fixture.SerializationManager) { StreamWriter = writer }; serializers.Serialize(input, context, input.GetType()); var reader = new DeserializationContext(this.fixture.SerializationManager) { StreamReader = new BinaryTokenStreamReader(writer.ToByteArray()) }; var deserialized = (FieldTest)serializers.Deserialize(input.GetType(), reader); Assert.Null(input.Context); Assert.NotNull(deserialized.Context); Assert.Equal(this.fixture.SerializationManager, deserialized.Context.GetSerializationManager()); }
public void EventHubSequenceTokenV2_DeepCopy_IfNotNull() { var token = new EventHubSequenceTokenV2("name", long.MaxValue, int.MaxValue); var copy = EventHubSequenceTokenV2.DeepCopy(token) as EventSequenceToken; Assert.NotNull(copy); Assert.NotSame(token, copy); Assert.Equal(token.EventIndex, copy.EventIndex); Assert.Equal(token.SequenceNumber, copy.SequenceNumber); var writer = new BinaryTokenStreamWriter(); SerializationManager.Serialize(token, writer); var bytes = writer.ToByteArray(); var reader = new BinaryTokenStreamReader(bytes); copy = SerializationManager.Deserialize(reader) as EventHubSequenceTokenV2; Assert.NotNull(copy); Assert.NotSame(token, copy); Assert.Equal(token.EventIndex, copy.EventIndex); Assert.Equal(token.SequenceNumber, copy.SequenceNumber); }
private void RunGrainReferenceSerializationTest <TGrainInterface>() { var counters = new List <CounterStatistic> { CounterStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BODY_FALLBACK_SERIALIZATION), CounterStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BODY_FALLBACK_DESERIALIZATION), CounterStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BODY_FALLBACK_DEEPCOPIES) }; // Get the (generated) grain reference implementation for a particular grain interface type. var grainReference = this.GetGrainReference <TGrainInterface>(); // Get the current value of each of the fallback serialization counters. var initial = counters.Select(_ => _.GetCurrentValue()).ToList(); // Serialize and deserialize the grain reference. var writer = new BinaryTokenStreamWriter(); SerializationManager.Serialize(grainReference, writer); var deserialized = SerializationManager.Deserialize(new BinaryTokenStreamReader(writer.ToByteArray())); var copy = (GrainReference)SerializationManager.DeepCopy(deserialized); // Get the final value of the fallback serialization counters. var final = counters.Select(_ => _.GetCurrentValue()).ToList(); // Ensure that serialization was correctly performed. Assert.IsAssignableFrom(grainReference.GetType(), deserialized); //Deserialized GrainReference type should match original type var deserializedGrainReference = (GrainReference)deserialized; Assert.True( deserializedGrainReference.GrainId.Equals(grainReference.GrainId), "Deserialized GrainReference should have same GrainId as original value."); Assert.IsAssignableFrom(grainReference.GetType(), copy); Assert.True( copy.GrainId.Equals(grainReference.GrainId), "DeepCopy GrainReference should have same GrainId as original value."); // Check that the counters have not changed. var initialString = string.Join(",", initial); var finalString = string.Join(",", final); Assert.Equal(initialString, finalString); // GrainReference serialization should not use fallback serializer. }
public T RoundTripSerialization <T>(T source) { BinaryTokenStreamWriter writer = new BinaryTokenStreamWriter(); SerializationManager.Serialize(source, writer); T output = (T)SerializationManager.Deserialize(new BinaryTokenStreamReader(writer.ToByteArray())); return(output); }
public void ExceptionSerializer_UnknownException() { var expected = GetNewException(); var knowsException = new ILBasedExceptionSerializer(this.serializerGenerator, new TypeSerializer(new CachedTypeResolver())); var writer = new BinaryTokenStreamWriter(); var context = new SerializationContext(this.environment.SerializationManager) { StreamWriter = writer }; knowsException.Serialize(expected, context, null); // Deep copies should be reference-equal. var copyContext = new SerializationContext(this.environment.SerializationManager); Assert.Equal(expected, knowsException.DeepCopy(expected, copyContext), ReferenceEqualsComparer.Instance); // Create a deserializer which doesn't know about the expected exception type. var reader = new DeserializationContext(this.environment.SerializationManager) { StreamReader = new BinaryTokenStreamReader(writer.ToByteArray()) }; // Ensure that the deserialized object has the fallback type. var doesNotKnowException = new ILBasedExceptionSerializer(this.serializerGenerator, new TestTypeSerializer(new CachedTypeResolver())); var untypedActual = doesNotKnowException.Deserialize(null, reader); Assert.IsType <RemoteNonDeserializableException>(untypedActual); // Ensure that the original type name is preserved correctly. var actualDeserialized = (RemoteNonDeserializableException)untypedActual; Assert.Equal(RuntimeTypeNameFormatter.Format(typeof(ILExceptionSerializerTestException)), actualDeserialized.OriginalTypeName); // Re-serialize the deserialized object using the serializer which does not have access to the original type. writer = new BinaryTokenStreamWriter(); context = new SerializationContext(this.environment.SerializationManager) { StreamWriter = writer }; doesNotKnowException.Serialize(untypedActual, context, null); reader = new DeserializationContext(this.environment.SerializationManager) { StreamReader = new BinaryTokenStreamReader(writer.ToByteArray()) }; // Deserialize the round-tripped object and verify that it has the original type and all properties are // correctly. untypedActual = knowsException.Deserialize(null, reader); Assert.IsType <ILExceptionSerializerTestException>(untypedActual); var actual = (ILExceptionSerializerTestException)untypedActual; Assert.Equal(expected.BaseField.Value, actual.BaseField.Value, StringComparer.Ordinal); Assert.Equal(expected.SubClassField, actual.SubClassField, StringComparer.Ordinal); Assert.Equal(expected.OtherField.Value, actual.OtherField.Value, StringComparer.Ordinal); // Check for referential equality in the two fields which happened to be reference-equals. Assert.Equal(actual.BaseField, actual.OtherField, ReferenceEqualsComparer.Instance); }