コード例 #1
0
    public async Task ShouldDisposeOfSemaphoreSlim(SemaphoreSlimLock sut)
    {
        sut.Dispose();

        await Assert.ThrowsAsync <ObjectDisposedException>(
            () => GetSemaphore(sut).WaitAsync());
    }
コード例 #2
0
    public async Task ShouldWaitAndRelease(SemaphoreSlimLock sut)
    {
        var mutex = GetSemaphore(sut);

        Assert.Equal(1, mutex.CurrentCount);

        await sut.WaitAsync(Cts.Token);

        Assert.Equal(0, mutex.CurrentCount);

        var second = sut.WaitAsync(Cts.Token);

        await Wait();

        Assert.False(second.IsCompleted);
        Assert.Equal(0, mutex.CurrentCount);

        await sut.ReleaseAsync(Cts.Token);

        await second;

        Assert.True(second.IsCompletedSuccessfully);
        Assert.Equal(0, mutex.CurrentCount);

        await sut.ReleaseAsync(Cts.Token);

        Assert.Equal(1, mutex.CurrentCount);

        await AssertThrowsAsync <SemaphoreFullException>(
            () => sut.ReleaseAsync(Cts.Token));
    }
コード例 #3
0
    private async Task HandleAsync(TcpClient tcpClient)
    {
        string connectionDetails;

        try
        {
            connectionDetails = tcpClient.Client.RemoteEndPoint?.ToString() ?? "No details";
        }
        catch (Exception exception)
        {
            connectionDetails = "Failed to get details";
            _logger.LogError(exception, "Failed to get connection details.");
        }

        try
        {
            using var stream      = tcpClient.GetStream();
            using var sendLock    = new SemaphoreSlimLock();
            using var receiveLock = new SemaphoreSlimLock();
            var connection = _protobufConnectionFactory.CreateProtobufConnection(stream)
                             .WithLocking(sendLock, receiveLock);

            var task = _connectionHandler
                       .HandleAsync(connection, _cts.Token)
                       .HandleCancellationAsync(exception =>
            {
                _logger.LogDebug(
                    exception,
                    "Cancellation request received for client: {ConnectionDetails}",
                    connectionDetails);
            })
                       .HandleExceptionAsync <Exception>(exception =>
            {
                _logger.LogError(
                    exception,
                    "Error happened while handling TCP connection: {ConnectionDetails}",
                    connectionDetails);
            });

            _connectionProcessors.Add(task);
            _connectionProcessors.RemoveAll(t => t.IsCompleted);

            await task.ConfigureAwait(false);
        }
#pragma warning disable CA1031 // This method should not throw ANY exceptions, it is a top-level handler.
        catch (Exception exception)
#pragma warning restore CA1031
        {
            _logger.LogError(
                exception,
                "Error happened while creating TCP connection: {ConnectionDetails}",
                connectionDetails);
        }
        finally
        {
            tcpClient.Dispose();
        }
    }
コード例 #4
0
    public async Task ReleaseAsync_ShouldThrowIfDisposed(SemaphoreSlimLock sut)
    {
        sut.Dispose();

        var exception = await AssertThrowsAsync <ObjectDisposedException>(
            () => sut.ReleaseAsync(Cts.Token));

        Assert.Equal(typeof(SemaphoreSlimLock).FullName, exception.ObjectName);
    }
コード例 #5
0
    public async Task ShouldCancelWaiting(SemaphoreSlimLock sut)
    {
        var mutex = GetSemaphore(sut);
        await sut.WaitAsync(Cts.Token);

        var second = sut.WaitAsync(Cts.Token);

        await Wait();

        Assert.False(second.IsCompleted);
        Assert.Equal(0, mutex.CurrentCount);

        Cts.Cancel();
        await AssertThrowsAsync <OperationCanceledException>(
            () => second);

        Assert.True(second.IsCanceled);
        Assert.Equal(0, mutex.CurrentCount);
    }
コード例 #6
0
    public async ValueTask <ConnectionWithDisconnect> ConnectAsync(CancellationToken cancellationToken)
    {
        var client = new TcpClient();
        await client.ConnectAsync(_host, _port, cancellationToken).ConfigureAwait(false);

        var stream      = client.GetStream();
        var sendLock    = new SemaphoreSlimLock();
        var receiveLock = new SemaphoreSlimLock();
        var connection  = _factory.CreateProtobufConnection(stream)
                          .WithLocking(sendLock, receiveLock);

        return(new ConnectionWithDisconnect(connection, async() =>
        {
            receiveLock.Dispose();
            sendLock.Dispose();
            stream.Close();

            await stream.DisposeAsync().ConfigureAwait(false);
            client.Dispose();
        }));
    }
コード例 #7
0
 public void ShouldBeSyncManagedDisposable(SemaphoreSlimLock sut)
 {
     Assert.IsAssignableFrom <SyncManagedDisposable>(sut);
 }
コード例 #8
0
 private static SemaphoreSlim GetSemaphore(SemaphoreSlimLock sut)
 {
     return((SemaphoreSlim)GetPrivateField(sut, "_mutex") !);
 }