Esempio n. 1
0
        public async Task OnSchedule_can_be_used_to_configure_scheduler_middleware_for_a_specific_command_type()
        {
            var scheduled = new List <string>();

            using (var configuration = new Configuration()
                                       .UseInMemoryScheduling())
            {
                configuration.OnSchedule <string>(async(delivery, next) =>
                {
                    scheduled.Add(delivery.Command);
                    await next(delivery);
                });

                using (var clock = VirtualClock.Start())
                {
                    clock.Repeat(async c =>
                    {
                        await configuration.CommandScheduler <string>().Schedule(c.Now().ToString());
                    }, () => 1.Seconds());

                    var handler = CommandHandler.Create <string>(delivery =>
                    {
                    });

                    configuration.CommandReceiver <string>().Subscribe(handler);

                    await Clock.Current.Wait(1.Minutes());
                }
            }

            scheduled.Count.Should().Be(60);
        }
Esempio n. 2
0
        public void SetUp()
        {
            disposables = new CompositeDisposable
            {
                VirtualClock.Start()
            };

            clockName = Any.CamelCaseName();
            targetId  = Any.Word();
            target    = new CommandTarget(targetId);
            store     = new InMemoryStore <CommandTarget>(
                _ => _.Id,
                id => new CommandTarget(id))
            {
                target
            };

            CommandSchedulerDbContext.NameOrConnectionString =
                @"Data Source=(localdb)\MSSQLLocalDB; Integrated Security=True; MultipleActiveResultSets=False; Initial Catalog=ItsCqrsTestsCommandScheduler";

            configuration = new Configuration()
                            .UseInMemoryCommandScheduling()
                            .UseDependency <IStore <CommandTarget> >(_ => store)
                            .UseDependency <GetClockName>(c => _ => clockName)
                            .TraceScheduledCommands();

            scheduler = configuration.CommandScheduler <CommandTarget>();

            Command <CommandTarget> .AuthorizeDefault = (commandTarget, command) => true;

            disposables.Add(ConfigurationContext.Establish(configuration));
            disposables.Add(configuration);
        }
        public void SetUp()
        {
            disposables = new CompositeDisposable();
            // disable authorization
            Command <Order> .AuthorizeDefault           = (o, c) => true;
            Command <CustomerAccount> .AuthorizeDefault = (o, c) => true;

            disposables.Add(VirtualClock.Start());

            customerAccountId = Any.Guid();

            configuration = new Configuration()
                            .UseInMemoryCommandScheduling()
                            .UseInMemoryEventStore()
                            .TraceScheduledCommands();

            customerRepository = configuration.Repository <CustomerAccount>();
            orderRepository    = configuration.Repository <Order>();

            customerRepository.Save(new CustomerAccount(customerAccountId)
                                    .Apply(new ChangeEmailAddress(Any.Email())));

            disposables.Add(ConfigurationContext.Establish(configuration));
            disposables.Add(configuration);
        }
Esempio n. 4
0
        public async Task <IActionResult> SignatureHelp(
            [FromBody] WorkspaceRequest request,
            [FromHeader(Name = "Timeout")] string timeoutInMilliseconds = "15000")
        {
            if (Debugger.IsAttached && !(Clock.Current is VirtualClock))
            {
                _disposables.Add(VirtualClock.Start());
            }

            using (var operation = Log.OnEnterAndConfirmOnExit())
            {
                operation.Info("Processing workspaceType {workspaceType}", request.Workspace.WorkspaceType);
                if (!int.TryParse(timeoutInMilliseconds, out var timeoutMs))
                {
                    return(BadRequest());
                }

                var runTimeout = TimeSpan.FromMilliseconds(timeoutMs);
                var budget     = new TimeBudget(runTimeout);
                var server     = GetServerForWorkspace(request.Workspace);
                var result     = await server.GetSignatureHelp(request, budget);

                budget.RecordEntry();
                operation.Succeed();

                return(Ok(result));
            }
        }
        public async Task test_with_commandHandler()
        {
            using (var clock = VirtualClock.Start())
            {
                var processed = new List <int>();
                var cfg       = new Configuration();
                cfg = cfg
                      .TraceCommands()
                      .UseDependency <ICircuitBreakerBroker>(type => new InMemoryCircuitBreakerBroker())
                      .UseInMemoryScheduling()
                      .UseHandlerDiscovery()
                      .UseCircuitBreaker <TestCommand>("TestCircuitBreaker");

                var scheduler = cfg.CommandScheduler <TestCommand>();
                await scheduler.Schedule(new TestCommand(1, processed), 1.Seconds());

                await scheduler.Schedule(new TestCommand(2, processed), 2.Seconds());

                await scheduler.Schedule(new TestCommand(11, processed), 3.Seconds());

                await scheduler.Schedule(new TestCommand(3, processed), 4.Seconds());

                await clock.AdvanceBy(5.Seconds());

                processed.Should().BeEquivalentTo(1, 2);

                await clock.AdvanceBy(6.Seconds());

                processed.Should().BeEquivalentTo(1, 2, 3);
            }
        }
