Beispiel #1
0
 public DynamicRouterTests()
 {
     _defaultTemplate = new Mock <ITemplate>();
     _recorder        = new Mock <IRecorder>();
     _normalizer      = new Mock <IUrlNormalizer>();
     _target          = new DynamicRouter(_defaultTemplate.Object, _recorder.Object, _normalizer.Object);
 }
        public static DynamicRouter MapHandler(this DynamicRouter dynaRouter, string mountTemplate,
                                               IRequestHandler handler)
        {
            mountTemplate = mountTemplate.TrimEnd('/');
            IRouteResolver resolver = handler as IRouteResolver;

            HandlerMappingDetail[] details;
            if (resolver != null)
            {
                details = resolver.ResolveRoutes().Select(rrt => new HandlerMappingDetail(
                                                              mountTemplate, rrt.Template, rrt.Constraint, rrt.DataTokens)).ToArray();
            }
            else
            {
                details = new[] { new HandlerMappingDetail(mountTemplate) };
            }

            foreach (var d in details)
            {
                object cons = null;
                if (d.Constraint != null)
                {
                    cons = new { theConstraint = d.Constraint }
                }
                ;

                dynaRouter.MapRoute(d.RouteTemplate, async context => {
                    context.Items[HandleMappingDetailKey] = d;
                    var result = await handler.HandleAsync(context);
                    await result.EvaluateAsync(context);
                }, constraints: cons, dataTokens: d.DataTokens);
            }

            return(dynaRouter);
        }
Beispiel #3
0
            public void ShouldRoute()
            {
                var documentItRouted = "document.it.routed";
                var commandRouteThis = "command.route.this";
                var consumedEvent = new AutoResetEvent(false);
                var storage = new InMemoryKeyValueStorage<ExpandoObject>();
                var router = new DynamicRouter<ExpandoObject>((m, s) => documentItRouted.ToMessageLabel(), storage);
                this.StartBus(
                    "pipeline", 
                    cfg =>
                        {
                            cfg.On(commandRouteThis)
                                .ReactWith(new PipelineConsumerOf<ExpandoObject>(router))
                                .RequiresAccept();
                            cfg.Route(documentItRouted);
                        });

                var producer = this.StartBus(
                    "producer", 
                    cfg => { cfg.Route(commandRouteThis); });

                this.StartBus(
                    "consumer", 
                    cfg =>
                        {
                            cfg.On(documentItRouted)
                                .ReactWith<ExpandoObject>((m, ctx) => { consumedEvent.Set(); });
                        });

                producer.Emit(commandRouteThis, new { });
                var consumed = consumedEvent.WaitOne(5000);
                consumed.Should()
                    .BeTrue("Должно быть получено сообщение перенаправленое роутером.");
            }
Beispiel #4
0
            public void ShouldChangeFilterCondition()
            {
                var documentItFiltered  = "document.it.fitered";
                var commandFilterThis   = "command.filter.this";
                var commandChangeFilter = "command.change.filter";
                var key             = "key";
                var switchCondition = 4;
                var callCount       = 0;
                var filteredEvent   = new AutoResetEvent(false);
                var consumedEvent   = new ManualResetEvent(false);
                var storage         = new InMemoryKeyValueStorage <ExpandoObject>();
                var filter          = new DynamicFilter <ExpandoObject>(
                    (m, s) =>
                {
                    dynamic data = s.Get(key);
                    filteredEvent.Set();
                    return(!(data == null || data.Count < switchCondition));
                },
                    storage);
                var control = new DynamicRouter <ExpandoObject> .DynamicRouterControlConsumer <ExpandoObject>(
                    (message, filterStorage) =>
                {
                    dynamic data = filterStorage.Get(key);
                    if (data == null)
                    {
                        dynamic item = new ExpandoObject();
                        item.Count   = 1;
                        filterStorage.Set(key, item);
                        filteredEvent.Set();
                        return;
                    }

                    data.Count++;
                    filterStorage.Set(key, data);
                    filteredEvent.Set();
                },
                    storage);

                this.StartBus(
                    "pipeline",
                    cfg =>
                {
                    cfg.On(commandFilterThis)
                    .ReactWith(new PipelineConsumerOf <ExpandoObject>(filter, new StaticRouter(documentItFiltered)))
                    .RequiresAccept();
                    cfg.On(commandChangeFilter)
                    .ReactWith(control);
                    cfg.Route(documentItFiltered);
                });

                var producer = this.StartBus(
                    "producer",
                    cfg =>
                {
                    cfg.Route(commandFilterThis);
                    cfg.Route(commandChangeFilter);
                });

                this.StartBus(
                    "consumer",
                    cfg =>
                {
                    cfg.On(documentItFiltered)
                    .ReactWith <ExpandoObject>(
                        (m, ctx) =>
                    {
                        callCount++;
                        consumedEvent.Set();
                    });
                });

                for (int i = 0; i <= switchCondition; i++)
                {
                    producer.Emit(commandFilterThis, new { });
                    filteredEvent.WaitOne();
                    producer.Emit(commandChangeFilter, new { });
                    filteredEvent.WaitOne();
                }

                consumedEvent.WaitOne().Should().BeTrue("Должно быть получено сообщение перенаправленое роутером.");
                callCount.Should().Be(1, "Сообщение должно быть получено только один раз.");
            }
