public void verify_bahaviour_for_concurrent_access_under_different_keys()
        {
            var keys    = new[] { "a", "b" };
            var counter = new ConcurrentStack <int>();      // value factory threads
            var storage = new ConcurrentStack <TestItem>(); // cached items

            // first run
            var threads = MakeThreads(keys);

            threads.ForEach(t => t.Start(new object[] { storage, counter }));
            threads.ForEach(t => t.Join());

            Assert.Equal(2, counter.Count);
            Assert.Equal(2, storage.Count);
            Assert.NotSame(storage.First(), storage.Last());
            var a = storage.FirstOrDefault(x => x.Id == "a");
            var b = storage.FirstOrDefault(x => x.Id == "b");

            // cleanups and second run
            storage.Clear();
            counter.Clear();

            threads = MakeThreads(keys);
            threads.ForEach(t => t.Start(new object[] { storage, counter }));
            threads.ForEach(t => t.Join());

            Assert.Equal(0, counter.Count);
            Assert.Equal(2, storage.Count);
            Assert.NotSame(storage.First(), storage.Last());
            var aa = storage.FirstOrDefault(x => x.Id == "a");
            var bb = storage.FirstOrDefault(x => x.Id == "b");

            Assert.Same(a, aa);
            Assert.Same(b, bb);
        }
        public void verify_bahaviour_for_concurrent_access_under_different_keys()
        {
            var keys = new[] {"a", "b"};
            var counter = new ConcurrentStack<int>(); // value factory threads
            var storage = new ConcurrentStack<TestItem>(); // cached items

            // first run
            var threads = MakeThreads(keys);
            threads.ForEach(t => t.Start(new object[] {storage, counter}));
            threads.ForEach(t => t.Join());

            Assert.Equal(2, counter.Count);
            Assert.Equal(2, storage.Count);
            Assert.NotSame(storage.First(), storage.Last());
            var a = storage.FirstOrDefault(x => x.Id == "a");
            var b = storage.FirstOrDefault(x => x.Id == "b");

            // cleanups and second run
            storage.Clear();
            counter.Clear();

            threads = MakeThreads(keys);
            threads.ForEach(t => t.Start(new object[] {storage, counter}));
            threads.ForEach(t => t.Join());

            Assert.Equal(0, counter.Count);
            Assert.Equal(2, storage.Count);
            Assert.NotSame(storage.First(), storage.Last());
            var aa = storage.FirstOrDefault(x => x.Id == "a");
            var bb = storage.FirstOrDefault(x => x.Id == "b");
            Assert.Same(a, aa);
            Assert.Same(b, bb);
        }
Exemple #3
0
        public void ExceptionsVsLinqSimpleTest()
        {
            var count = 100;

            // getting linqResult first to do the runtime optimization
            // before getting exceptionResult
            var linqResult = GenericUtil.Execute(
                () =>
            {
                // use concurrent collection in order not to use the fastest collection
                var errors = new ConcurrentStack <string>();
                errors.Push(GetStringRandom);
                var error = errors.FirstOrDefault();
                return(error);
            },
                count);

            var exceptionResult = GenericUtil.Execute(
                () => ThrowAndGetMessageBase(GetStringRandom),
                count);

            TestContext.WriteLine($@"{exceptionResult.TimeSpent} : time of {nameof(exceptionResult)}");
            TestContext.WriteLine($@"{linqResult.TimeSpent} : time of {nameof(linqResult)}");
            TestContext.WriteLine($@"getting {nameof(exceptionResult)} was {
                exceptionResult.TimeSpent / linqResult.TimeSpent
                } times slower than {nameof(linqResult)}");

            //AssertUtil.AssertGreater(exceptionResult.TimeSpent, linqResult.TimeSpent);
        }
        private IDiscriminatorConvention?InternalGetConvention(Type type)
        {
            IDiscriminatorConvention?convention = _conventions.FirstOrDefault(c => c.TryRegisterType(type));

            if (convention != null)
            {
                IObjectMapping objectMapping = _options.GetObjectMappingRegistry().Lookup(type);
                objectMapping.AddDiscriminatorMapping();

                // setup discriminator for all base types
                for (Type?currentType = type.BaseType; currentType != null && currentType != typeof(object); currentType = currentType.BaseType)
                {
                    objectMapping = _options.GetObjectMappingRegistry().Lookup(currentType);
                    objectMapping.AddDiscriminatorMapping();
                    _conventionsByType.TryAdd(currentType, convention);
                }

                // setup discriminator for all interfaces
                foreach (Type @interface in type.GetInterfaces())
                {
                    _conventionsByType.TryAdd(@interface, convention);
                }
            }

            return(convention);
        }
 public bool Contains(Predicate <T> predicate)
 {
     if (queue != null)
     {
         return(queue.FirstOrDefault(x => predicate(x)) != null);
     }
     else
     {
         return(stack.FirstOrDefault(x => predicate(x)) != null);
     }
 }
