コード例 #1
0
        public void GatherItems_GetsChildren_ProcessesChildren()
        {
            Guid      parentGuid       = Guid.NewGuid();
            Guid      childGuid        = Guid.NewGuid();
            IItemData expectedItemData = Substitute.For <IItemData>();

            expectedItemData.Name.Returns("ExpectedName");
            expectedItemData.GetChildren().Returns(new List <IItemData> {
                Substitute.For <IItemData>()
            });

            ContentItemPuller contentItemPuller = CreateInstance <ContentItemPuller>();

            GetSubstitute <IRemoteContentService>().GetRemoteItemDataWithChildren(parentGuid, Arg.Any <string>()).Returns(new ChildrenItemDataModel {
                GrandChildren = new List <Guid> {
                    childGuid
                }
            });
            GetSubstitute <IRemoteContentService>().GetRemoteItemDataWithChildren(childGuid, Arg.Any <string>()).Returns(new ChildrenItemDataModel());
            GetSubstitute <IYamlSerializationService>().DeserializeYaml(Arg.Any <string>(), Arg.Any <string>()).Returns(expectedItemData);
            contentItemPuller.ProcessingIds.Add(parentGuid);

            contentItemPuller.GatherItems(true, "", CancellationToken.None, false);

            contentItemPuller.GatheredRemoteItems.Count.Should().Be(2);
        }
コード例 #2
0
        public async Task GatherItems_MultipleThreads_AllCompleteWhenFinished()
        {
            int       expectedCount    = 1000;
            IItemData expectedItemData = Substitute.For <IItemData>();

            expectedItemData.Name.Returns("ExpectedName");
            expectedItemData.GetChildren().Returns(new List <IItemData> {
                Substitute.For <IItemData>()
            });

            ContentItemPuller contentItemPuller = CreateInstance <ContentItemPuller>();

            GetSubstitute <IRemoteContentService>().GetRemoteItemDataWithChildren(Arg.Any <Guid>(), Arg.Any <string>()).Returns(new ChildrenItemDataModel());
            GetSubstitute <IYamlSerializationService>().DeserializeYaml(Arg.Any <string>(), Arg.Any <string>()).Returns(expectedItemData);
            for (int i = 0; i < expectedCount; i++)
            {
                contentItemPuller.ProcessingIds.Add(Guid.NewGuid());
            }

            var cancellationTokenSource = new CancellationTokenSource();

            Task[] taskList =
            {
                Task.Run(() =>
                {
                    contentItemPuller.GatherItems(false, "", cancellationTokenSource.Token, false);
                }),
                Task.Run(() =>
                {
                    contentItemPuller.GatherItems(false, "", cancellationTokenSource.Token, false);
                }),
                Task.Run(() =>
                {
                    contentItemPuller.GatherItems(false, "", cancellationTokenSource.Token, false);
                })
            };

            await Task.WhenAny(taskList);

            contentItemPuller.GatheredRemoteItems.Count.Should().Be(expectedCount);
            taskList.All(t => t.IsCompleted).Should().BeTrue();
        }
コード例 #3
0
        public void GatherItems_DeserializesRemoteContentItem()
        {
            ContentItemPuller contentItemPuller = CreateInstance <ContentItemPuller>();

            GetSubstitute <IRemoteContentService>().GetRemoteItemDataWithChildren(Arg.Any <Guid>(), Arg.Any <string>()).Returns(new ChildrenItemDataModel());
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());

            contentItemPuller.GatherItems(false, "", CancellationToken.None, false);

            GetSubstitute <IYamlSerializationService>().Received(1).DeserializeYaml(Arg.Any <string>(), Arg.Any <string>());
        }
コード例 #4
0
        public void GatherItems_TakesFromProcessingList()
        {
            ContentItemPuller contentItemPuller = CreateInstance <ContentItemPuller>();

            GetSubstitute <IRemoteContentService>().GetRemoteItemDataWithChildren(Arg.Any <Guid>(), Arg.Any <string>()).Returns(new ChildrenItemDataModel());
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());

            contentItemPuller.GatherItems(false, "", CancellationToken.None, false);

            contentItemPuller.ProcessingIds.Should().BeEmpty();
        }
コード例 #5
0
        public void GatherItems_AddsDeserializedValueToGatheredRemoteItems()
        {
            IItemData expectedItemData = Substitute.For <IItemData>();

            expectedItemData.Name.Returns("ExpectedName");
            ContentItemPuller contentItemPuller = CreateInstance <ContentItemPuller>();

            GetSubstitute <IRemoteContentService>().GetRemoteItemDataWithChildren(Arg.Any <Guid>(), Arg.Any <string>()).Returns(new ChildrenItemDataModel());
            GetSubstitute <IYamlSerializationService>().DeserializeYaml(Arg.Any <string>(), Arg.Any <string>()).Returns(expectedItemData);
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());

            contentItemPuller.GatherItems(false, "", CancellationToken.None, false);

            contentItemPuller.GatheredRemoteItems.Should().NotBeEmpty();
            contentItemPuller.GatheredRemoteItems.Should().Contain(expectedItemData);
        }
コード例 #6
0
        public void GatherItems_MultipleItems_ProcessesEachItem()
        {
            IItemData expectedItemData = Substitute.For <IItemData>();

            expectedItemData.Name.Returns("ExpectedName");
            expectedItemData.GetChildren().Returns(new List <IItemData> {
                Substitute.For <IItemData>()
            });

            ContentItemPuller contentItemPuller = CreateInstance <ContentItemPuller>();

            GetSubstitute <IRemoteContentService>().GetRemoteItemDataWithChildren(Arg.Any <Guid>(), Arg.Any <string>()).Returns(new ChildrenItemDataModel());
            GetSubstitute <IYamlSerializationService>().DeserializeYaml(Arg.Any <string>(), Arg.Any <string>()).Returns(expectedItemData);
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());

            contentItemPuller.GatherItems(false, "", CancellationToken.None, false);

            contentItemPuller.GatheredRemoteItems.Count.Should().Be(3);
        }
コード例 #7
0
        public void GatherItems_MultipleItems_CanceledDuringFirstItem_OnlyGathersOneItem()
        {
            ContentItemPuller contentItemPuller = CreateInstance <ContentItemPuller>();

            GetSubstitute <IRemoteContentService>().GetRemoteItemDataWithChildren(Arg.Any <Guid>(), Arg.Any <string>()).Returns(ci =>
            {
                // Introduce a delay
                Thread.Sleep(10);
                return(new ChildrenItemDataModel());
            });
            GetSubstitute <IYamlSerializationService>().DeserializeYaml(Arg.Any <string>(), Arg.Any <string>()).Returns(Substitute.For <IItemData>());
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());
            contentItemPuller.ProcessingIds.Add(Guid.NewGuid());

            CancellationTokenSource cts = new CancellationTokenSource(10);

            contentItemPuller.GatherItems(false, "", cts.Token, false);

            contentItemPuller.GatheredRemoteItems.Count.Should().BeLessThan(3);
        }