Skip to content

Commit

Permalink
Remove sync WaitForCompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
jalauzon-msft committed Jan 9, 2025
1 parent 6d25121 commit 96976ef
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 509 deletions.
16 changes: 3 additions & 13 deletions sdk/storage/Azure.Storage.DataMovement/src/TransferOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,7 @@ internal TransferOperation(
}

/// <summary>
/// Ensures completion of the TransferOperation and attempts to get result
/// </summary>
public void WaitForCompletion(CancellationToken cancellationToken = default)
{
#pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extension method instead.
WaitForCompletionAsync(cancellationToken).GetAwaiter().GetResult();
#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extension method instead.
}

/// <summary>
/// Waits until the data transfer itself has completed
/// Waits until the transfer has completed.
/// </summary>
/// <param name="cancellationToken"></param>
public async Task WaitForCompletionAsync(CancellationToken cancellationToken = default)
Expand All @@ -80,11 +70,11 @@ public async Task WaitForCompletionAsync(CancellationToken cancellationToken = d
}

/// <summary>
/// Attempts to pause the current Data Transfer.
/// Attempts to pause the current transfer.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>
/// Will return false if the data transfer has already been completed.
/// Will return false if the transfer has already been completed.
///
/// Will return true if the pause has taken place.
/// </returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,119 +832,6 @@ await TestTransferWithTimeout.WaitForCompletionAsync(
Assert.AreEqual(true, transfer.Status.HasSkippedItems);
}

[RecordedTest]
public async Task StartTransfer_EnsureCompleted()
{
// Arrange
await using IDisposingContainer<TSourceContainerClient> source = await GetSourceDisposingContainerAsync();
await using IDisposingContainer<TDestinationContainerClient> destination = await GetDestinationDisposingContainerAsync();

TransferOptions options = new TransferOptions();
TestEventsRaised testEventsRaised = new TestEventsRaised(options);

// Create transfer to do a EnsureCompleted
TransferOperation transfer = await CreateStartTransfer(
source.Container,
destination.Container,
concurrency: 1,
options: options);

// Act
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
TestTransferWithTimeout.WaitForCompletion(
transfer,
testEventsRaised,
cancellationTokenSource.Token);

// Assert
await testEventsRaised.AssertSingleCompletedCheck();
Assert.NotNull(transfer);
Assert.IsTrue(transfer.HasCompleted);
Assert.AreEqual(TransferState.Completed, transfer.Status.State);
}

[RecordedTest]
public async Task StartTransfer_EnsureCompleted_Failed()
{
// Arrange
await using IDisposingContainer<TSourceContainerClient> source = await GetSourceDisposingContainerAsync();
await using IDisposingContainer<TDestinationContainerClient> destination = await GetDestinationDisposingContainerAsync();

TransferOptions options = new TransferOptions()
{
CreationPreference = StorageResourceCreationPreference.FailIfExists
};
TestEventsRaised testEventsRaised = new TestEventsRaised(options);

// Create transfer to do a AwaitCompletion
TransferOperation transfer = await CreateStartTransfer(
source.Container,
destination.Container,
concurrency: 1,
createFailedCondition: true,
options: options);

// Act
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
TestTransferWithTimeout.WaitForCompletion(
transfer,
testEventsRaised,
cancellationTokenSource.Token);

// Assert
await testEventsRaised.AssertSingleFailedCheck(1);
Assert.NotNull(transfer);
Assert.IsTrue(transfer.HasCompleted);
Assert.AreEqual(TransferState.Completed, transfer.Status.State);
Assert.AreEqual(true, transfer.Status.HasFailedItems);
var testException = testEventsRaised.FailedEvents.First().Exception;
if (testException is RequestFailedException rfe)
{
Assert.That(rfe.ErrorCode, Does.Contain(_expectedOverwriteExceptionMessage));
}
else
{
Assert.IsTrue(testException.Message.Contains(_expectedOverwriteExceptionMessage));
}
}

[RecordedTest]
public async Task StartTransfer_EnsureCompleted_Skipped()
{
// Arrange
await using IDisposingContainer<TSourceContainerClient> source = await GetSourceDisposingContainerAsync();
await using IDisposingContainer<TDestinationContainerClient> destination = await GetDestinationDisposingContainerAsync();

// Create transfer options with Skipping available
TransferOptions options = new TransferOptions()
{
CreationPreference = StorageResourceCreationPreference.SkipIfExists
};
TestEventsRaised testEventsRaised = new TestEventsRaised(options);

// Create transfer to do a EnsureCompleted
TransferOperation transfer = await CreateStartTransfer(
source.Container,
destination.Container,
concurrency: 1,
createFailedCondition: true,
options: options);

// Act
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
TestTransferWithTimeout.WaitForCompletion(
transfer,
testEventsRaised,
cancellationTokenSource.Token);

// Assert
await testEventsRaised.AssertSingleSkippedCheck();
Assert.NotNull(transfer);
Assert.IsTrue(transfer.HasCompleted);
Assert.AreEqual(TransferState.Completed, transfer.Status.State);
Assert.AreEqual(true, transfer.Status.HasSkippedItems);
}

