public async Task InvalidAffinity(string affinityKey)
        {
            ComplexRequest request = new ComplexRequest
            {
                Name   = "name",
                Number = 10,
                Inner  = new ComplexInner
                {
                    Name        = "name",
                    Number      = 10,
                    NestedInner = new ComplexInner {
                        NestedInner = new ComplexInner()
                    }
                }
            };

            var config = CreateApiConfig(affinityKey, Command.Bind);

            await RunWithServer(
                new CopyService(),
                config,
                async (invoker, client) =>
                await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await client.DoComplexAsync(request)),
                (invoker, client) =>
                Assert.ThrowsException <InvalidOperationException>(() => client.DoComplex(request)));
        }
        public async Task NullOrEmptyAffinity_Unbind(string affinityKey, ComplexRequest request)
        {
            var config = CreateApiConfig(affinityKey, Command.Unbind);

            await RunWithServer(
                new CopyService(),
                config,
                async (invoker, client) =>
            {
                await client.DoComplexAsync(request);
                AssertNoAffinity(invoker);
            },
                (invoker, client) =>
            {
                client.DoComplex(request);
                AssertNoAffinity(invoker);
            });

            void AssertNoAffinity(GcpCallInvoker invoker)
            {
                var channelRefs = invoker.GetChannelRefsForTest();

                Assert.AreEqual(0, channelRefs.Sum(cr => cr.ActiveStreamCount));
            }
        }
 public override async Task <ComplexResponse> DoComplex(ComplexRequest request, ServerCallContext context) =>
 new ComplexResponse
 {
     Name   = request.Name,
     Number = request.Number,
     Inner  = request.Inner
 };
Example #4
0
        public async Task BatchAffinity_Unbind(string affinityKey, ComplexRequest request)
        {
            var service      = new CopyService();
            var bindConfig   = CreateApiConfig(affinityKey, Command.Bind);
            var unbindConfig = CreateApiConfig(affinityKey, Command.Unbind);

            // First we need to Bind.
            // Bind works, we test elsewhere.
            Bind();

            // Test for Unbind async
            await RunWithServer(
                service,
                unbindConfig,
                async (invoker, client) =>
            {
                await client.DoComplexAsync(request);
                AssertNoAffinity(invoker);
            },
                (invoker, client) =>
            {
                // No op, we are testing unbind async here.
                // Can't unbind twice.
            });

            // Bind again so we can test sync unbind
            Bind();

            // Test for Unbind sync
            await RunWithServer(
                service,
                unbindConfig,
                async (invoker, client) =>
            {
                // No op, we are testing unbind sync here.
                // Can't unbind twice.
                await Task.FromResult(0);
            },
                (invoker, client) =>
            {
                client.DoComplex(request);
                AssertNoAffinity(invoker);
            });

            async void Bind()
            {
                await RunWithServer(
                    service,
                    bindConfig,
                    async (invoker, client) =>
                {
                    await client.DoComplexAsync(request);
                },
                    (invoker, client) =>
                {
                    // No op, we are just binding to test unbind after.
                    // We are not testing Bind here.
                });
            }
        }
Example #5
0
        public async Task BatchAffinity_Bound(string affinityKey, ComplexRequest request)
        {
            var config = CreateApiConfig(affinityKey, Command.Bound);

            await RunWithServer(
                new CopyService(),
                config,
                async (invoker, client) =>
                await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await client.DoComplexAsync(request)),
                (invoker, client) =>
                Assert.ThrowsException <InvalidOperationException>(() => client.DoComplex(request)));
        }
Example #6
0
        public async Task NullOrEmptyAffinity(string affinityKey, ComplexRequest request, Command command)
        {
            var config = CreateApiConfig(affinityKey, command);

            await RunWithServer(
                new CopyService(),
                config,
                async (invoker, client) =>
            {
                await client.DoComplexAsync(request);
                AssertNoAffinity(invoker);
            },
                (invoker, client) =>
            {
                client.DoComplex(request);
                AssertNoAffinity(invoker);
            });
        }
Example #7
0
        public async Task BatchAffinity_Bind(string affinityKey, ComplexRequest request)
        {
            var config = CreateApiConfig(affinityKey, Command.Bind);

            await RunWithServer(
                new CopyService(),
                config,
                async (invoker, client) =>
            {
                await client.DoComplexAsync(request);
                AssertBatchAffinity(invoker, 4);
            },
                (invoker, client) =>
            {
                client.DoComplex(request);
                // Testing here with 8 due to how the tests are set up.
                // We use the same test server to test identical sync and async calls.
                // In relation to affinity and batch affinity binding, two calls will probably never
                // be the same in reality, because binding should occur on a unique identifier for the
                // object that is being bound to the channel, on creation of said object.
                // There's a TODO on the CallInvoker code to check that this actually never happens.
                // In the meantime, it's not even a bad thing to have these tests as are and this explanation here.
                AssertBatchAffinity(invoker, 8);
            });

            void AssertBatchAffinity(GcpCallInvoker invoker, int expectedAffinityCount)
            {
                var channelRefs = invoker.GetChannelRefsForTest();

                Assert.AreEqual(expectedAffinityCount, channelRefs.Sum(cr => cr.AffinityCount));

                var affinityKeys = invoker.GetChannelRefsByAffinityKeyForTest().Keys;

                Assert.AreEqual(4, affinityKeys.Count);
                Assert.IsTrue(affinityKeys.Contains("nested1"));
                Assert.IsTrue(affinityKeys.Contains("nested2"));
                Assert.IsTrue(affinityKeys.Contains("nested3"));
                Assert.IsTrue(affinityKeys.Contains("nested4"));
            }
        }
 public override async Task <ComplexResponse> DoComplex(ComplexRequest request, ServerCallContext context) =>
 throw new Exception("Bang");