コード例 #1
0
    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));
    }
コード例 #2
0
    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));
    }
コード例 #3
0
    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());
        });
    }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
            }
        }