Esempio n. 1
0
        public async Task TestThirdScenario()
        {
            var cosmosState      = new MockCosmosState();
            var database         = new MockCosmosDatabase(cosmosState);
            var accountContainer = (MockCosmosContainer)await database.CreateContainerAsync(Constants.AccountContainerName);

            var imageContainer = (MockCosmosContainer)await database.CreateContainerAsync(Constants.ImageContainerName);

            var blobContainer   = new MockBlobContainerProvider();
            var messagingClient = new MockMessagingClient(blobContainer);

            var petImagesClient = new TestPetImagesClient(accountContainer, imageContainer, blobContainer, messagingClient);

            string accountName = "MyAccount";
            string imageName   = "pet.jpg";

            // Create an account request payload
            var account = new Account()
            {
                Name = accountName
            };

            var accountResult = await petImagesClient.CreateAccountAsync(account);

            Assert.IsTrue(accountResult.StatusCode == HttpStatusCode.OK);

            var task1 = petImagesClient.CreateOrUpdateImageAsync(accountName, new Image()
            {
                Name = imageName, Content = GetDogImageBytes()
            });
            var task2 = petImagesClient.CreateOrUpdateImageAsync(accountName, new Image()
            {
                Name = imageName, Content = GetCatImageBytes()
            });
            await Task.WhenAll(task1, task2);

            Assert.IsTrue(task1.Result.StatusCode == HttpStatusCode.OK);
            Assert.IsTrue(task1.Result.StatusCode == HttpStatusCode.OK);

            var imageResult = await petImagesClient.GetImageAsync(accountName, imageName);

            Assert.IsTrue(imageResult.StatusCode == HttpStatusCode.OK);
            byte[] image = imageResult.Resource;

            byte[] thumbnail;
            while (true)
            {
                var thumbnailResult = await petImagesClient.GetImageThumbnailAsync(accountName, imageName);

                if (thumbnailResult.StatusCode == HttpStatusCode.OK)
                {
                    thumbnail = thumbnailResult.Resource;
                    break;
                }
            }

            Assert.IsTrue(
                (IsDogImage(image) && IsDogThumbnail(thumbnail)) ||
                (IsCatImage(image) && IsCatThumbnail(thumbnail)));
        }
Esempio n. 2
0
        public async Task TestSecondScenario()
        {
            var cosmosState      = new MockCosmosState();
            var database         = new MockCosmosDatabase(cosmosState);
            var accountContainer = (MockCosmosContainer)await database.CreateContainerAsync(Constants.AccountContainerName);

            var imageContainer = (MockCosmosContainer)await database.CreateContainerAsync(Constants.ImageContainerName);

            var blobContainer   = new MockBlobContainerProvider();
            var messagingClient = new MockMessagingClient(blobContainer);

            var petImagesClient = new TestPetImagesClient(accountContainer, imageContainer, blobContainer, messagingClient);

            string accountName = "MyAccount";
            string imageName   = "pet.jpg";

            // Create an account request payload
            var account = new Account()
            {
                Name = accountName
            };

            var accountResult = await petImagesClient.CreateAccountAsync(account);

            Assert.IsTrue(accountResult.StatusCode == HttpStatusCode.OK);

            imageContainer.EnableRandomizedFaults();

            var task1 = petImagesClient.CreateImageAsync(accountName, new Image()
            {
                Name = imageName, Content = GetDogImageBytes()
            });
            var task2 = petImagesClient.CreateImageAsync(accountName, new Image()
            {
                Name = imageName, Content = GetDogImageBytes()
            });
            await Task.WhenAll(task1, task2);

            var statusCode1 = task1.Result.StatusCode;
            var statusCode2 = task2.Result.StatusCode;

            imageContainer.DisableRandomizedFaults();

            Assert.IsTrue(statusCode1 == HttpStatusCode.OK || statusCode1 == HttpStatusCode.Conflict || statusCode1 == HttpStatusCode.ServiceUnavailable);
            Assert.IsTrue(statusCode2 == HttpStatusCode.OK || statusCode2 == HttpStatusCode.Conflict || statusCode2 == HttpStatusCode.ServiceUnavailable);

            if (task1.Result.StatusCode == HttpStatusCode.OK || task2.Result.StatusCode == HttpStatusCode.OK)
            {
                var imageContentResult = await petImagesClient.GetImageAsync(accountName, imageName);

                Assert.IsTrue(imageContentResult.StatusCode == HttpStatusCode.OK);
                Assert.IsTrue(IsDogImage(imageContentResult.Resource));
            }
        }
Esempio n. 3
0
 public ServiceFactory()
 {
     this.BlobContainer   = new MockBlobContainerProvider();
     this.MessagingClient = new MockMessagingClient(this.BlobContainer);
     this.CosmosDatabase  = new MockCosmosDatabase(new MockCosmosState());
 }