public async Task AfterCreating_is_threadsafe()
        {
            var container = new PocketContainer()
                            .Register <IList <string> >(c => new List <string>());

            var barrier = new Barrier(2);

            var task1 = Task.Run(() =>
            {
                barrier.SignalAndWait();
                container.AfterCreating <IList <string> >(list =>
                {
                    list.Add("one");
                });
            });

            var task2 = Task.Run(() =>
            {
                barrier.SignalAndWait();
                container.AfterCreating <IList <string> >(list =>
                {
                    list.Add("two");
                });
            });

            await Task.WhenAll(task1, task2);

            container.Resolve <IList <string> >()
            .Should()
            .Contain("one")
            .And
            .Contain("two");
        }
Exemple #2
0
        public TestTargetRegistry Add(
            string environment,
            string application,
            Uri baseAddress,
            Action <TestDependencyRegistry> testDependencies = null)
        {
            if (baseAddress == null)
            {
                throw new ArgumentNullException(nameof(baseAddress));
            }
            if (!baseAddress.IsAbsoluteUri)
            {
                throw new ArgumentException("Base address must be an absolute URI.");
            }
            if (string.IsNullOrWhiteSpace(environment))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(environment));
            }
            if (string.IsNullOrWhiteSpace(application))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(application));
            }

            var container = new PocketContainer()
                            .Register(c => new HttpClient())
                            .RegisterSingle(c => new TestTarget(c.Resolve)
            {
                Application = application,
                Environment = environment,
                BaseAddress = baseAddress
            });

            if (services != null)
            {
                // fall back to application's IServiceProvider
                container.AddStrategy(type =>
                {
                    if (typeof(IPeakyTest).IsAssignableFrom(type))
                    {
                        return(null);
                    }

                    return(c => services.GetRequiredService(type));
                });
            }

            testDependencies?.Invoke(new TestDependencyRegistry((t, func) => container.Register(t, c => func())));

            container.AfterCreating <HttpClient>(client =>
            {
                if (client.BaseAddress == null)
                {
                    client.BaseAddress = baseAddress;
                }
                return(client);
            });

            targets.Add($"{environment}:{application}",
                        new Lazy <TestTarget>(() => container.Resolve <TestTarget>()));

            return(this);
        }