Exemplo n.º 1
0
        public void IsClientConnected_ShouldBeConnected_WhenClientExists(
            ConnectedClient client,
            ConnectedClientStore sut)
        {
            sut.Add(client);

            Assert.True(sut.IsClientConnected(client.ClientId));
        }
Exemplo n.º 2
0
 public void Add_ShouldMarkForUpdate_WhenAdded(
     [Frozen] Mock <IUpdateDetector> updateDetector,
     ConnectedClient client,
     ConnectedClientStore sut)
 {
     sut.Add(client);
     updateDetector.Verify(x => x.MarkForUpdate(client.Group));
 }
    public void Find_ShouldFind_WhenAdded(
        ConnectedClient client,
        ConnectedClientStore sut)
    {
        sut.Add(client);
        var found = sut.Find(client.ClientId);

        Assert.Equal(client, found);
    }
    public void Add_ShouldThrow_WhenAlreadyAdded(ConnectedClientStore sut)
    {
        Fixture.Customizations.Add(
            new ClientWithIdBuilder(Create <string>()));

        sut.Add(Create <ConnectedClient>());

        Assert.Throws <ClientAlreadyConnectedException>(
            () => sut.Add(Create <ConnectedClient>()));
    }
 public void Add_ShouldMarkForUpdateMultipleGroups_WhenAdded(
     [Frozen] Mock <IUpdateDetector> updateDetector,
     ConnectedClient client,
     ConnectedClientStore sut)
 {
     sut.Add(client);
     updateDetector.Verify(x => x.MarkForUpdate(It.Is <IEnumerable <string> >(
                                                    x => x.Count() == 3 &&
                                                    x.SequenceEqual(client.Groups))));
 }
    public void Remove_ShouldRemoveClient(
        ConnectedClient client,
        ConnectedClientStore sut)
    {
        sut.Add(client);
        Assert.Equal(client, sut.Find(client.ClientId));

        sut.Remove(client.ClientId);
        Assert.Null(sut.Find(client.ClientId));
    }
 public void Add_ShouldMarkForUpdate_WhenAdded(
     [Frozen] Mock <IUpdateDetector> updateDetector,
     ConnectedClient client,
     ConnectedClientStore sut)
 {
     sut.Add(client);
     updateDetector.Verify(x => x.MarkForUpdate(It.Is <IEnumerable <string> >(
                                                    x => x.Count() == 1 &&
                                                    x.First() == client.Group)));
 }
    public void Add_ShouldAddClient_WhenNotAdded(
        ConnectedClient client,
        ConnectedClientStore sut)
    {
        sut.Add(client);

        var result = sut.Find(client.ClientId);

        Assert.NotNull(result);
        Assert.Equal(client, result);
    }
