Ejemplo n.º 1
0
        public void EnumerateTwiceNotAllTest()
        {
            var target = new CachedEnumerable <int>(GetNumbers(3));

            Assert.AreEqual(new[] { 0, 1 }, target.Take(2).ToArray());
            Assert.AreEqual(new[] { 0, 1 }, target.Take(2).ToArray());
        }
Ejemplo n.º 2
0
 public AnyElementNode(AnyElementNode parent, CachedEnumerable <IXPathElement> elements, int index)
 {
     Parent    = parent;
     Element   = elements[index];
     _elements = elements;
     _index    = index;
 }
Ejemplo n.º 3
0
        public void CachedEnumerableMultipleEnumerators()
        {
            var source = Enumerable.Range(1, 5).ToList();

            var cachedEnumerable = new CachedEnumerable <int>(source);

            var enumerator1 = cachedEnumerable.GetEnumerator();
            var result1     = new List <int>();

            enumerator1.MoveNext();
            result1.Add(enumerator1.Current);
            enumerator1.MoveNext();
            result1.Add(enumerator1.Current);

            var enumerator2 = cachedEnumerable.GetEnumerator();
            var result2     = new List <int>();

            enumerator2.MoveNext();
            result2.Add(enumerator2.Current);

            while (enumerator1.MoveNext())
            {
                result1.Add(enumerator1.Current);
            }
            while (enumerator2.MoveNext())
            {
                result2.Add(enumerator2.Current);
            }

            CollectionAssert.AreEqual(source, result1);
            CollectionAssert.AreEqual(source, result2);
        }
Ejemplo n.º 4
0
 internal ComputedCssStyleDeclaration(CssStyleSheet defaultStyleSheet, Element elt, Func <int> getVersion)
 {
     _defaultStyleSheet = defaultStyleSheet;
     _elt           = elt;
     _getVersion    = getVersion;
     _styles        = new CachedEnumerable <ICssStyleDeclaration>(GetStylesFor(elt));
     _cachedVersion = getVersion();
 }
Ejemplo n.º 5
0
        public void MultipleEnumerations_ShouldEnumerateOnce()
        {
            var enumerable       = new SingleEnumerable <int>(Enumerable.Range(1, 3));
            var cachedEnumerable = CachedEnumerable.Create(enumerable);

            Assert.Equal(new[] { 1, 2, 3 }, cachedEnumerable);
            Assert.Equal(new[] { 1, 2, 3 }, cachedEnumerable);
        }
        private IEnumerable <ServerDescription> SelectForReplicaSet(ClusterDescription cluster, IEnumerable <ServerDescription> servers)
        {
            if (_maxStaleness.HasValue)
            {
                var minHeartBeatIntervalTicks = servers.Select(s => s.HeartbeatInterval.Ticks).Min();
                if (_maxStaleness.Value.Ticks < 2 * minHeartBeatIntervalTicks)
                {
                    throw new MongoClientException("MaxStaleness must be at least twice the heartbeat interval.");
                }

                servers = new CachedEnumerable <ServerDescription>(SelectFreshServers(cluster, servers)); // prevent multiple enumeration
            }
            else
            {
                servers = new CachedEnumerable <ServerDescription>(servers); // prevent multiple enumeration
            }

            switch (_readPreference.ReadPreferenceMode)
            {
            case ReadPreferenceMode.Primary:
                return(servers.Where(n => n.Type == ServerType.ReplicaSetPrimary));

            case ReadPreferenceMode.PrimaryPreferred:
                var primary = servers.FirstOrDefault(n => n.Type == ServerType.ReplicaSetPrimary);
                if (primary != null)
                {
                    return(new[] { primary });
                }
                else
                {
                    return(SelectByTagSets(servers.Where(n => n.Type == ServerType.ReplicaSetSecondary)));
                }

            case ReadPreferenceMode.Secondary:
                return(SelectByTagSets(servers.Where(n => n.Type == ServerType.ReplicaSetSecondary)));

            case ReadPreferenceMode.SecondaryPreferred:
                var matchingSecondaries = SelectByTagSets(servers.Where(n => n.Type == ServerType.ReplicaSetSecondary)).ToList();
                if (matchingSecondaries.Count != 0)
                {
                    return(matchingSecondaries);
                }
                else
                {
                    return(servers.Where(n => n.Type == ServerType.ReplicaSetPrimary));
                }

            case ReadPreferenceMode.Nearest:
                return(SelectByTagSets(servers.Where(n => n.Type == ServerType.ReplicaSetPrimary || n.Type == ServerType.ReplicaSetSecondary)));

            default:
                throw new ArgumentException("Invalid ReadPreferenceMode.");
            }
        }