Esempio n. 6
0
        public async Task When_ServiceBusCommandQueueSender_is_subscribed_to_the_service_bus_then_messages_are_scheduled_to_trigger_event_based_scheduled_commands()
        {
            VirtualClock.Start(DateTimeOffset.Now.AddHours(-13));

            using (var queueReceiver = CreateQueueReceiver())
            {
                var aggregateIds = Enumerable.Range(1, 5)
                                   .Select(_ => Guid.NewGuid())
                                   .ToArray();

                aggregateIds.ForEach(async id =>
                {
                    var order = CommandSchedulingTests_EventSourced.CreateOrder(orderId: id);

                    // due enough in the future that the scheduler won't apply the commands immediately
                    var due = Clock.Now().AddSeconds(5);
                    order.Apply(new ShipOn(due));

                    Console.WriteLine(new { ShipOrderId = order.Id, due });

                    await Configuration.Current.Repository <Order>().Save(order);
                });

                // reset the clock so that when the messages are delivered, the target commands are now due
                Clock.Reset();

                await queueReceiver.StartReceivingMessages();

                schedulerActivity
                .Select(a => Guid.Parse(a.TargetId))
                .ShouldBeEquivalentTo(aggregateIds);
            }
        }
Esempio n. 7
0
        public async Task When_ServiceBusCommandQueueSender_is_subscribed_to_the_service_bus_then_messages_are_scheduled_to_trigger_directly_scheduled_commands()
        {
            VirtualClock.Start(DateTimeOffset.Now.AddHours(-13));

            using (var queueReceiver = CreateQueueReceiver())
            {
                var aggregateIds = Enumerable.Range(1, 5)
                                   .Select(_ => Guid.NewGuid())
                                   .ToArray();

                aggregateIds.ForEach(id =>
                {
                    // TODO: (When_ServiceBusCommandQueueSender_is_subscribed_to_the_service_bus_then_messages_are_scheduled_to_trigger_directly_scheduled_commands)
                });

                // reset the clock so that when the messages are delivered, the target commands are now due
                Clock.Reset();

                await queueReceiver.StartReceivingMessages();

                schedulerActivity
                .Select(a => Guid.Parse(a.TargetId))
                .ShouldBeEquivalentTo(aggregateIds);
            }
        }
Esempio n. 8
0
        public async Task When_ServiceBusCommandQueueSender_is_subscribed_to_the_service_bus_then_messages_are_scheduled_to_trigger_directly_scheduled_commands()
        {
            VirtualClock.Start(DateTimeOffset.Now.AddHours(-13));

            using (var queueReceiver = CreateQueueReceiver())
            {
                var aggregateIds = Enumerable.Range(1, 5)
                                   .Select(_ => Guid.NewGuid())
                                   .ToArray();

                aggregateIds.ForEach(id =>
                {
                    // FIX: (When_ServiceBusCommandQueueSender_is_subscribed_to_the_service_bus_then_messages_are_scheduled_to_trigger_directly_scheduled_commands)
                });

                await RunCatchup();

                // reset the clock so that when the messages are delivered, the target commands are now due
                Clock.Reset();

                await queueReceiver.StartReceivingMessages();

                var activity = await scheduler.Activity
                               .Where(a => aggregateIds.Contains(a.ScheduledCommand.AggregateId))
                               .Take(5)
                               .ToList()
                               .Timeout(TimeSpan.FromMinutes(5));

                activity.Select(a => a.ScheduledCommand.AggregateId)
                .ShouldBeEquivalentTo(aggregateIds);
            }
        }
