Пример #1
0
    public void ShouldReturnMessageSynchronouslyIfAlreadyReceived(
        [Frozen] Notificator notificator,
        NotificatorConnection sut)
    {
        notificator.NotifyReceived(Create <object>());
        Assert.True(sut.ReceiveAsync(Cts.Token).IsCompletedSuccessfully);

        var message = Create <object>();

        notificator.NotifyReceived(message);
        Assert.Equal(message, sut.ReceiveAsync(Cts.Token).Result);
    }
Пример #2
0
    public void ShouldPushMessagesToBuffer_WhenNoSubscription()
    {
        var messages = Fixture.CreateMany <object>(2).ToList();

        var sut = new Notificator();

        sut.NotifyReceived(messages[0]);
        sut.NotifyReceived(messages[1]);

        Assert.Equal(2, sut.ReceivedMessagesBuffer.Count);
        Assert.Contains(messages[0], sut.ReceivedMessagesBuffer);
        Assert.Contains(messages[1], sut.ReceivedMessagesBuffer);
    }
        public async Task ShouldNotUseUnderlyingConnectionToWaitForMessages(
            [Frozen] Mock <IConnection> connection,
            [Frozen] Notificator notificator,
            NotificatorConnection sut)
        {
            notificator.NotifyReceived(Create <object>());
            await sut.ReceiveAsync(Cts.Token);

            connection.Verify(x => x.ReceiveAsync(Cts.Token), Times.Never);
        }
Пример #4
0
    public void ShouldFirstPushAndThenNotify()
    {
        var    message = Create <object>();
        object?result  = null;

        var sut = new Notificator();

        sut.Received += () => sut.ReceivedMessagesBuffer.TryDequeue(out result);
        sut.NotifyReceived(message);

        Assert.Equal(message, result);
    }
Пример #5
0
    public async Task ShouldReturnMessageWhenNotified(
        [Frozen] Notificator notificator,
        TestMessage message,
        NotificatorConnection sut)
    {
        var result = sut.ReceiveAsync(Cts.Token);

        await Wait();

        Assert.False(result.IsCompleted);

        notificator.NotifyReceived(message);
        var received = await result;

        Assert.Equal(message, received);
    }
Пример #6
0
    public void ShouldPushMessagesToBufferAndNotify_WhenSubscribed()
    {
        var message     = Create <object>();
        var isNotified1 = false;
        var isNotified2 = false;

        var sut = new Notificator();

        sut.Received += () => isNotified1 = true;
        sut.Received += () => isNotified2 = true;
        sut.NotifyReceived(message);

        Assert.Single(sut.ReceivedMessagesBuffer);
        Assert.Contains(message, sut.ReceivedMessagesBuffer);
        Assert.True(isNotified1);
        Assert.True(isNotified2);
    }
Пример #7
0
    public async Task ShouldWorkInHighConcurrencyScenario(
        [Frozen] Notificator notificator,
        NotificatorConnection sut)
    {
        var notifyTask = Task.WhenAll(Enumerable.Range(0, 1000).Select(_ => Task.Run(async() =>
        {
            await Wait(RandomNumberGenerator.GetInt32(100, 150));
            notificator.NotifyReceived("message");
        })));

        var processingTask = Task.Run(async() =>
        {
            for (var i = 0; i < 1000; i++)
            {
                Assert.Equal("message", await sut.ReceiveAsync(Cts.Token));
            }
        });

        await Task.WhenAll(notifyTask, processingTask);
    }
Пример #8
0
    public async Task ShouldUnsubscribeAfterNotification(
        [Frozen] Notificator notificator,
        NotificatorConnection sut)
    {
        int GetSubscriptionCount()
        {
            return((typeof(Notificator)
                    .GetField(nameof(Notificator.Received), BindingFlags.Instance | BindingFlags.NonPublic)?
                    .GetValue(notificator) as Action)?.GetInvocationList()?.Length ?? 0);
        }

        Assert.Equal(0, GetSubscriptionCount());

        var result = sut.ReceiveAsync(Cts.Token);

        await Wait();

        Assert.Equal(1, GetSubscriptionCount());

        notificator.NotifyReceived(Create <object>());
        await result;

        Assert.Equal(0, GetSubscriptionCount());
    }
Пример #9
0
 private ClientToServerSignalRMessageSender(HubConnection hub, Notificator notificator)
 {
     _hub = hub;
     _hub.On <MessageData>(SignalRConstants.Send, message => notificator.NotifyReceived(message));
 }