private async Task CopyRemoteObjects_VerifyProperties(
TSourceContainerClient sourceContainer,
TDestinationContainerClient destinationContainer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,115 +793,9 @@ await TestTransferWithTimeout.WaitForCompletionAsync(
await testEventsRaised.AssertContainerCompletedWithSkippedCheck(1);
}

[RecordedTest]
public async Task StartTransfer_EnsureCompleted()
{
// Arrange
await using IDisposingContainer<TSourceContainerClient> source = await GetSourceDisposingContainerAsync();
await using IDisposingContainer<TDestinationContainerClient> destination = await GetDestinationDisposingContainerAsync();

// Create transfer to do a EnsureCompleted
TransferOptions options = new TransferOptions();
TestEventsRaised testEventsRaised = new TestEventsRaised(options);

TransferOperation transfer = await CreateStartTransfer(
source.Container,
destination.Container,
1,
options: options);

// Act
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
TestTransferWithTimeout.WaitForCompletion(
transfer,
testEventsRaised,
cancellationTokenSource.Token);

// Assert
testEventsRaised.AssertUnexpectedFailureCheck();
Assert.NotNull(transfer);
Assert.IsTrue(transfer.HasCompleted);
Assert.AreEqual(TransferState.Completed, transfer.Status.State);
}

[Test]
[LiveOnly] // https://github.com/Azure/azure-sdk-for-net/issues/46717
public async Task StartTransfer_EnsureCompleted_Failed()
{
// Arrange
await using IDisposingContainer<TSourceContainerClient> source = await GetSourceDisposingContainerAsync();
await using IDisposingContainer<TDestinationContainerClient> destination = await GetDestinationDisposingContainerAsync();

TransferOptions options = new TransferOptions()
{
CreationPreference = StorageResourceCreationPreference.FailIfExists
};
TestEventsRaised testEventsRaised = new TestEventsRaised(options);

// Create transfer to do a AwaitCompletion
TransferOperation transfer = await CreateStartTransfer(
source.Container,
destination.Container,
1,
createFailedCondition: true,
options: options);

// Act
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
TestTransferWithTimeout.WaitForCompletion(
transfer,
testEventsRaised,
cancellationTokenSource.Token);

// Assert
Assert.NotNull(transfer);
Assert.IsTrue(transfer.HasCompleted);
Assert.AreEqual(TransferState.Completed, transfer.Status.State);
Assert.AreEqual(true, transfer.Status.HasFailedItems);
await testEventsRaised.AssertContainerCompletedWithFailedCheck(1);
Assert.IsTrue(testEventsRaised.FailedEvents.First().Exception.Message.Contains(_expectedOverwriteExceptionMessage));
}

[RecordedTest]
public async Task StartTransfer_EnsureCompleted_Skipped()
{
// Arrange
await using IDisposingContainer<TSourceContainerClient> source = await GetSourceDisposingContainerAsync();
await using IDisposingContainer<TDestinationContainerClient> destination = await GetDestinationDisposingContainerAsync();

// Create transfer options with Skipping available
TransferOptions options = new TransferOptions()
{
CreationPreference = StorageResourceCreationPreference.SkipIfExists
};
TestEventsRaised testEventsRaised = new TestEventsRaised(options);

// Create transfer to do a EnsureCompleted
TransferOperation transfer = await CreateStartTransfer(
source.Container,
destination.Container,
1,
createFailedCondition: true,
options: options);

// Act
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
TestTransferWithTimeout.WaitForCompletion(
transfer,
testEventsRaised,
cancellationTokenSource.Token);

// Assert
testEventsRaised.AssertUnexpectedFailureCheck();
Assert.NotNull(transfer);
Assert.IsTrue(transfer.HasCompleted);
Assert.AreEqual(TransferState.Completed, transfer.Status.State);
Assert.AreEqual(true, transfer.Status.HasSkippedItems);
}

[Test]
[LiveOnly] // https://github.com/Azure/azure-sdk-for-net/issues/46717
public async Task StartTransfer_EnsureCompleted_Failed_SmallChunks()
public async Task StartTransfer_AwaitCompletion_Failed_SmallChunks()
{
// Arrange
await using IDisposingContainer<TSourceContainerClient> source = await GetSourceDisposingContainerAsync();
Expand All @@ -926,7 +820,7 @@ public async Task StartTransfer_EnsureCompleted_Failed_SmallChunks()

// Act
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
TestTransferWithTimeout.WaitForCompletion(
await TestTransferWithTimeout.WaitForCompletionAsync(
transfer,
testEventsRaised,
cancellationTokenSource.Token);
Expand Down
Loading

0 comments on commit 96976ef

Please sign in to comment.