Esempio n. 9
0
 public void When_dates_are_not_specified_then_original_due_time_is_set_to_Clock_Now()
 {
     using (VirtualClock.Start())
     {
         new CommandDelivery <string>("").OriginalDueTime.Should().Be(Clock.Now());
     }
 }
Esempio n. 10
0
 public override void OnActionExecuting(ActionExecutingContext context)
 {
     if (Debugger.IsAttached && !(Clock.Current is VirtualClock))
     {
         _disposables.Add(VirtualClock.Start());
     }
 }
Esempio n. 11
0
        public async Task OnHandle_can_be_used_to_configure_scheduler_middleware_for_a_specific_command_type()
        {
            var handled = new List <CreateCommandTarget>();

            using (var configuration = new Configuration()
                                       .UseHandlerDiscovery()
                                       .UseDependency <IStore <CommandTarget> >(_ => new InMemoryStore <CommandTarget>())
                                       .UseInMemoryScheduling())
            {
                configuration.OnHandle <CreateCommandTarget>(async(delivery, next) =>
                {
                    handled.Add(delivery.Command);
                    return(await next(delivery));
                });

                using (var clock = VirtualClock.Start())
                {
                    clock.Repeat(async c =>
                    {
                        await configuration.CommandScheduler <CreateCommandTarget>()
                        .Schedule(new CreateCommandTarget(Guid.NewGuid().ToString()));
                    }, () => 1.Seconds());

                    // an extra tick is required to get to 60 because the 60th command is dispatched at the 1 minute mark
                    await Clock.Current.Wait(1.Minutes() + 1.Ticks());
                }
            }

            handled.Count.Should().Be(60);
        }
Esempio n. 12
0
        public async Task Scheduled_commands_in_initial_events_are_executed_if_they_become_due_after_Prepare_is_called()
        {
            using (VirtualClock.Start())
            {
                var customerAccountId = Any.Guid();
                var scenario          = CreateScenarioBuilder()
                                        .AddEvents(
                    new CustomerAccount.Created
                {
                    AggregateId = customerAccountId
                },
                    new Order.Created
                {
                    CustomerName = Any.FullName(),
                    OrderNumber  = "42",
                    CustomerId   = customerAccountId
                },
                    new CommandScheduled <Order>
                {
                    Command = new Cancel(),
                    DueTime = Clock.Now().AddDays(102)
                }).Prepare();

                scenario.AdvanceClockBy(TimeSpan.FromDays(103));

                (await scenario.GetLatestAsync <Order>())
                .EventHistory
                .Last()
                .Should()
                .BeOfType <Order.Cancelled>();
            }
        }
Esempio n. 13
0
        public void SetUp()
        {
            disposables = new CompositeDisposable
            {
                VirtualClock.Start()
            };

            clockName = Any.CamelCaseName();
            targetId  = Any.Word();
            target    = new CommandTarget(targetId);
            store     = new InMemoryStore <CommandTarget>(
                _ => _.Id,
                id => new CommandTarget(id))
            {
                target
            };

            configuration = new Configuration()
                            .UseInMemoryCommandScheduling()
                            .UseDependency <IStore <CommandTarget> >(_ => store)
                            .UseDependency <GetClockName>(c => _ => clockName)
                            .TraceScheduledCommands();

            scheduler = configuration.CommandScheduler <CommandTarget>();

            Command <CommandTarget> .AuthorizeDefault = (commandTarget, command) => true;

            disposables.Add(ConfigurationContext.Establish(configuration));
            disposables.Add(configuration);
        }
