public void GetSerializedMessageReturnsCachedSerializationIfAvailable() { var invocation = new InvocationMessage("Foo", new object[0]); var message = new SerializedHubMessage(invocation); var protocol = new DummyHubProtocol("p1"); // This should cache it _ = message.GetSerializedMessage(protocol); // Get it again var serialized = message.GetSerializedMessage(protocol); Assert.Equal(DummyHubProtocol.DummySerialization, serialized.ToArray()); // We should still only have written one message Assert.Collection(protocol.GetWrittenMessages(), actualMessage => Assert.Same(invocation, actualMessage)); }
public void GetSerializedMessageSerializesUsingHubProtocolIfNoCacheAvailable() { var invocation = new InvocationMessage("Foo", new object[0]); var message = new SerializedHubMessage(invocation); var protocol = new DummyHubProtocol("p1"); var serialized = message.GetSerializedMessage(protocol); Assert.Equal(DummyHubProtocol.DummySerialization, serialized.ToArray()); Assert.Collection(protocol.GetWrittenMessages(), actualMessage => Assert.Same(invocation, actualMessage)); }
public async Task SerializingTwoMessagesFromTheSameProtocolSimultaneouslyResultsInOneCachedItemAsync(int numberOfSerializationsToPreCache) { var invocation = new InvocationMessage("Foo", new object[0]); var message = new SerializedHubMessage(invocation); // "Pre-cache" the requested number of serializations (so we can test scenarios involving each of the fields and the fallback list) for (var i = 0; i < numberOfSerializationsToPreCache; i++) { _ = message.GetSerializedMessage(new DummyHubProtocol($"p{i}")); } var onWrite = SyncPoint.Create(2, out var syncPoints); var protocol = new DummyHubProtocol("test", () => onWrite().Wait()); // Serialize once, but hold at the Hub Protocol var firstSerialization = Task.Run(() => message.GetSerializedMessage(protocol)); await syncPoints[0].WaitForSyncPoint(); // Serialize again, which should hit the lock var secondSerialization = Task.Run(() => message.GetSerializedMessage(protocol)); Assert.False(secondSerialization.IsCompleted); // Release both instances of the syncpoint syncPoints[0].Continue(); syncPoints[1].Continue(); // Everything should finish and only one serialization should be written await firstSerialization.DefaultTimeout(); await secondSerialization.DefaultTimeout(); Assert.Collection(message.GetAllSerializations().Skip(numberOfSerializationsToPreCache).ToArray(), serializedMessage => { Assert.Equal("test", serializedMessage.ProtocolName); Assert.Equal(DummyHubProtocol.DummySerialization, serializedMessage.Serialized.ToArray()); }); }
public static IReadOnlyDictionary <string, byte[]> ToProtocolDictionary(this IEnumerable <IHubProtocol> protocols, string methodName, object[] args) { var serializedMessageHub = new SerializedHubMessage(new InvocationMessage(methodName, args)); var messages = new Dictionary <string, byte[]>(); foreach (var protocol in protocols) { ReadOnlyMemory <byte> serialized = serializedMessageHub.GetSerializedMessage(protocol); messages.Add(protocol.Name, serialized.ToArray()); } return(messages); }
private static void WriteSerializedHubMessage(Stream stream, SerializedHubMessage message) { // Written as a MessagePack 'map' where the keys are the name of the protocol (as a MessagePack 'str') // and the values are the serialized blob (as a MessagePack 'bin'). var protocol = new JsonHubProtocol(); MessagePackBinary.WriteMapHeader(stream, 1); MessagePackBinary.WriteString(stream, protocol.Name); var serialized = message.GetSerializedMessage(protocol); MemoryMarshal.TryGetArray(serialized, out var array); MessagePackBinary.WriteBytes(stream, array.Array, array.Offset, array.Count); }
private void WriteSerializedHubMessage(Stream stream, SerializedHubMessage message) { // Written as a MessagePack 'map' where the keys are the name of the protocol (as a MessagePack 'str') // and the values are the serialized blob (as a MessagePack 'bin'). MessagePackBinary.WriteMapHeader(stream, this.protocols.Count); foreach (var protocol in this.protocols) { MessagePackBinary.WriteString(stream, protocol.Name); var serialized = message.GetSerializedMessage(protocol); var isArray = MemoryMarshal.TryGetArray(serialized, out var array); Debug.Assert(isArray, "should be array"); MessagePackBinary.WriteBytes(stream, array.Array, array.Offset, array.Count); } }