public void When_multiple_dynamic_instances_for_local_endpoint_should_round_robin()
        {
            var endpointInstances = new EndpointInstances();

            endpointInstances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance("Endpoint", "1"),
                new EndpointInstance("Endpoint", "2"),
            });

            var options = new SendOptions();

            options.RouteToThisEndpoint();

            var context = CreateContext(options, new MyMessage());

            var router = CreateRouter(instances: endpointInstances);

            var route1 = router.Route(context);
            var route2 = router.Route(context);
            var route3 = router.Route(context);

            Assert.AreEqual("Endpoint-1", ExtractDestination(route1));
            Assert.AreEqual("Endpoint-2", ExtractDestination(route2));
            Assert.AreEqual("Endpoint-1", ExtractDestination(route3));
        }
    async Task UpdateCaches(EndpointInstance instanceName, Type[] handledTypes, Type[] publishedTypes)
    {
        var newInstanceMap  = BuildNewInstanceMap(instanceName);
        var newEndpointMap  = BuildNewEndpointMap(instanceName.Endpoint, handledTypes, endpointMap);
        var newPublisherMap = BuildNewPublisherMap(instanceName, publishedTypes, publisherMap);

        LogChangesToEndpointMap(endpointMap, newEndpointMap);
        LogChangesToInstanceMap(instanceMap, newInstanceMap);
        var toSubscribe = LogChangesToPublisherMap(publisherMap, newPublisherMap).ToArray();

        #region AddOrReplace

        routingTable.AddOrReplaceRoutes("AutomaticRouting", newEndpointMap.Select(
                                            x => new RouteTableEntry(x.Key, UnicastRoute.CreateFromEndpointName(x.Value))).ToList());

        publishers.AddOrReplacePublishers("AutomaticRouting", newPublisherMap.Select(
                                              x => new PublisherTableEntry(x.Key, PublisherAddress.CreateFromEndpointName(x.Value))).ToList());

        endpointInstances.AddOrReplaceInstances("AutomaticRouting", newInstanceMap.SelectMany(x => x.Value).ToList());

        #endregion

        instanceMap  = newInstanceMap;
        endpointMap  = newEndpointMap;
        publisherMap = newPublisherMap;

        foreach (var type in toSubscribe.Intersect(messageTypesHandledByThisEndpoint))
        {
            await messageSession.Subscribe(type)
            .ConfigureAwait(false);
        }
    }
        public void When_multiple_dynamic_instances_for_local_endpoint_and_instance_selected_should_not_round_robin()
        {
            var routingTable = new UnicastRoutingTable();

            routingTable.AddOrReplaceRoutes("A", new List <RouteTableEntry>
            {
                new RouteTableEntry(typeof(MyMessage), UnicastRoute.CreateFromEndpointName("Endpoint"))
            });

            var endpointInstances = new EndpointInstances();

            endpointInstances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance("Endpoint", "1"),
                new EndpointInstance("Endpoint", "2"),
            });

            var options = new SendOptions();

            options.RouteToSpecificInstance("2");

            var context = CreateContext(options, new MyMessage());

            var router = CreateRouter(routingTable: routingTable, instances: endpointInstances);
            var route1 = router.Route(context);
            var route2 = router.Route(context);
            var route3 = router.Route(context);

            Assert.AreEqual("Endpoint-2", ExtractDestination(route1));
            Assert.AreEqual("Endpoint-2", ExtractDestination(route2));
            Assert.AreEqual("Endpoint-2", ExtractDestination(route3));
        }
        public void When_multiple_dynamic_instances_for_logical_endpoints_should_round_robin()
        {
            var sales        = "Sales";
            var routingTable = new UnicastRoutingTable();

            routingTable.AddOrReplaceRoutes("A", new List <RouteTableEntry>
            {
                new RouteTableEntry(typeof(MyMessage), UnicastRoute.CreateFromEndpointName(sales))
            });

            var endpointInstances = new EndpointInstances();

            endpointInstances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance(sales, "1"),
                new EndpointInstance(sales, "2"),
            });

            var context = CreateContext(new SendOptions(), new MyMessage());

            var router = CreateRouter(routingTable: routingTable, instances: endpointInstances);
            var route1 = router.Route(context);
            var route2 = router.Route(context);
            var route3 = router.Route(context);

            Assert.AreEqual("Sales-1", ExtractDestination(route1));
            Assert.AreEqual("Sales-2", ExtractDestination(route2));
            Assert.AreEqual("Sales-1", ExtractDestination(route3));
        }
        public void When_routing_to_specific_instance_should_select_appropriate_instance()
        {
            var table     = new UnicastRoutingTable();
            var instances = new EndpointInstances();

            table.AddOrReplaceRoutes("A", new List <RouteTableEntry>
            {
                new RouteTableEntry(typeof(MyMessage), UnicastRoute.CreateFromEndpointName("Endpoint"))
            });
            instances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance("Endpoint", "1"),
                new EndpointInstance("Endpoint", "2"),
                new EndpointInstance("Endpoint", "3")
            });
            var router  = CreateRouter(routingTable: table, instances: instances);
            var options = new SendOptions();

            options.RouteToSpecificInstance("2");

            var context = CreateContext(options);

            var route = router.Route(context);

            Assert.AreEqual("Endpoint-2", ExtractDestination(route));
        }
        public void Should_filter_out_duplicate_instances()
        {
            var instances = new EndpointInstances();
            var sales     = "Sales";

            instances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance(sales, "dup"),
                new EndpointInstance(sales, "dup")
            });

            var salesInstances = instances.FindInstances(sales);

            Assert.AreEqual(1, salesInstances.Count());
        }
        public void Should_return_instances_configured_by_static_route()
        {
            var instances = new EndpointInstances();
            var sales     = "Sales";

            instances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance(sales, "1"),
                new EndpointInstance(sales, "2")
            });

            var salesInstances = instances.FindInstances(sales);

            Assert.AreEqual(2, salesInstances.Count());
        }
        private void PopulateInstances(EndpointInstances instances, KeyValuePair <string, string> address)
        {
            var partitionedEndpoints = new List <EndpointInstance>();

            var serviceName = new Uri(address.Value);

            using (var client = new FabricClient())
            {
                var partitions = client.QueryManager.GetPartitionListAsync(serviceName).GetAwaiter().GetResult();

                foreach (var partition in partitions)
                {
                    partitionedEndpoints.Add(new EndpointInstance(address.Key, partition.PartitionInformation.Id.ToString()));
                }
            }

            instances.AddOrReplaceInstances(address.Value, partitionedEndpoints);
        }
        public void Should_add_instances_grouped_by_endpoint_name()
        {
            var          instances     = new EndpointInstances();
            const string endpointName1 = "EndpointA";
            const string endpointName2 = "EndpointB";

            instances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance(endpointName1),
                new EndpointInstance(endpointName2)
            });

            var salesInstances = instances.FindInstances(endpointName1);

            Assert.AreEqual(1, salesInstances.Count());

            var otherInstances = instances.FindInstances(endpointName2);

            Assert.AreEqual(1, otherInstances.Count());
        }