Esempio n. 14
0
        public async Task When_the_VirtualClock_is_advanced_past_a_commands_due_time_then_in_EnactCommand_ClockNow_returns_the_commands_due_time()
        {
            var dueTime = DateTimeOffset.Parse("2019-09-01 +00:00");

            VirtualClock.Start(DateTimeOffset.Parse("2019-01-01 +00:00"));

            var target        = new NonEventSourcedCommandTarget(Any.CamelCaseName());
            var configuration = Configuration.Current;
            await configuration.Store <NonEventSourcedCommandTarget>().Put(target);

            var clockNowAtCommandDeliveryTime = default(DateTimeOffset);

            configuration.UseCommandHandler <NonEventSourcedCommandTarget, TestCommand>(
                enactCommand: async(_, __) =>
            {
                clockNowAtCommandDeliveryTime = Clock.Now();
            });

            var scheduler = configuration.CommandScheduler <NonEventSourcedCommandTarget>();

            await scheduler.Schedule(target.Id,
                                     new TestCommand(),
                                     dueTime : dueTime);

            VirtualClock.Current.AdvanceBy(365.Days());

            clockNowAtCommandDeliveryTime.Should().Be(dueTime);
        }
Esempio n. 15
0
        public async Task Advancing_the_clock_blocks_until_triggered_commands_on_the_InMemoryCommandScheduler_are_completed()
        {
            VirtualClock.Start();

            var repository = new InMemoryEventSourcedRepository <Order>();

            var scheduler = new InMemoryCommandScheduler <Order>(repository);

            var aggregateId = Any.Guid();
            await scheduler.Schedule(new CommandScheduled <Order>
            {
                Command = new CreateOrder(Any.FullName())
                {
                    AggregateId = aggregateId
                },
                DueTime     = Clock.Now().AddHours(1),
                AggregateId = aggregateId
            });

            VirtualClock.Current.AdvanceBy(TimeSpan.FromDays(1));

            var order = await repository.GetLatest(aggregateId);

            order.Should().NotBeNull();
        }
        public async Task When_using_pipelined_SQL_command_scheduling_then_advancing_the_clock_blocks_until_triggered_commands_are_completed()
        {
            VirtualClock.Start();

            var scheduler  = Configuration.Current.CommandScheduler <Order>();
            var repository = Configuration.Current.Repository <Order>();

            var aggregateId = Any.Guid();

            await scheduler.Schedule(new CommandScheduled <Order>
            {
                Command = new CreateOrder(Any.FullName())
                {
                    AggregateId = aggregateId
                },
                DueTime     = Clock.Now().AddHours(1),
                AggregateId = aggregateId
            });

            VirtualClock.Current.AdvanceBy(TimeSpan.FromDays(1));

            var order = await repository.GetLatest(aggregateId);

            order.Should().NotBeNull();
        }
Esempio n. 17
0
        public async Task When_readme_file_is_on_root_browser_opens_there()
        {
            var directoryAccessor = new InMemoryDirectoryAccessor
            {
                ("./readme.md", ""),
                ("./subfolder/part1.md", ""),
                ("./subfolder/part2.md", "")
            };

            var root = directoryAccessor.GetFullyQualifiedPath(new RelativeDirectoryPath(".")) as DirectoryInfo;

            var options = new StartupOptions(dir: root);

            using (var clock = VirtualClock.Start())
                using (var agent = new AgentService(options: options, directoryAccessor: directoryAccessor))
                {
                    await clock.Wait(5.Seconds());

                    agent.BrowserLauncher
                    .LaunchedUri
                    .ToString()
                    .Should()
                    .Match("http://localhost:*/readme.md");
                }
        }
Esempio n. 18
0
        public void When_VirtualClock_Start_is_called_while_a_VirtualClock_is_already_in_use_it_throws()
        {
            VirtualClock.Start();
            Action startAgain = () => VirtualClock.Start();

            startAgain.ShouldThrow <InvalidOperationException>();
        }
Esempio n. 19
0
        public void SetUp()
        {
            // disable authorization
            Command <Order> .AuthorizeDefault           = (o, c) => true;
            Command <CustomerAccount> .AuthorizeDefault = (o, c) => true;

            disposables = new CompositeDisposable {
                VirtualClock.Start()
            };

            var customerId = Any.Guid();

            scenario = new ScenarioBuilder(c => c.UseInMemoryEventStore()
                                           .UseInMemoryCommandScheduling())
                       .AddEvents(new EventSequence(customerId)
            {
                new CustomerAccount.UserNameAcquired
                {
                    UserName = Any.Email()
                }
            }.ToArray())
                       .Prepare();

            disposables.Add(scenario);
        }
Esempio n. 20
0
        public void VirtualClock_Start_can_be_used_to_specify_a_virtual_time_that_Clock_Now_will_return()
        {
            var time = Any.DateTimeOffset();

            using (VirtualClock.Start(time))
            {
                Clock.Now().Should().Be(time);
            }
        }