Ejemplo n.º 7
0
        public void CachedEnumerableEnumerate()
        {
            var source = Enumerable.Range(1, 5).ToList();

            var cachedEnumerable = new CachedEnumerable <int>(source);
            var result           = new List <int>();

            foreach (var i in cachedEnumerable)
            {
                result.Add(i);
            }

            CollectionAssert.AreEqual(result, source);
        }
Ejemplo n.º 8
0
        ITerminationHandle ISearchLens <GameObject> .SetSearchData(ISelectionPosition selectionPosition,
                                                                   IConsumer <GameObject> consumer, string search)
        {
            if (_searchString == null)
            {
                _objects = new CachedEnumerable <GameObject>(SceneWalker.SceneObjectsDFS(ShouldExploreObject));
            }
            _searchString = search;
            if (search.Length > 0)
            {
                var matcher = SoftStringMatcher.New(search);
                consumer.Consume(_objects.Where(g => matcher.IsMatch(g.name)));
            }

            return(null);
        }
Ejemplo n.º 9
0
        public void MultipleConcurrentEnumerations_ShouldEnumerateOnce()
        {
            // Arrange
            var count            = 0;
            var cachedEnumerable = CachedEnumerable.Create(new SingleEnumerable <int>(GetData()));

            IEnumerable <int> GetData()
            {
                yield return(++count);

                yield return(++count);

                yield return(++count);
            }

            var enumerator1 = cachedEnumerable.GetEnumerator();
            var enumerator2 = cachedEnumerable.GetEnumerator();

            // Act & assert
            Assert.True(enumerator1.MoveNext());
            Assert.Equal(1, enumerator1.Current);
            Assert.Equal(1, count);

            Assert.True(enumerator2.MoveNext());
            Assert.Equal(1, enumerator2.Current);
            Assert.Equal(1, count);

            Assert.True(enumerator2.MoveNext());
            Assert.Equal(2, enumerator2.Current);
            Assert.Equal(2, count);

            Assert.True(enumerator2.MoveNext());
            Assert.Equal(3, enumerator2.Current);
            Assert.Equal(3, count);

            Assert.True(enumerator1.MoveNext());
            Assert.Equal(2, enumerator1.Current);
            Assert.Equal(3, count);

            Assert.False(enumerator2.MoveNext());
            Assert.True(enumerator1.MoveNext());
            Assert.False(enumerator1.MoveNext());
            Assert.Equal(3, count);

            Assert.Equal(new[] { 1, 2, 3 }, cachedEnumerable);
        }
Ejemplo n.º 10
0
        private IEnumerable <ServerDescription> SelectForReplicaSet(ClusterDescription cluster, IEnumerable <ServerDescription> servers)
        {
            EnsureMaxStalenessIsValid(cluster);

            servers = new CachedEnumerable <ServerDescription>(servers); // prevent multiple enumeration

            switch (_readPreference.ReadPreferenceMode)
            {
            case ReadPreferenceMode.Primary:
                return(SelectPrimary(servers));

            case ReadPreferenceMode.PrimaryPreferred:
                var primary = SelectPrimary(servers);
                if (primary.Count != 0)
                {
                    return(primary);
                }
                else
                {
                    return(SelectByTagSets(SelectFreshSecondaries(cluster, servers)));
                }

            case ReadPreferenceMode.Secondary:
                return(SelectByTagSets(SelectFreshSecondaries(cluster, servers)));

            case ReadPreferenceMode.SecondaryPreferred:
                var selectedSecondaries = SelectByTagSets(SelectFreshSecondaries(cluster, servers)).ToList();
                if (selectedSecondaries.Count != 0)
                {
                    return(selectedSecondaries);
                }
                else
                {
                    return(SelectPrimary(servers));
                }

            case ReadPreferenceMode.Nearest:
                return(SelectByTagSets(SelectPrimary(servers).Concat(SelectFreshSecondaries(cluster, servers))));

            default:
                throw new ArgumentException("Invalid ReadPreferenceMode.");
            }
        }
