Esempio n. 1
0
        public async Task TestProxy_ByClass_AsyncVoidMethod_AsyncPolicy()
        {
            var policy = Policy.Handle <ArgumentException>().RetryAsync(1);
            var cli    = new ClientTest();
            var proxy  = PollyProxy <ClientTest> .Create(cli, config => config
                                                         .When(mi => mi.Name == "FailFirstVoidAsync", policy));

            await proxy.FailFirstVoidAsync("y");

            Assert.AreEqual(false, cli._fail);
        }
Esempio n. 2
0
        public async Task TestProxy_ByInterface_AsyncVoidMethod_AsyncPolicy()
        {
            var policy = Policy.Handle <ArgumentException>().RetryAsync(1);
            var cli    = new ClientTest();
            var proxy  = PollyProxy <IClientTest> .Create(cli, config => config
                                                          .For("NotVirtualFailFirstVoidAsync", policy));

            await proxy.NotVirtualFailFirstVoidAsync("y");

            Assert.IsFalse(cli._fail);
        }
Esempio n. 3
0
        public async Task TestProxy_NoDefault_AsyncVoidMethod()
        {
            var policy   = Policy.Handle <ArgumentException>().RetryAsync(1);
            var policyNA = Policy.Handle <NullReferenceException>().RetryAsync(2);
            var cli      = new ClientTest();
            var proxy    = PollyProxy <IClientTest> .Create(cli, config => config
                                                            .For("XXXX", policyNA)
                                                            .Default(policy));

            await proxy.NotVirtualFailFirstVoidAsync("y");

            Assert.AreEqual(false, cli._fail);
        }
Esempio n. 4
0
        public void TestProxy_NotMatchingPolicy()
        {
            var policy = Policy.Handle <ArgumentException>().Retry(3);
            var cli    = new ClientTest();
            var proxy  = PollyProxy <IClientTest> .Create(cli, config => config
                                                          .Map(mi => mi.Name == "XXXXX" ? policy : null)
                                                          .When(mi => mi.GetCustomAttribute <IgnoreAttribute>() != null, policy)
                                                          .For("XX", policy)
                                                          .Default(null));

            Assert.Throws <ArgumentException>(() =>
            {
                var ret2 = proxy.NotVirtualFailFirst("z");  // should not retry because no matching policy
            });
        }
Esempio n. 5
0
        public async Task TestProxy_AsyncBy_AsyncStateMachineAttribute()
        {
            var policy = Policy.Handle <ArgumentException>().RetryAsync(1);
            var cli    = new ClientTest();
            var proxy  = PollyProxy <ClientTest> .Create(cli, config => config
                                                         .When(mi => mi.GetCustomAttribute <AsyncStateMachineAttribute>() != null, policy));

            var ret = await proxy.FailFirstAsync("y");

            cli._fail = true;
            await proxy.FailFirstVoidAsync("y");

            cli._fail = true;
            Assert.Throws <ArgumentException>(() =>
            {
                proxy.FailFirst("y");
            });
            Assert.AreEqual("y", ret);
        }
Esempio n. 6
0
        public async Task TestProxy_MapRule()
        {
            var policy   = Policy.Handle <ArgumentException>().RetryAsync(1);
            var policyNA = Policy.Handle <NullReferenceException>().RetryAsync(2);
            var cli      = new ClientTest();
            var proxy    = PollyProxy <IClientTest> .Create(cli, config => config
                                                            .Map(mi => mi.Name == "NotVirtualFailFirstAsync" ? policy : null)
                                                            .Default(policyNA));

            var ret = await proxy.NotVirtualFailFirstAsync("y");

            Assert.IsFalse(cli._fail);
            cli._fail = true;
            Assert.Throws <ArgumentException>(() =>
            {
                var ret2 = proxy.NotVirtualFailFirst("z");  // should not retry because of handling wrong exception
            });
            Assert.AreEqual("y", ret);
        }