Example #1
0
        public async Task ConfigureClient_is_thread_safe()
        {
            var fac      = new PerHostFlurlClientFactory();
            var sequence = new List <int>();

            var task1 = Task.Run(() => fac.ConfigureClient("http://api.com", c => {
                sequence.Add(1);
                Thread.Sleep(5000);
                sequence.Add(3);
            }));

            await Task.Delay(200);

            // modifies same client as task1, should get blocked until task1 is done
            var task2 = Task.Run(() => fac.ConfigureClient("http://api.com", c => {
                sequence.Add(4);
            }));

            await Task.Delay(200);

            // modifies different client, should run immediately
            var task3 = Task.Run(() => fac.ConfigureClient("http://api2.com", c => {
                sequence.Add(2);
            }));

            await Task.WhenAll(task1, task2, task3);

            Assert.AreEqual("1,2,3,4", string.Join(",", sequence));
        }
Example #2
0
        public void ConfigureClient_is_thread_safe()
        {
            var fac = new PerHostFlurlClientFactory();

            var sequence = new List <int>();

            var task1 = Task.Run(() => fac.ConfigureClient("http://api.com", c => {
                sequence.Add(1);
                Thread.Sleep(200);
                sequence.Add(3);
            }));

            Thread.Sleep(50);

            // modifies same client as task1, should get blocked until task1 is done
            var task2 = Task.Run(() => fac.ConfigureClient("http://api.com", c => {
                sequence.Add(4);
            }));

            Thread.Sleep(50);

            // modifies different client, should run immediately
            var task3 = Task.Run(() => fac.ConfigureClient("http://api2.com", c => {
                sequence.Add(2);
            }));


            Task.WaitAll(task1, task2, task3);
            CollectionAssert.AreEqual(new[] { 1, 2, 3, 4 }, sequence);
        }
Example #3
0
        public void can_configure_client_from_factory()
        {
            var fac = new PerHostFlurlClientFactory()
                      .ConfigureClient("http://api.com/foo", c => c.Settings.Timeout = TimeSpan.FromSeconds(123));

            Assert.AreEqual(TimeSpan.FromSeconds(123), fac.Get("https://api.com/bar").Settings.Timeout);
            Assert.AreNotEqual(TimeSpan.FromSeconds(123), fac.Get("http://api2.com/foo").Settings.Timeout);
        }
Example #4
0
        public void per_host_factory_provides_same_client_per_host()
        {
            var fac  = new PerHostFlurlClientFactory();
            var cli1 = fac.Get("http://api.com/foo");
            var cli2 = fac.Get("https://api.com/bar");

            Assert.AreSame(cli1, cli2);
        }
Example #5
0
        public void can_configure_client_from_factory()
        {
            var fac = new PerHostFlurlClientFactory()
                      .ConfigureClient("http://api.com/foo", c => c.Settings.CookiesEnabled = true);

            Assert.IsTrue(fac.Get("https://api.com/bar").Settings.CookiesEnabled);
            Assert.IsFalse(fac.Get("http://api2.com/foo").Settings.CookiesEnabled);
        }