Exemplo n.º 9
0
        public void Remove_ShouldMarkForUpdate_WhenRemoved(
            [Frozen] Mock <IUpdateDetector> updateDetector,
            ConnectedClient client,
            ConnectedClientStore sut)
        {
            sut.Add(client);
            updateDetector.Verify(x => x.MarkForUpdate(client.Group), Times.Once);

            sut.Remove(client.ClientId);
            updateDetector.Verify(x => x.MarkForUpdate(client.Group), Times.Exactly(2));
        }
    public void Remove_ShouldMarkForUpdate_WhenRemoved(
        [Frozen] Mock <IUpdateDetector> updateDetector,
        ConnectedClient client,
        ConnectedClientStore sut)
    {
        sut.Add(client);
        updateDetector.Verify(x => x.MarkForUpdate(It.Is <IEnumerable <string> >(
                                                       x => x.Count() == 1 && x.First() == client.Group)), Times.Once);

        sut.Remove(client.ClientId);
        updateDetector.Verify(x => x.MarkForUpdate(It.Is <IEnumerable <string> >(
                                                       x => x.Count() == 1 && x.First() == client.Group)), Times.Exactly(2));
    }
    public void Add_ShouldMarkForUpdateMultipleGroups_WhenAddedOneMoreGroup(
        [Frozen] Mock <IUpdateDetector> updateDetector,
        ConnectedClient client,
        string group,
        ConnectedClientStore sut)
    {
        // Possibly improve this unit test.
        client.AddToGroup(group);

        sut.Add(client);
        updateDetector.Verify(x => x.MarkForUpdate(It.Is <IEnumerable <string> >(
                                                       x => x.Count() == 4 &&
                                                       x.SequenceEqual(client.Groups) &&
                                                       x.Contains(group))));
    }
    public void Remove_ShouldMarkForUpdateMultiGroupClient_WhenRemoved(
        [Frozen] Mock <IUpdateDetector> updateDetector,
        string[] groups,
        ConnectedClientStore sut)
    {
        var client = CreateMultiGroupClient(groups);

        sut.Add(client);
        updateDetector.Verify(x => x.MarkForUpdate(It.Is <IEnumerable <string> >(
                                                       x => x.SequenceEqual(groups))), Times.Once);

        sut.Remove(client.ClientId);
        updateDetector.Verify(x => x.MarkForUpdate(It.Is <IEnumerable <string> >(
                                                       x => x.SequenceEqual(groups))), Times.Exactly(2));
    }
    public void ShouldFindAllClientsInGroups(FindType findType, ConnectedClientStore sut)
    {
        var group1 = Fixture.Create <string>();
        var group2 = Fixture.Create <string>();
        var group3 = Fixture.Create <string>();

        var clientsInGroup1 = Fixture.Build <ConnectedClient>()
                              .With(x => x.Group, group1)
                              .CreateMany(2);

        var clientsInGroup2 = Fixture.Build <ConnectedClient>()
                              .With(x => x.Group, group2)
                              .CreateMany(2);

        var clientsInGroup3 = Fixture.Build <ConnectedClient>()
                              .With(x => x.Group, group3)
                              .CreateMany(2);

        var clientInGroups1And2 = CreateMultiGroupClient(new[] { group1, group2 });
        var clientInGroups123   = CreateMultiGroupClient(new[] { group1, group2 });

        Assert.Throws <InvalidOperationException>(() => clientInGroups123.Group = group3);
        clientInGroups123.AddToGroup(group3);

        foreach (var client in clientsInGroup1
                 .Concat(clientsInGroup2)
                 .Concat(clientsInGroup3)
                 .Append(clientInGroups1And2)
                 .Append(clientInGroups123))
        {
            sut.Add(client);
        }

        var result = findType switch
        {
            FindType.RegularMethod => sut.FindInGroups(new[] { group1, group3 }),
            FindType.ExtensionParamsMethod => sut.FindInGroups(group1, group3),
            _ => throw new InvalidOperationException()
        };

        Assert.Equal(6, result.Count());
        Assert.True(clientsInGroup1.All(x => result.Contains(x)));
        Assert.True(clientsInGroup2.All(x => !result.Contains(x)));
        Assert.True(clientsInGroup3.All(x => result.Contains(x)));
        Assert.Contains(clientInGroups1And2, result);
        Assert.Contains(clientInGroups123, result);
    }
    public async Task ConcurrencyTest(
        string clientId,
        ConnectedClientStore sut)
    {
        Fixture.Customizations.Add(new ClientWithIdBuilder(clientId));

        var tasks = new List <Task>();

        for (var i = 0; i < 100; i++)
        {
            tasks.Add(new Task(() => sut.Add(Create <ConnectedClient>())));
            tasks.Add(new Task(() => sut.Remove(clientId)));
            tasks.Add(new Task(() => sut.Find(clientId)));
        }

        Parallel.ForEach(tasks, task => task.Start());
        try { await Task.WhenAll(tasks); } catch (ClientAlreadyConnectedException) { }
        try { sut.Add(Create <ConnectedClient>()); } catch (ClientAlreadyConnectedException) { }
        Assert.NotNull(sut.Find(clientId));
    }
    public async Task SendAsync_ShouldSendMessageToAllClientsThatAreInSpecifiedGroup(ConnectedClientStore sut)
    {
        var connections = Fixture.CreateMany <IConnection>(3).ToList();

        var clients = connections
                      .Select(connection => CreateSingleGroupClient(connection))
                      .ToList();

        foreach (var client in clients)
        {
            sut.Add(client);
        }
        clients[1].Group = clients[0].Group;

        Mock.Get(connections[0]).Setup(x => x.SendAsync(It.IsAny <object>(), It.IsAny <CancellationToken>()))
        .Callback(() => { });

        var message = Create <TestMessage>();
        await sut.SendAsync(message, clients[0].Group, Cts.Token);

        Mock.Get(connections[0]).Verify(x => x.SendAsync(message, Cts.Token));
        Mock.Get(connections[1]).Verify(x => x.SendAsync(message, Cts.Token));
        Mock.Get(connections[2]).Verify(x => x.SendAsync(message, Cts.Token), Times.Never);
    }
 public void Find_ShouldReturnNull_WhenNotAdded(ConnectedClientStore sut)
 {
     Assert.Null(sut.Find(Create <string>()));
 }
 public void ShouldNotThrow_WhenRemovingNonExistingClient(ConnectedClientStore sut)
 {
     sut.Remove(Create <string>());
 }
Exemplo n.º 18
0
        public async Task SendAsync_ShouldSendMessageToAllClientsThatAreInSpecifiedGroup(ConnectedClientStore sut)
        {
            var connections = Fixture.CreateMany <IConnection>(3).ToList();
            var clients     = connections
                              .Select(connection => Create <ConnectedClient>(
                                          new ClientWithConnectionBuilder(connection)))
                              .ToList();

            foreach (var client in clients)
            {
                sut.Add(client);
            }
            clients[1].Group = clients[0].Group;

            var message = Create <TestMessage>();
            await sut.SendAsync(message, clients[0].Group, Cts.Token);

            Mock.Get(connections[0]).Verify(x => x.SendAsync(message, Cts.Token));
            Mock.Get(connections[1]).Verify(x => x.SendAsync(message, Cts.Token));
            Mock.Get(connections[2]).Verify(x => x.SendAsync(message, Cts.Token), Times.Never);
        }
Exemplo n.º 19
0
 public void IsClientConnected_ShouldNotBeConnected_WhenClientDoesNotExist(ConnectedClientStore sut)
 {
     Assert.False(sut.IsClientConnected(Create <string>()));
 }