当前位置: 首页>>代码示例>>C#>>正文


C# BlockingCollection.Should方法代码示例

本文整理汇总了C#中BlockingCollection.Should方法的典型用法代码示例。如果您正苦于以下问题:C# BlockingCollection.Should方法的具体用法?C# BlockingCollection.Should怎么用?C# BlockingCollection.Should使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BlockingCollection的用法示例。


在下文中一共展示了BlockingCollection.Should方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: it_should_return_the_same_instance_even_when_calling_it_from_different_threads

            public void it_should_return_the_same_instance_even_when_calling_it_from_different_threads()
            {
                ContextResolver sut = new ConcreteResolverBuilder().Initialize();
                var firstResolvedContext = sut.GetCurrentContext<PubsContext>();
                var numberOfConcurrentProcess = 10000;
                var contextResolverInstances = new BlockingCollection<DbContext>();
                var tasks = new Task[numberOfConcurrentProcess];

                Console.WriteLine("Initial Thread ID: {0}", Thread.CurrentThread.ManagedThreadId);

                for (int i = 0; i < numberOfConcurrentProcess; i++)
                {
                    tasks[i] = Task.Factory.StartNew(() =>
                    {
                        var context = new ConcreteResolverBuilder().Initialize().Build().GetCurrentContext<PubsContext>();

                        contextResolverInstances.Add(context);

                        Console.WriteLine("Task - Thread ID: {0}", Thread.CurrentThread.ManagedThreadId);
                    }, TaskCreationOptions.LongRunning);
                }

                Task.WaitAll(tasks);

                contextResolverInstances
                    .Should()
                        .NotBeNull()
                    .And
                        .NotBeEmpty()
                    .And
                        .HaveCount(numberOfConcurrentProcess)
                    .And
                        .ContainItemsAssignableTo<DbContext>()
                    .And
                        .NotContainNulls();

                var firstResolvedContextHashCode = (firstResolvedContext as object).GetHashCode();

                firstResolvedContextHashCode.Should().NotBe(default(int));

                foreach (var item in contextResolverInstances)
                {
                    var itemHashCode = (item as object).GetHashCode();

                    itemHashCode.Should().NotBe(default(int));

                    itemHashCode.Should().Be(firstResolvedContextHashCode);
                    item.Should().Be(firstResolvedContext);

                    var areReferenceEquals = ReferenceEquals(item, firstResolvedContext);

                    areReferenceEquals.Should().BeTrue();
                }
            }
开发者ID:jupaol,项目名称:LearningProjects,代码行数:54,代码来源:ContextResolverTests.cs


注:本文中的BlockingCollection.Should方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。