Beispiel #5
0
            public void ShouldChangeRoute()
            {
                var documentItRouted   = "document.it.routed";
                var documentItDirected = "document.it.directed";
                var commandRouteThis   = "command.route.this";
                var commandChangeRoute = "command.change.route";
                var key             = "key";
                var switchCondition = 4;
                var consumedEvent   = new AutoResetEvent(false);
                var directedEvent   = new AutoResetEvent(false);
                var storage         = new InMemoryKeyValueStorage <ExpandoObject>();
                var router          = new DynamicRouter <ExpandoObject>(
                    (m, s) =>
                {
                    dynamic data = s.Get(key);
                    if (data == null || data.Count < switchCondition)
                    {
                        return(documentItRouted.ToMessageLabel());
                    }

                    return(documentItDirected.ToMessageLabel());
                },
                    storage);
                var control = new DynamicRouter <ExpandoObject> .DynamicRouterControlConsumer <ExpandoObject>(
                    (message, routeStorage) =>
                {
                    dynamic data = routeStorage.Get(key);
                    if (data == null)
                    {
                        dynamic item = new ExpandoObject();
                        item.Count   = 1;
                        routeStorage.Set(key, item);
                        directedEvent.Set();
                        return;
                    }

                    data.Count++;
                    routeStorage.Set(key, data);
                    directedEvent.Set();
                },
                    storage);

                this.StartBus(
                    "pipeline",
                    cfg =>
                {
                    cfg.On(commandRouteThis)
                    .ReactWith(new PipelineConsumerOf <ExpandoObject>(router))
                    .RequiresAccept();
                    cfg.On(commandChangeRoute)
                    .ReactWith(control);
                    cfg.Route(documentItRouted);
                    cfg.Route(documentItDirected);
                });

                var producer = this.StartBus(
                    "producer",
                    cfg => { cfg.Route(commandRouteThis); });

                this.StartBus(
                    "director",
                    cfg =>
                {
                    cfg.On(documentItRouted)
                    .ReactWith <ExpandoObject>(
                        (m, ctx) => { ctx.Bus.Emit(commandChangeRoute, new { }); });
                    cfg.Route(commandChangeRoute);
                });

                this.StartBus(
                    "consumer",
                    cfg =>
                {
                    cfg.On(documentItDirected)
                    .ReactWith <ExpandoObject>((m, ctx) => { consumedEvent.Set(); });
                });

                for (int i = 0; i <= switchCondition; i++)
                {
                    producer.Emit(commandRouteThis, new { });

                    if (i < switchCondition)
                    {
                        directedEvent.WaitOne(5000).Should().BeTrue("Сообщение должно быть обработано.");
                    }
                }

                consumedEvent.WaitOne(5000).Should().BeTrue("Должно быть получено сообщение перенаправленое роутером.");
            }