public async Task GetBlobListReturnsAllBlobsInContainer()
        {
            var objId1 = Guid.NewGuid().ToString();
            var objId2 = Guid.NewGuid().ToString();

            var azureBlobContainer = new AzureBlobContainer <string>(
                account,
                AzureBlobTestContainer);
            await azureBlobContainer.SaveAsync(objId1, "testText");

            await azureBlobContainer.SaveAsync(objId2, "testText");

            var blobList = azureBlobContainer.GetBlobList().Select(b => b.Name).ToList();

            CollectionAssert.Contains(blobList, objId1);
            CollectionAssert.Contains(blobList, objId2);
        }
Пример #2
0
        private async Task SaveAsync(AzureBlobContainer <List <string> > answerListContainer, string slugName, string surveyAnswerId)
        {
            // NOTE: appending ID's into a blob doesn't scale well under heavy load. Other solution is left
            //       for the reader to implement.
            var answerIdList = await answerListContainer.GetAsync(slugName)
                               .ConfigureAwait(false) ?? new List <string>();

            answerIdList.Add(surveyAnswerId);
            await answerListContainer.SaveAsync(slugName, answerIdList)
            .ConfigureAwait(false);
        }
        public async Task SaveShouldStoreTheBlob()
        {
            var objId = Guid.NewGuid().ToString();

            var azureBlobContainer = new AzureBlobContainer <string>(
                account,
                AzureBlobTestContainer);
            await azureBlobContainer.SaveAsync(objId, "testText");

            Assert.IsNotNull(await azureBlobContainer.GetAsync(objId));
        }
        public async Task CreateNewBlob()
        {
            var azureBlobContainer = new AzureBlobContainer <string>(
                account,
                AzureBlobTestContainer);

            var objId = Guid.NewGuid().ToString();

            var text = await azureBlobContainer.GetAsync(objId);

            Assert.IsNull(text);

            await azureBlobContainer.SaveAsync(objId, "testText");

            text = await azureBlobContainer.GetAsync(objId);

            Assert.IsNotNull(text);
        }
        public async Task SaveAndGet()
        {
            var surveyStorage  = new AzureBlobContainer <Survey>(account, SurveyContainer);
            var expectedSurvey = new Survey {
                Title = "title", SlugName = "slugname"
            };
            var question1 = new Question {
                Text = "text 1", Type = "SimpleText", PossibleAnswers = string.Empty
            };
            var question2 = new Question {
                Text = "text 2", Type = "MultipleChoice", PossibleAnswers = "answer 1\nanswer2"
            };
            var question3 = new Question {
                Text = "text 3", Type = "FiveStars", PossibleAnswers = string.Empty
            };

            (expectedSurvey.Questions as List <Question>).AddRange(new[] { question1, question2, question3 });

            await surveyStorage.SaveAsync(expectedSurvey.SlugName, expectedSurvey);

            var actualSurvey = await surveyStorage.GetAsync(expectedSurvey.SlugName);

            Assert.AreEqual(expectedSurvey.Title, actualSurvey.Title);
            Assert.AreEqual(expectedSurvey.SlugName, actualSurvey.SlugName);
            Assert.AreEqual(3, actualSurvey.Questions.Count);
            var actualQuestionAnswer1 = actualSurvey.Questions.SingleOrDefault(q =>
                                                                               q.Text == "text 1" &&
                                                                               q.Type == "SimpleText" &&
                                                                               q.PossibleAnswers == string.Empty);

            Assert.IsNotNull(actualQuestionAnswer1);
            var actualQuestionAnswer2 = actualSurvey.Questions.SingleOrDefault(q =>
                                                                               q.Text == "text 2" &&
                                                                               q.Type == "MultipleChoice" &&
                                                                               q.PossibleAnswers == "answer 1\nanswer2");

            Assert.IsNotNull(actualQuestionAnswer2);
            var actualQuestionAnswer3 = actualSurvey.Questions.SingleOrDefault(q =>
                                                                               q.Text == "text 3" &&
                                                                               q.Type == "FiveStars" &&
                                                                               q.PossibleAnswers == string.Empty);

            Assert.IsNotNull(actualQuestionAnswer3);
        }
        public async Task SaveAndDelete()
        {
            var surveyStorage  = new AzureBlobContainer <Survey>(account, SurveyContainer);
            var expectedSurvey = new Survey {
                Title = "title", SlugName = "slugname"
            };

            await surveyStorage.SaveAsync(expectedSurvey.SlugName, expectedSurvey);

            Survey savedSurvey = await surveyStorage.GetAsync(expectedSurvey.SlugName);

            Assert.IsNotNull(savedSurvey);

            await surveyStorage.DeleteAsync(expectedSurvey.SlugName);

            Survey deletedSurveyAnswer = await surveyStorage.GetAsync(expectedSurvey.SlugName);

            Assert.IsNull(deletedSurveyAnswer);
        }
Пример #7
0
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            int  batchSize = 100;
            long delayMs   = 20;

            surveyQueue = await this.StateManager.GetOrAddAsync <IReliableConcurrentQueue <ClientModels.SurveyAnswer> >("surveyQueue");

            ISurveyAnalysisService surveyAnalysisService = new Tailspin.SurveyAnalysisService.Client.SurveyAnalysisService();

            List <ClientModels.SurveyAnswer> processItems = new List <ClientModels.SurveyAnswer>();

            try
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    using (var tx = this.StateManager.CreateTransaction())
                    {
                        ConditionalValue <ClientModels.SurveyAnswer> ret;

                        for (int i = 0; i < batchSize; ++i)
                        {
                            ret = await surveyQueue.TryDequeueAsync(tx, cancellationToken);

                            if (ret.HasValue)
                            {
                                processItems.Add(ret.Value.DeepCopy());
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (processItems.Count > 0)
                        {
                            foreach (var sa in processItems)
                            {
                                var model = sa.ToSurveyAnswer();
                                model.CreatedOn = DateTime.UtcNow;

                                var container = new AzureBlobContainer <ApiModels.SurveyAnswer>(
                                    ServiceFabricConfiguration.GetCloudStorageAccount(),
                                    $"{model.SlugName}-answers");

                                try
                                {
                                    await container.SaveAsync(model.Id, model);
                                }
                                catch (StorageException ex)
                                {
                                    if (ex.Message.Contains("404"))
                                    {
                                        await container.EnsureExistsAsync();

                                        await container.SaveAsync(model.Id, model);
                                    }
                                    else
                                    {
                                        throw ex;
                                    }
                                }

                                await this.AppendSurveyAnswerIdToSurveyAnswerListAsync(model.SlugName, model.Id);

                                await surveyAnalysisService.MergeSurveyAnswerToAnalysisAsync(model.ToAnalysisServiceSurveyAnswer());
                            }

                            processItems.Clear();
                        }

                        await tx.CommitAsync();
                    }

                    await Task.Delay(TimeSpan.FromMilliseconds(delayMs), cancellationToken);
                }
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.ServiceRequestFailed(ex.ToString());
                throw;
            }
        }