public void GetMetadataOrEmpty_ShouldGetMetadata_WhenObjectHasMetadata(
        MessageMetadata metadata,
        MessageWithMetadata message)
    {
        message.Metadata = metadata;

        var result = message.GetMetadataOrEmpty();

        Assert.Equal(metadata, result);
    }
    public void GetMetadataOrEmpty_ShouldGetDefaultMetadata_WhenObjectDoesNotHaveMetadata(
        MessageWithMetadata message)
    {
        message.Metadata = null !;

        var metadata = message.GetMetadataOrEmpty();

        Assert.Equal(AcknowledgementType.Handled, metadata.AcknowledgementType);
        Assert.Null(metadata.MessageId);
    }
    public void ClientToServer_ShouldBeSerializable(
        string message,
        ClientToServerMessageMetadata metadata)
    {
        AssertSerializable <MessageWithMetadata>();

        var sut = new MessageWithMetadata(message, metadata);

        Assert.Equal(message, sut.Message);
        Assert.Equal(metadata, sut.Metadata);
    }
    public static ValueTask SendAsync(
        this IMessageSender messageSender,
        object message,
        MessageMetadata metadata,
        CancellationToken cancellationToken)
    {
        var messageWithMetadata = new MessageWithMetadata
        {
            Message  = message,
            Metadata = metadata
        };

        return(messageSender.SendAsync(messageWithMetadata, cancellationToken));
    }
    public async Task ShouldNotSendAcknowledgeReceived_WhenAcknowledgementTypeIsNotReceived(
        AcknowledgementType acknowledgementType,
        MessageWithMetadata message,
        [Frozen] Mock <IConnection> connection,
        ReceivedAcknowledgingConnection sut)
    {
        connection.Setup(x => x.ReceiveAsync(Cts.Token))
        .ReturnsAsync(message);

        message.Metadata !.AcknowledgementType = acknowledgementType;

        await sut.ReceiveAsync(Cts.Token);

        connection.Verify(x => x.SendAsync(It.IsAny <object>(), Cts.Token), Times.Never);
    }
Пример #6
0
    public async Task SendAsync_ShouldHaveMetadata_WhenMessageMetadataIsSet(
        MessageWithMetadata message, string serialized, string typeId)
    {
        _messageSerializer.Setup(x => x.Serialize(message.Message))
        .Returns(serialized);

        _messageTypeCache.Setup(x => x.GetTypeId(message.Message.GetType()))
        .Returns(typeId);

        await _sut.SendAsync(message, Cts.Token);

        _connection.Verify(x => x.SendAsync(It.Is <MessageData>(msg =>
                                                                msg.Data == serialized &&
                                                                msg.TypeId == typeId &&
                                                                msg.Metadata == message.Metadata), Cts.Token));
    }
        public async Task CannotDeserializeWithNullSchemaId()
        {
            var client    = CreateClient();
            var groupName = TestEnvironment.SchemaRegistryGroup;

            var encoder = new SchemaRegistryAvroEncoder(client, groupName, new SchemaRegistryAvroObjectEncoderOptions {
                AutoRegisterSchemas = true
            });
            var messageData = new MessageWithMetadata
            {
                Data        = new BinaryData(Array.Empty <byte>()),
                ContentType = null
            };

            Assert.ThrowsAsync <ArgumentNullException>(async() => await encoder.DecodeMessageDataAsync <TimeZoneInfo>(messageData));
            await Task.CompletedTask;
        }
    public async Task ShouldNotSendAcknowledgeReceived_WhenMessageHasId_AndAcknowledgementTypeIsReceived_ButMessageIsAcknowledgement(
        MessageWithMetadata message,
        [Frozen] Mock <IConnection> connection,
        ReceivedAcknowledgingConnection sut)
    {
        connection.Setup(x => x.ReceiveAsync(Cts.Token))
        .ReturnsAsync(message);

        message.Metadata !.AcknowledgementType = AcknowledgementType.Received;

        message.Message = new AcknowledgeReceived();
        await sut.ReceiveAsync(Cts.Token);

        message.Message = new AcknowledgeHandled();
        await sut.ReceiveAsync(Cts.Token);

        connection.Verify(x => x.SendAsync(It.IsAny <object>(), Cts.Token), Times.Never);
    }
    //[Theory, AutoMoqData]
    public async Task ShouldSendDisconnectedMessage_WhenDispatcherThrows(
        [Frozen] Mock <IConnectionInitializer> initializer,
        [Frozen] Mock <IMessageDispatcher> dispatcher,
        IConnection connection,
        MessageWithMetadata clientMessage,
        ConnectionHandler sut)
    {
        dispatcher.Setup(x => x.DispatchAsync(It.IsAny <ConnectedClient>(), It.IsAny <object>(), Cts.Token))
        .ThrowsAsync(Create <Exception>());

        Mock.Get(connection).Setup(x => x.ReceiveAsync(Cts.Token))
        .ReturnsAsync(clientMessage);

        SetupClientFromInitializer(initializer, connection, Create <ConnectedClient>(
                                       new ClientWithConnectionBuilder(connection)));

        await SwallowAnyAsync(sut.HandleAsync(connection, Cts.Token));

        Mock.Get(connection).Verify(x => x.SendAsync(It.IsAny <Disconnected>(), Cts.Token));
    }
    public async Task ShouldSendAcknowledgeReceived_WhenMessageHasId_AndAcknowledgementTypeIsReceived(
        MessageWithMetadata message,
        [Frozen] Mock <IConnection> connection,
        ReceivedAcknowledgingConnection sut)
    {
        connection.Setup(x => x.ReceiveAsync(Cts.Token))
        .ReturnsAsync(message);

        message.Metadata !.AcknowledgementType = AcknowledgementType.Received;

        await sut.ReceiveAsync(Cts.Token);

#pragma warning disable S2219 // Runtime type checking should be simplified
        connection.Verify(x => x.SendAsync(
                              It.Is <MessageWithMetadata>(
                                  y => y.Message.GetType() == typeof(AcknowledgeReceived) &&
                                  ((AcknowledgeReceived)y.Message).MessageId == message.Metadata.MessageId &&
                                  y.Metadata !.MessageId == message.Metadata.MessageId),
                              Cts.Token));
#pragma warning restore S2219
    }
        public async Task CanSerializeAndDeserialize()
        {
            var client    = CreateClient();
            var groupName = TestEnvironment.SchemaRegistryGroup;
            var employee  = new Employee {
                Age = 42, Name = "Caketown"
            };

            #region Snippet:SchemaRegistryAvroEncodeDecodeMessageWithMetadata
            var encoder = new SchemaRegistryAvroEncoder(client, groupName, new SchemaRegistryAvroObjectEncoderOptions()
            {
                AutoRegisterSchemas = true
            });
            MessageWithMetadata messageData = await encoder.EncodeMessageDataAsync <MessageWithMetadata>(employee, typeof(Employee));

            Employee decodedEmployee = await encoder.DecodeMessageDataAsync <Employee>(messageData);

            #endregion

            Assert.IsNotNull(decodedEmployee);
            Assert.AreEqual("Caketown", decodedEmployee.Name);
            Assert.AreEqual(42, decodedEmployee.Age);
        }