Ejemplo n.º 11
0
        public async Task MultipleConcurrentEnumerations_ShouldEnumerateOnce_ThreadSafe()
        {
            // Arrange
            var maxCount    = 1000;
            var threadCount = 16;
            var resetEvent  = new ManualResetEventSlim(initialState: false);

            var count            = 0;
            var cachedEnumerable = CachedEnumerable.Create(new SingleEnumerable <int>(GetData()));

            IEnumerable <int> GetData()
            {
                for (var i = 0; i < maxCount; i++)
                {
                    yield return(++count);
                }
            }

            var results = new List <int> [1000];
            var task    = Task.Run(() => Parallel.For(0, 1000, new ParallelOptions {
                MaxDegreeOfParallelism = threadCount
            }, i =>
            {
                results[i] = cachedEnumerable.ToList();
            }));

            resetEvent.Set();
            await task;

            // Act & assert
            Assert.Equal(maxCount, count);
            foreach (var result in results)
            {
                Assert.Equal(Enumerable.Range(1, maxCount), result);
            }
        }
Ejemplo n.º 12
0
        public void EnumerateOnceTest()
        {
            var target = new CachedEnumerable <int>(GetNumbers(3));

            Assert.AreEqual(new[] { 0, 1, 2 }, target.ToArray());
        }
 public void Dispose()
 {
     _CachedEnumerable = null;
 }
 public CachedEnumerator(CachedEnumerable <T> cachedEnumerable)
 {
     _CachedEnumerable = cachedEnumerable;
 }
        public void CollectionCreate01()
        {
            using var result = CachedEnumerable.Create(this.PersonProperList);

            this.Consumer.Consume(result);
        }
        private IEnumerable<ServerDescription> SelectForReplicaSet(ClusterDescription cluster, IEnumerable<ServerDescription> servers)
        {
            EnsureMaxStalenessIsValid(cluster);

            servers = new CachedEnumerable<ServerDescription>(servers); // prevent multiple enumeration

            switch (_readPreference.ReadPreferenceMode)
            {
                case ReadPreferenceMode.Primary:
                    return SelectPrimary(servers);

                case ReadPreferenceMode.PrimaryPreferred:
                    var primary = SelectPrimary(servers);
                    if (primary.Count != 0)
                    {
                        return primary;
                    }
                    else
                    {
                        return SelectByTagSets(SelectFreshSecondaries(cluster, servers));
                    }

                case ReadPreferenceMode.Secondary:
                    return SelectByTagSets(SelectFreshSecondaries(cluster, servers));

                case ReadPreferenceMode.SecondaryPreferred:
                    var selectedSecondaries = SelectByTagSets(SelectFreshSecondaries(cluster, servers)).ToList();
                    if (selectedSecondaries.Count != 0)
                    {
                        return selectedSecondaries;
                    }
                    else
                    {
                        return SelectPrimary(servers);
                    }

                case ReadPreferenceMode.Nearest:
                    return SelectByTagSets(SelectPrimary(servers).Concat(SelectFreshSecondaries(cluster, servers)));

                default:
                    throw new ArgumentException("Invalid ReadPreferenceMode.");
            }
        }
Ejemplo n.º 17
0
 public AnyAttributeNode(CachedEnumerable <IXPathAttribute> attributes, int index)
 {
     _attributes = attributes;
     _index      = index;
     _attribute  = _attributes[index];
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Instantiates and returns a <see cref="CachedEnumerable{T}"/> for a given <paramref name="enumerable"/>.
 /// Notice: The first item is always iterated through.
 /// </summary>
 public static CachedEnumerable <T> ToCachedEnumerable <T>(this IEnumerable <T> enumerable)
 {
     return(CachedEnumerable <T> .Create(enumerable));
 }
Ejemplo n.º 19
0
 public CachedEnumerator(CachedEnumerable <T> parent)
 {
     this.parent = parent;
     Reset();
 }