本文整理汇总了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();
}
}