Exemple #6
0
        private void ReplayResponse(ITestableHttpRequest request, IMockHttpResponse response)
        {
            var expectation = _expectations.FirstOrDefault(e => e.Match(request));

            if (expectation != null)
            {
                Replay(response, expectation.PrepareForReplay(request, _repository));
            }
            else
            {
                ReplayNoMapping(response);
            }
        }
Exemple #7
0
        private IDiscriminatorConvention?InternalGetConvention(Type type)
        {
            IDiscriminatorConvention?convention = _conventions.FirstOrDefault(c => c.TryRegisterType(type));

            if (convention != null)
            {
                // setup discriminator for all base types
                for (Type?currentType = type.BaseType; currentType != null && currentType != typeof(object); currentType = currentType.BaseType)
                {
                    _conventionsByType.TryAdd(currentType, convention);
                }
            }

            return(convention);
        }
Exemple #8
0
        public T this[int index]
        {
            get
            {
                int pageIndex  = index / PageSize;
                int pageOffset = index % PageSize;

                var page = _pageStack.FirstOrDefault(x => x.Idx == pageIndex);

                if (page == null)
                {
                    page = new Page <T>();
                    _pageStack.Push(page);
                }

                var res = page.GetItem(pageOffset);

                return(res);
            }
            set => throw new System.NotImplementedException();
        private object Create(Type type)
        {
            var configuratedType = container.GetConfiguratedType(type);

            if (configuratedType != null)
            {
                if (!stack.Contains(configuratedType.GetImplementationInterface))
                {
                    stack.Push(configuratedType.GetImplementationInterface);

                    var instanceType = configuratedType.GetImplementation;
                    if (instanceType.IsGenericTypeDefinition)
                    {
                        instanceType = instanceType.MakeGenericType(currentType.GenericTypeArguments);
                    }

                    var constructors = instanceType.GetConstructors().OrderByDescending
                                           (x => x.GetParameters().Length).ToArray();

                    bool   isCreated         = false;
                    int    constructorNumber = 1;
                    object result            = null;

                    while (!isCreated && constructorNumber <= constructors.Count())
                    {
                        try
                        {
                            var useConstructor = constructors[constructorNumber - 1];
                            var param          = GetConstructorParam(useConstructor);
                            result    = Activator.CreateInstance(instanceType, param);
                            isCreated = true;
                        }
                        catch
                        {
                            isCreated = false;
                            constructorNumber++;
                        }
                    }

                    if (!stack.TryPop(out var temp) || temp != configuratedType.GetImplementationInterface)
                    {
                        throw new Exception("can't correctly pop element from stack");
                    }

                    if (isCreated)
                    {
                        return(result);
                    }
                    else
                    {
                        throw new Exception($"{type.FullName} can't be created");
                    }
                }
                else
                {
                    throw new Exception($"can't resolve type{stack.FirstOrDefault().Name}");
                }
            }
            else
            {
                throw new Exception($"{type.FullName} is not registered");
            }
        }