Esempio n. 21
0
        public void Clock_can_be_overridden_using_VirtualClock()
        {
            var virtualTime = DateTimeOffset.Parse("2027-06-02 12:23am", CultureInfo.InvariantCulture);

            using (VirtualClock.Start(virtualTime))
            {
                Clock.Now().Should().Be(virtualTime);
            }
        }
Esempio n. 22
0
        public void VirtualClock_cannot_go_back_in_time_using_AdvanceBy()
        {
            var time = Any.DateTimeOffset();

            using (VirtualClock.Start(time))
            {
                Action goBackInTime = () => VirtualClock.Current.AdvanceBy(TimeSpan.FromSeconds(-1));
                goBackInTime.ShouldThrow <ArgumentException>();
            }
        }
Esempio n. 23
0
        public async Task The_clock_can_be_advanced_by_a_specific_timespan()
        {
            var initialTime = DateTimeOffset.Parse("2017-6-15 1:00pm", CultureInfo.InvariantCulture);

            using (var clock = VirtualClock.Start(initialTime))
            {
                await clock.AdvanceBy(12.Minutes());

                Clock.Now().Should().Be(initialTime + 12.Minutes());
            }
        }
Esempio n. 24
0
        public void ScenarioBuilder_StartTime_returns_clock_time_when_there_are_no_initial_events()
        {
            var clockTime = Any.DateTimeOffset();

            using (VirtualClock.Start(clockTime))
            {
                var builder = CreateScenarioBuilder().AdvanceClockTo(clockTime);

                builder.StartTime().Should().Be(clockTime);
            }
        }
Esempio n. 25
0
        public async Task Instantiating_a_VirtualClock_freezes_time()
        {
            var virtualTime = DateTimeOffset.Parse("2027-06-02 12:23am", CultureInfo.InvariantCulture);

            using (VirtualClock.Start(virtualTime))
            {
                await Task.Delay(200);

                Clock.Now().Should().Be(virtualTime);
            }
        }
Esempio n. 26
0
 public void The_virtual_clock_cannot_be_moved_backwards_using_AdvanceTo()
 {
     using (var clock = VirtualClock.Start())
     {
         Func <Task> moveBackwards = () => clock.AdvanceTo(clock.Now().Subtract(1.Minutes()));
         moveBackwards.Should().Throw <ArgumentException>()
         .Which
         .Message
         .Should()
         .Be("The clock cannot be moved backward in time.");
     }
 }
Esempio n. 27
0
        public async Task The_clock_can_be_advanced_to_a_specific_time()
        {
            var initialTime = DateTimeOffset.Parse("2017-01-15 1:00pm", CultureInfo.InvariantCulture);
            var newTime     = DateTimeOffset.Parse("2018-06-16 1:50pm", CultureInfo.InvariantCulture);

            using (var clock = VirtualClock.Start(initialTime))
            {
                await clock.AdvanceTo(newTime);

                Clock.Now().Should().Be(newTime);
            }
        }
Esempio n. 28
0
        public void When_VirtualClock_is_disposed_then_Clock_Current_is_restored_to_the_system_clock()
        {
            var time = Any.DateTimeOffset();

            using (VirtualClock.Start(time))
            {
            }

            var now = Clock.Now();

            now.Should().BeInRange(now, DateTimeOffset.Now.AddMilliseconds(10));
        }
Esempio n. 29
0
        public void When_VirtualClock_is_disposed_then_Clock_Current_is_restored_to_the_system_clock()
        {
            var time = Any.DateTimeOffset();

            using (VirtualClock.Start(time.Subtract(60.Seconds())))
            {
            }

            var now = Clock.Now();

            now.Should().BeCloseTo(DateTimeOffset.UtcNow, 10);
        }
Esempio n. 30
0
        public void Event_timestamps_can_be_set_using_VirtualClock()
        {
            var startTime = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(Any.PositiveInt(10000)));

            using (VirtualClock.Start(startTime))
            {
                var scenario = CreateScenarioBuilder();

                scenario.AddEvents(new Order.Cancelled());

                scenario.InitialEvents.Last().Timestamp.Should().Be(startTime);
            }
        }