public async Task GivenDataStream_WhenAppendToBlob_ThenDataShouldBeAppended() { IIntegrationDataStoreClientInitilizer <CloudBlobClient> initializer = GetClientInitializer(); CloudBlobClient client = await initializer.GetAuthorizedClientAsync(CancellationToken.None); string containerName = Guid.NewGuid().ToString("N"); string blobName = Guid.NewGuid().ToString("N"); Uri blobUri = new Uri(Path.Combine(client.StorageUri.PrimaryUri.ToString(), $"{containerName}/{blobName}")); try { AzureBlobIntegrationDataStoreClient blobClient = new AzureBlobIntegrationDataStoreClient(initializer, GetIntegrationDataStoreConfigurationOption(), new NullLogger <AzureBlobIntegrationDataStoreClient>()); await blobClient.PrepareResourceAsync(containerName, blobName, CancellationToken.None); long count = 30; List <string> blockIds = new List <string>(); for (long i = 0; i < count; ++i) { using Stream input = new MemoryStream(Encoding.UTF8.GetBytes(i.ToString() + "\r\n")); string blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); await blobClient.UploadBlockAsync(blobUri, input, blockId, CancellationToken.None); blockIds.Add(blockId); } await blobClient.CommitAsync(blobUri, blockIds.ToArray(), CancellationToken.None); ICloudBlob output = await client.GetBlobReferenceFromServerAsync(blobUri); using Stream outputStream = new MemoryStream(); await output.DownloadToStreamAsync(outputStream); outputStream.Position = 0; using StreamReader reader = new StreamReader(outputStream); long currentLine = 0; string content = null; while ((content = await reader.ReadLineAsync()) != null) { Assert.Equal(currentLine.ToString(), content); currentLine++; } Assert.Equal(count, currentLine); } finally { var container = client.GetContainerReference(containerName); await container.DeleteIfExistsAsync(); } }
public async Task GivenStorageBlob_WhenAcquireLease_ThenLeaseIdShouldBeReturned() { IIntegrationDataStoreClientInitilizer <CloudBlobClient> initializer = GetClientInitializer(); CloudBlobClient client = await initializer.GetAuthorizedClientAsync(CancellationToken.None); string containerName = Guid.NewGuid().ToString("N"); string blobName = Guid.NewGuid().ToString("N"); Uri blobUri = new Uri(Path.Combine(client.StorageUri.PrimaryUri.ToString(), $"{containerName}/{blobName}")); try { AzureBlobIntegrationDataStoreClient blobClient = new AzureBlobIntegrationDataStoreClient(initializer, GetIntegrationDataStoreConfigurationOption(), new NullLogger <AzureBlobIntegrationDataStoreClient>()); await blobClient.PrepareResourceAsync(containerName, blobName, CancellationToken.None); long count = 30; List <string> blockIds = new List <string>(); for (long i = 0; i < count; ++i) { using Stream input = new MemoryStream(Encoding.UTF8.GetBytes(i.ToString() + "\r\n")); string blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); await blobClient.UploadBlockAsync(blobUri, input, blockId, CancellationToken.None); blockIds.Add(blockId); } await blobClient.CommitAsync(blobUri, blockIds.ToArray(), CancellationToken.None); string leaseId = await blobClient.TryAcquireLeaseAsync(blobUri, blobName, CancellationToken.None); Assert.NotNull(leaseId); string nullLeaseId = await blobClient.TryAcquireLeaseAsync(blobUri, "dummy", CancellationToken.None); Assert.Null(nullLeaseId); await blobClient.TryReleaseLeaseAsync(blobUri, leaseId, CancellationToken.None); } finally { var container = client.GetContainerReference(containerName); await container.DeleteIfExistsAsync(); } }