Exemplo n.º 1
0
        public async Task TestExecuteNonQueryAlreadyCanceled(
            [Values] bool isAsync,
            [Values] bool isSystemDataSqlClient,
            [Values] bool isFastQuery)
        {
            using var cancellationTokenSource = new CancellationTokenSource();
            cancellationTokenSource.Cancel();

            await using var connection = CreateConnection(isSystemDataSqlClient);
            await connection.OpenAsync(CancellationToken.None);

            using var command = connection.CreateCommand();
            command.SetCommandText(
                isFastQuery
                    ? "SELECT 1"
                    : @"WHILE 1 = 1
                        BEGIN
                            DECLARE @x INT = 1
                        END"
                );

            if (isAsync)
            {
                Assert.CatchAsync <OperationCanceledException>(() => command.ExecuteNonQueryAsync(cancellationTokenSource.Token).AsTask());
            }
            else
            {
                Assert.Catch <OperationCanceledException>(() => SyncViaAsync.Run(_ => command.ExecuteNonQueryAsync(cancellationTokenSource.Token), 0));
            }
        }
Exemplo n.º 2
0
        public async Task TestExecuteNonQueryCanCancel([Values] bool isAsync, [Values] bool isSystemDataSqlClient)
        {
            using var cancellationTokenSource = new CancellationTokenSource();

            await using var connection = CreateConnection(isSystemDataSqlClient);
            await connection.OpenAsync(CancellationToken.None);

            using var command = connection.CreateCommand();
            command.SetCommandText(@"
                WHILE 1 = 1
                BEGIN
                    DECLARE @x INT = 1
                END"
                                   );

            var task = Task.Run(async() =>
            {
                if (isAsync)
                {
                    await command.ExecuteNonQueryAsync(cancellationTokenSource.Token, disallowAsyncCancellation: true);
                }
                else
                {
                    SyncViaAsync.Run(_ => command.ExecuteNonQueryAsync(cancellationTokenSource.Token), 0);
                }
            });

            Assert.IsFalse(task.Wait(TimeSpan.FromSeconds(.1)));

            cancellationTokenSource.Cancel();
            Assert.IsTrue(task.ContinueWith(_ => { }).Wait(TimeSpan.FromSeconds(5)));
            task.Status.ShouldEqual(TaskStatus.Canceled);
        }
Exemplo n.º 3
0
        public async Task TestLockOnDifferentBlobClientTypes(
            [Values] BlobClientType type,
            [Values] bool isAsync)
        {
            if (isAsync)
            {
                await TestAsync();
            }
            else
            {
                SyncViaAsync.Run(_ => TestAsync(), default(object));
            }

            async ValueTask TestAsync()
            {
                using var provider = new TestingAzureBlobLeaseDistributedLockProvider();
                var name   = provider.GetUniqueSafeName();
                var client = CreateClient(type, name);

                if (client is AppendBlobClient appendClient)
                {
                    Assert.That(
                        Assert.Throws <RequestFailedException>(() => appendClient.CreateIfNotExists()).ToString(),
                        Does.Contain("This feature is not currently supported by the Storage Emulator")
                        );
                    return;
                }
                if (client.GetType() == typeof(BlobBaseClient))
                {
                    // work around inability to do CreateIfNotExists for the base client
                    new BlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, name).Upload(Stream.Null);
                }

                var @lock = new AzureBlobLeaseDistributedLock(client);

                await using var handle = await @lock.TryAcquireAsync();

                Assert.IsNotNull(handle);
                await using var nestedHandle = await @lock.TryAcquireAsync();

                Assert.IsNull(nestedHandle);
            }
        }