Skip to content

Commit 3cd04da

Browse files
authored
Fix for flakey Download 304 tests to check LastModified time from Upload (#33325)
1 parent 74b901c commit 3cd04da

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

sdk/storage/Azure.Storage.Blobs/tests/BlobBaseClientTests.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,11 +1547,14 @@ public async Task DownloadTo_Initial304()
15471547
// Upload a blob
15481548
var data = GetRandomBuffer(Constants.KB);
15491549
BlobClient blob = InstrumentClient(test.Container.GetBlobClient(GetNewBlobName()));
1550+
Response<BlobContentInfo> uploadResponse;
15501551
using (var stream = new MemoryStream(data))
15511552
{
1552-
await blob.UploadAsync(stream);
1553+
uploadResponse = await blob.UploadAsync(stream);
15531554
}
15541555

1556+
CheckModifiedSinceAndWait(uploadResponse);
1557+
15551558
// Add conditions to cause a failure and ensure we don't explode
15561559
Response result = await blob.DownloadToAsync(
15571560
Stream.Null,
@@ -1571,11 +1574,14 @@ public async Task DownloadContent_Initial304()
15711574
// Upload a blob
15721575
var data = GetRandomBuffer(Constants.KB);
15731576
BlobClient blob = InstrumentClient(test.Container.GetBlobClient(GetNewBlobName()));
1577+
Response<BlobContentInfo> uploadResponse;
15741578
using (var stream = new MemoryStream(data))
15751579
{
1576-
await blob.UploadAsync(stream);
1580+
uploadResponse = await blob.UploadAsync(stream);
15771581
}
15781582

1583+
CheckModifiedSinceAndWait(uploadResponse);
1584+
15791585
// Add conditions to cause a failure and ensure we don't explode
15801586
Response<BlobDownloadResult> result = await blob.DownloadContentAsync(new BlobDownloadOptions
15811587
{
@@ -1596,11 +1602,14 @@ public async Task DownloadStreaming_Initial304()
15961602
// Upload a blob
15971603
var data = GetRandomBuffer(Constants.KB);
15981604
BlobClient blob = InstrumentClient(test.Container.GetBlobClient(GetNewBlobName()));
1605+
Response<BlobContentInfo> uploadResponse;
15991606
using (var stream = new MemoryStream(data))
16001607
{
1601-
await blob.UploadAsync(stream);
1608+
uploadResponse = await blob.UploadAsync(stream);
16021609
}
16031610

1611+
CheckModifiedSinceAndWait(uploadResponse);
1612+
16041613
// Add conditions to cause a failure and ensure we don't explode
16051614
Response<BlobDownloadStreamingResult> result = await blob.DownloadStreamingAsync(new BlobDownloadOptions
16061615
{
@@ -1621,11 +1630,14 @@ public async Task Download_Initial304()
16211630
// Upload a blob
16221631
var data = GetRandomBuffer(Constants.KB);
16231632
BlobClient blob = InstrumentClient(test.Container.GetBlobClient(GetNewBlobName()));
1633+
Response<BlobContentInfo> uploadResponse;
16241634
using (var stream = new MemoryStream(data))
16251635
{
1626-
await blob.UploadAsync(stream);
1636+
uploadResponse = await blob.UploadAsync(stream);
16271637
}
16281638

1639+
CheckModifiedSinceAndWait(uploadResponse);
1640+
16291641
// Add conditions to cause a failure and ensure we don't explode
16301642
Response<BlobDownloadInfo> result = await blob.DownloadAsync(
16311643
conditions: new BlobRequestConditions

sdk/storage/Azure.Storage.Blobs/tests/BlobTestBase.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Net;
88
using System.Text;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011
using Azure.Core;
1112
using Azure.Core.TestFramework;
@@ -573,5 +574,25 @@ public void Handle(BlobQueryError blobQueryError)
573574
Assert.AreEqual(_expectedBlobQueryError.Position, blobQueryError.Position);
574575
}
575576
}
577+
578+
/// <summary>
579+
/// For our Download Initial Response 304 tests, sometimes the Modfified Since time
580+
/// comes back to be later than the actual Utc time in the environment that ran the test.
581+
/// We run this method to check the IfModified time and if it's later than the time, then
582+
/// let's wait until that time has past and then continue.
583+
///
584+
/// Or else when we attempt to the Download at set the LastModified time before that, it will
585+
/// send us back a 200, or even an error for sending a LastModified time that's in the future.
586+
///
587+
/// At most it's a 1 second difference wait. But most of the time there's no wait.
588+
/// </summary>
589+
public void CheckModifiedSinceAndWait(Response<BlobContentInfo> uploadResponse)
590+
{
591+
DateTimeOffset offsetNow = Recording.UtcNow;
592+
if (DateTimeOffset.Compare(uploadResponse.Value.LastModified, offsetNow) > 0)
593+
{
594+
Thread.Sleep(uploadResponse.Value.LastModified.Subtract(offsetNow));
595+
}
596+
}
576597
}
577598
}

0 commit comments

Comments
 (0)