Пример #1
0
        public void StressTest()
        {
            List <Dummy> originalList = Enumerable.Range(0, 10000).Select(x => new Dummy()).ToList();
            var          rnd          = new Random(0);
            var          checkHashSet =
                new HashSet <Dummy>(
                    rnd.RandomUniqueSet(0, originalList.Count, originalList.Count / 2).Select(x => originalList[x]));
            var setToTest = new ProcessableSet <Dummy>();

            foreach (Dummy dummy in checkHashSet)
            {
                setToTest.Add(dummy);
            }

            setToTest.ForEach(
                x =>
            {
                if (rnd.NextDouble() < 0.3)
                {
                    int itemIndex  = rnd.Next(0, originalList.Count);
                    Dummy testItem = originalList[itemIndex];
                    bool goodResult;
                    bool testResult;
                    if (rnd.NextBool())
                    {
                        goodResult = checkHashSet.Add(testItem);
                        testResult = setToTest.Add(testItem);
                        testResult.Should().Be(goodResult, "Add operation wrong.");
                    }
                    else
                    {
                        goodResult = checkHashSet.Remove(testItem);
                        testResult = setToTest.Remove(testItem);
                        testResult.Should().Be(goodResult, "Remove operation wrong.");
                        if (testResult)
                        {
                            // Forgetting that processed, if it was removed.
                            testItem.ProcessedCount = 0;
                        }
                    }
                }

                x.ProcessedCount++;
            });

            foreach (Dummy dummy in setToTest)
            {
                dummy.ProcessedCount.Should().Be(1, "Some element processed more than once.");
            }

            setToTest.Count.Should().Be(checkHashSet.Count);
            foreach (Dummy dummy in checkHashSet)
            {
                setToTest.Should().Contain(dummy);
            }
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IxInstance"/> class.
        /// </summary>
        /// <remarks>
        /// Instance creates in the "Half-instantiated state".
        /// </remarks>
        /// <param name="providerNode">Node that produces instance.</param>
        /// <param name="parentInstance">Direct parent instance.</param>
        /// <param name="creatorTempLock">First temp lock for the creator of a new instance.</param>
        public IxInstance(
            IxProviderNode providerNode,
            [CanBeNull] IIxInstance parentInstance,
            out IIxInstanceLock creatorTempLock)
            : base(providerNode.Host.InstanceTreeSyncRoot)
        {
            ProviderNode              = providerNode;
            _parentInstance           = parentInstance;
            _ownedLocks               = new ProcessableSet <IIxInstanceLock>();
            _locks                    = new ProcessableSet <IIxInstanceLock>();
            _childrenDisposeCompleted = new AwaitableEvent();
            if (parentInstance != null)
            {
                new IxInstanceChildLock(parentInstance, this);
            }

            creatorTempLock = new IxInstancePinLock(this);
            _initTempLock   = creatorTempLock;
        }