public async void CreateNewBlobTwice_FalseShouldReturn() { var uniqueContainerName = Guid.NewGuid().ToString("N"); AzureBlobContainerClient blobProvider = GetTestBlobProvider(ConnectionString, uniqueContainerName); string blobName = Guid.NewGuid().ToString("N"); var blobContainerClient = new BlobContainerClient(ConnectionString, uniqueContainerName); var blobClient = blobContainerClient.GetBlobClient(blobName); await blobClient.DeleteIfExistsAsync(); Assert.False(blobClient.Exists()); // create a new blob using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("example"))) { var isCreated = await blobProvider.CreateBlobAsync(blobName, stream, CancellationToken.None); Assert.True(isCreated); Assert.True(blobClient.Exists()); } // create the blob again using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("new example"))) { var isCreated = await blobProvider.CreateBlobAsync(blobName, stream, CancellationToken.None); Assert.False(isCreated); Assert.True(blobClient.Exists()); } await blobContainerClient.DeleteAsync(); }
public async void AccessBlobProvider_WhenContainerNoExists_ExceptionShouldBeThrown() { var uniqueContainerName = Guid.NewGuid().ToString("N"); AzureBlobContainerClient blobProvider = GetTestBlobProvider(ConnectionString, uniqueContainerName); string blobName = Guid.NewGuid().ToString("N"); // call function of blobProvider using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("example"))) { Assert.True(await blobProvider.CreateBlobAsync(blobName, stream, CancellationToken.None)); } var blobServiceClient = new BlobServiceClient(ConnectionString); // delete the created container blobServiceClient.DeleteBlobContainer(uniqueContainerName); // call CreateBlobAsync() after the container is deleted by others using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("new example"))) { await Assert.ThrowsAsync <AzureBlobOperationFailedException>(() => blobProvider.CreateBlobAsync(blobName, stream, CancellationToken.None)); } // call UploadStreamToBlobAsync()after the container is deleted by others using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("new example"))) { await Assert.ThrowsAsync <AzureBlobOperationFailedException>(() => blobProvider.UpdateBlobAsync(blobName, stream, CancellationToken.None)); } // call GetBlobAsync() after the container is deleted by others await Assert.ThrowsAsync <AzureBlobOperationFailedException>(() => blobProvider.GetBlobAsync(blobName, CancellationToken.None)); }
public async void AcquireLease_WhenALeaseExists_ExceptionShouldBeThrown() { var uniqueContainerName = Guid.NewGuid().ToString("N"); AzureBlobContainerClient blobProvider = GetTestBlobProvider(ConnectionString, uniqueContainerName); string blobName = Guid.NewGuid().ToString("N"); var blobContainerClient = new BlobContainerClient(ConnectionString, uniqueContainerName); var blobClient = blobContainerClient.GetBlobClient(blobName); blobClient.DeleteIfExists(); Assert.False(blobClient.Exists()); // create a new blob string blobContent = "example"; var blobUrl_1 = await blobProvider.CreateBlobAsync(blobName, new MemoryStream(Encoding.ASCII.GetBytes(blobContent)), CancellationToken.None); Assert.True(blobClient.Exists()); var lease = await blobProvider.AcquireLeaseAsync(blobName, null, TimeSpan.FromSeconds(30), default); Assert.NotNull(lease); var lease2 = await blobProvider.AcquireLeaseAsync(blobName, null, TimeSpan.FromSeconds(30), default); Assert.Null(lease2); var lease3 = await blobProvider.AcquireLeaseAsync(blobName, lease, TimeSpan.FromSeconds(30), default); Assert.Equal(lease, lease3); await blobContainerClient.DeleteAsync(); }
public async void DownloadBlob_StreamShouldReturn() { var uniqueContainerName = Guid.NewGuid().ToString("N"); AzureBlobContainerClient blobProvider = GetTestBlobProvider(ConnectionString, uniqueContainerName); string blobName = Guid.NewGuid().ToString("N"); var blobContainerClient = new BlobContainerClient(ConnectionString, uniqueContainerName); var blobClient = blobContainerClient.GetBlobClient(blobName); string blobContent = "example"; // create a new blob using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(blobContent))) { var isCreated = await blobProvider.CreateBlobAsync(blobName, stream, CancellationToken.None); Assert.True(isCreated); Assert.True(blobClient.Exists()); } using Stream downloadStream = await blobProvider.GetBlobAsync(blobName, CancellationToken.None); using var reader = new StreamReader(downloadStream); Assert.Equal(blobContent, reader.ReadToEnd()); await blobContainerClient.DeleteAsync(); }
public async void CreateLargeNewBlobTwice_AtTheSameTime_TrueShouldReturnForTheFirstCompleted_And_FalseShouldReturnForTheSecondOne() { var uniqueContainerName = Guid.NewGuid().ToString("N"); AzureBlobContainerClient blobProvider = GetTestBlobProvider(ConnectionString, uniqueContainerName); string blobName = Guid.NewGuid().ToString("N"); var blobContainerClient = new BlobContainerClient(ConnectionString, uniqueContainerName); var blobClient = blobContainerClient.GetBlobClient(blobName); await blobClient.DeleteIfExistsAsync(); Assert.False(blobClient.Exists()); using MemoryStream sourceStream = new MemoryStream(); using StreamWriter writer = new StreamWriter(sourceStream); int lineNumber = (1024 * 1024) + 3; while (lineNumber-- > 0) { await writer.WriteLineAsync(Guid.NewGuid().ToString("N")); } await writer.FlushAsync(); sourceStream.Position = 0; var task_1 = blobProvider.CreateBlobAsync(blobName, sourceStream, CancellationToken.None); var task_2 = blobProvider.CreateBlobAsync(blobName, sourceStream, CancellationToken.None); var isCreateds = await Task.WhenAll(task_1, task_2); Assert.True(blobClient.Exists()); Assert.True(isCreateds[0] ^ isCreateds[1]); await blobContainerClient.DeleteAsync(); }