コード例 #1
0
        public async Task ReplayMethodHappyPath2()
        {
            Func <IReceiverMessage, Task> capturedCallback = null;

            Action <DateTime, DateTime?, Func <IReceiverMessage, Task>, string, string, bool, AutoOffsetReset> mockReplayEngineCallback =
                (start, end, callback, topic, bootstrapServers, enableAutoOffsetStore, autoOffsetReset) =>
            {
                capturedCallback = callback;
            };

            var mockReplayEngine = new Mock <IReplayEngine>();

            mockReplayEngine.Setup(m => m.Replay(It.IsAny <DateTime>(), It.IsAny <DateTime?>(), It.IsAny <Func <IReceiverMessage, Task> >(),
                                                 It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <AutoOffsetReset>()))
            .Returns(Task.CompletedTask)
            .Callback(mockReplayEngineCallback);

            var mockMessageHandler = new Mock <IMessageHandler>();

            mockMessageHandler.Setup(m => m.OnMessageReceivedAsync(It.IsAny <IReceiver>(), It.IsAny <IReceiverMessage>()))
            .Returns(Task.CompletedTask);

            var receiver =
                new KafkaReceiver("name", "one_topic", "groupId", "servers", true, AutoOffsetReset.Earliest, mockReplayEngine.Object);

            receiver.MessageHandler = mockMessageHandler.Object;

            var expectedStart = new DateTime(2020, 9, 3, 20, 22, 58, DateTimeKind.Local).ToUniversalTime();
            var expectedEnd   = new DateTime(2020, 9, 3, 20, 23, 19, DateTimeKind.Local).ToUniversalTime();

            await receiver.Replay(expectedStart, expectedEnd, null);

            mockReplayEngine.Verify(m =>
                                    m.Replay(expectedStart, expectedEnd, It.IsAny <Func <IReceiverMessage, Task> >(), "one_topic", "servers", true, AutoOffsetReset.Earliest),
                                    Times.Once());

            capturedCallback.Should().NotBeNull();

            var message = new FakeReceiverMessage("Hello, world!");

            await capturedCallback(message);

            mockMessageHandler.Verify(m => m.OnMessageReceivedAsync(receiver, message), Times.Once());
        }
コード例 #2
0
        public async Task ReplayMethodHappyPath1()
        {
            var mockReplayEngine = new Mock <IReplayEngine>();

            mockReplayEngine.Setup(m => m.Replay(It.IsAny <DateTime>(), It.IsAny <DateTime?>(), It.IsAny <Func <IReceiverMessage, Task> >(),
                                                 It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <AutoOffsetReset>()))
            .Returns(Task.CompletedTask);

            var receiver =
                new KafkaReceiver("name", "one_topic", "groupId", "servers", true, AutoOffsetReset.Earliest, mockReplayEngine.Object);

            var start = new DateTime(2020, 9, 3, 20, 22, 58, DateTimeKind.Local).ToUniversalTime();
            var end   = new DateTime(2020, 9, 3, 20, 23, 19, DateTimeKind.Local).ToUniversalTime();
            Func <IReceiverMessage, Task> callback = message => Task.CompletedTask;

            await receiver.Replay(start, end, callback);

            mockReplayEngine.Verify(m =>
                                    m.Replay(start, end, callback, "one_topic", "servers", true, AutoOffsetReset.Earliest),
                                    Times.Once());
        }
コード例 #3
0
        public async Task ReplayMethodSadPath()
        {
            var mockReplayEngine = new Mock <IReplayEngine>();

            mockReplayEngine.Setup(m => m.Replay(It.IsAny <DateTime>(), It.IsAny <DateTime?>(), It.IsAny <Func <IReceiverMessage, Task> >(),
                                                 It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <AutoOffsetReset>()))
            .Returns(Task.CompletedTask);

            var receiver =
                new KafkaReceiver("name", "one_topic", "groupId", "servers", true, AutoOffsetReset.Earliest, mockReplayEngine.Object);

            var start = new DateTime(2020, 9, 3, 20, 22, 58, DateTimeKind.Local).ToUniversalTime();
            var end   = new DateTime(2020, 9, 3, 20, 23, 19, DateTimeKind.Local).ToUniversalTime();

            Func <Task> act = async() => await receiver.Replay(start, end, null);

            (await act.Should().ThrowExactlyAsync <InvalidOperationException>())
            .WithMessage("Replay cannot be called with a null 'callback' parameter before the receiver has been started.");

            mockReplayEngine.Verify(m =>
                                    m.Replay(It.IsAny <DateTime>(), It.IsAny <DateTime?>(), It.IsAny <Func <IReceiverMessage, Task> >(),
                                             It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <AutoOffsetReset>()),
                                    Times.Never());
        }