Esempio n. 1
0
 /// <summary>
 ///     Resolves a scope for the provided context.  If none of the registered resolvers can resolve the scope, null
 ///     is returned
 /// </summary>
 public object Resolve(IContext context)
 {
     return(resolvers
            .OrderBy(entry => entry.Order)
            .Select(entry => entry.Resolver.Resolve(context))
            .FirstOrDefault(scope => scope != null));
 }
Esempio n. 2
0
        public void ManyConcurrentAddsTakes_CollectionRemainsConsistent(double seconds)
        {
            IProducerConsumerCollection <int> c = CreateProducerConsumerCollection();

            DateTime end = DateTime.UtcNow + TimeSpan.FromSeconds(seconds);

            // Thread that adds
            Task <HashSet <int> > adds = ThreadFactory.StartNew(() =>
            {
                var added = new HashSet <int>();
                int i     = int.MinValue;
                while (DateTime.UtcNow < end)
                {
                    i++;
                    Assert.True(c.TryAdd(i));
                    added.Add(i);
                }
                return(added);
            });

            // Thread that adds and takes
            Task <KeyValuePair <HashSet <int>, HashSet <int> > > addsAndTakes = ThreadFactory.StartNew(() =>
            {
                var added = new HashSet <int>();
                var taken = new HashSet <int>();

                int i = 1; // avoid 0 as default(T), to detect accidentally reading a default value
                while (DateTime.UtcNow < end)
                {
                    i++;
                    Assert.True(c.TryAdd(i));
                    added.Add(i);

                    int item;
                    if (c.TryTake(out item))
                    {
                        Assert.NotEqual(0, item);
                        taken.Add(item);
                    }
                }

                return(new KeyValuePair <HashSet <int>, HashSet <int> >(added, taken));
            });

            // Thread that just takes
            Task <HashSet <int> > takes = ThreadFactory.StartNew(() =>
            {
                var taken = new HashSet <int>();
                while (DateTime.UtcNow < end)
                {
                    int item;
                    if (c.TryTake(out item))
                    {
                        Assert.NotEqual(0, item);
                        taken.Add(item);
                    }
                }
                return(taken);
            });

            // Wait for them all to finish
            WaitAllOrAnyFailed(adds, addsAndTakes, takes);

            // Combine everything they added and remove everything they took
            var total = new HashSet <int>(adds.Result);

            total.UnionWith(addsAndTakes.Result.Key);
            total.ExceptWith(addsAndTakes.Result.Value);
            total.ExceptWith(takes.Result);

            // What's left should match what's in the bag
            Assert.Equal(total.OrderBy(i => i), c.OrderBy(i => i));
        }