示例#1
0
        Given_ReportActor_When_sending_start_message_Then_total_usage_projection_launched_producing_data()
        {
            var dep = await Init();

            _output.WriteLine(Sys.Settings.ToString());
            //generate some data

            var calculationActor = Sys.ActorOf(Props.Create <CalculatorActor>(), "CalculatorOne");

            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1+2-3"));
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1-2*3"));
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1/2+3"));

            await Task.Delay(10000);

            var projected = new FindProjectionQuery(dep.CreateFunctionUsageContext()).ExecuteForFunctionsTotalUsage();

            Assert.Equal(3, projected.Sequence);

            var usage = await new FunctionsTotalUsageQuery(dep.CreateFunctionUsageContext()).Execute();

            usage.Should().BeEquivalentTo(
                new FunctionTotalUsage {
                FunctionName = "AddChecked", InvocationsCount = 2
            },
                new FunctionTotalUsage {
                FunctionName = "SubtractChecked", InvocationsCount = 2
            },
                new FunctionTotalUsage {
                FunctionName = "MultiplyChecked", InvocationsCount = 1
            },
                new FunctionTotalUsage {
                FunctionName = "Divide", InvocationsCount = 1
            });
        }
        public void Given_empty_context_When_executing_projection_query_Then_it_should_return_Null()
        {
            var options = new DbContextOptionsBuilder <FunctionUsageContext>()
                          .UseInMemoryDatabase(nameof(Given_empty_context_When_executing_projection_query_Then_it_should_return_Null)).Options;

            var context = new FunctionUsageContext(options);
            var query   = new FindProjectionQuery(context);

            var result = query.Execute("testName", "projector", "testEvent");

            Assert.Null(result);
        }
示例#3
0
        public async Task Given_ReportActor_When_sending_start_message_Then_usage_projection_launched_producing_data()
        {
            var dep = await Init();

            _output.WriteLine(Sys.Settings.ToString());
            //generate some data
            var now         = DateTimeOffset.Now;
            var periodStart = now.ToMinutePeriodBegin();
            var periodEnd   = now.ToMinutePeriodEnd();
            var oneMinute   = TimeSpan.FromMinutes(1);

            var calculatorName   = "CalculatorOne";
            var calculationActor = Sys.ActorOf(Props.Create <CalculatorActor>(), calculatorName);

            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1+2-3"));
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1-2*3"));
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1/2+3"));

            await Task.Delay(10000);

            var projected = new FindProjectionQuery(dep.CreateFunctionUsageContext()).ExecuteForFunctionsUsage();

            Assert.Equal(3, projected.Sequence);

            var usage = await new FunctionsUsageQuery(dep.CreateFunctionUsageContext()).Execute(calculatorName);

            usage.Should().BeEquivalentTo(
                new FunctionUsage
            {
                FunctionName = "AddChecked", InvocationsCount = 2, CalculatorName = calculatorName,
                Period       = oneMinute, PeriodEnd = periodEnd, PeriodStart = periodStart
            },
                new FunctionUsage
            {
                FunctionName = "SubtractChecked", CalculatorName = calculatorName, InvocationsCount = 2,
                Period       = oneMinute, PeriodEnd = periodEnd, PeriodStart = periodStart
            },
                new FunctionUsage
            {
                FunctionName = "MultiplyChecked", CalculatorName = calculatorName, InvocationsCount = 1,
                Period       = oneMinute, PeriodEnd = periodEnd, PeriodStart = periodStart
            },
                new FunctionUsage
            {
                FunctionName = "Divide", CalculatorName = calculatorName, InvocationsCount = 1, Period = oneMinute,
                PeriodEnd    = periodEnd, PeriodStart = periodStart
            });
        }
示例#4
0
        public async Task Given_journal_When_starting_projection_stream_Then_projection_launched_producing_data()
        {
            var dep = await Init();

            _output.WriteLine(Sys.Settings.ToString());
            //generate some data
            var calculationActor = Sys.ActorOf(Props.Create <CalculatorActor>(), "CalculatorOne");

            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1+2-3"));
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1-2*3"));
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1/2+3"));

            var eventName = nameof(CalculatorActor.CalculationPerformed);

            var readJournal      = PersistenceQuery.Get(Sys).ReadJournalFor <SqlReadJournal>(SqlReadJournal.Identifier);
            var sharedKillSwitch = KillSwitches.Shared("test");
            var source           = readJournal.EventsByTag(eventName, Offset.NoOffset())
                                   .Via(sharedKillSwitch.Flow <EventEnvelope>());

            var flow = FunctionTotalUsageFlow.Instance;
            var sink = FunctionTotalUsageSink.Create(Sys, eventName);

            source.Via(flow).To(sink).Run(Sys.Materializer());

            await Task.Delay(5000);

            var projected = new FindProjectionQuery(dep.CreateFunctionUsageContext()).ExecuteForFunctionsTotalUsage();

            Assert.Equal(3, projected.Sequence);

            var usage = await new FunctionsTotalUsageQuery(dep.CreateFunctionUsageContext()).Execute();

            usage.Should().BeEquivalentTo(
                new FunctionTotalUsage {
                FunctionName = "AddChecked", InvocationsCount = 2
            },
                new FunctionTotalUsage {
                FunctionName = "SubtractChecked", InvocationsCount = 2
            },
                new FunctionTotalUsage {
                FunctionName = "MultiplyChecked", InvocationsCount = 1
            },
                new FunctionTotalUsage {
                FunctionName = "Divide", InvocationsCount = 1
            });
        }
        public async Task Given_context_with_data_When_executing_projection_query_Then_it_should_return_data()
        {
            var options = new DbContextOptionsBuilder <FunctionUsageContext>()
                          .UseInMemoryDatabase(nameof(Given_context_with_data_When_executing_projection_query_Then_it_should_return_data)).Options;

            var context = new FunctionUsageContext(options);
            var query   = new FindProjectionQuery(context);

            var projection = new Projection
            {
                Event = "testEvent", Name = "testName", Projector = "projector", Sequence = 10
            };

            context.Projections.Add(projection);
            await context.SaveChangesAsync();

            var res = query.Execute(projection.Name, projection.Projector, projection.Event);

            Assert.Equal(projection, res);
        }
示例#6
0
        public async Task Given_existing_total_usage_projection_When_starting_it_Then_projection_is_resumed()
        {
            var dep = await Init();

            //generate some data

            var calculationActor = Sys.ActorOf(Props.Create <CalculatorActor>(), "CalculatorOne");

            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1+2-3"));
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1-2*3"));
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1/2+3"));


            await Task.Delay(5000);

            //ensure it is persisted

            var projected = new FindProjectionQuery(dep.CreateFunctionUsageContext()).ExecuteForFunctionsTotalUsage();

            Assert.Equal(3, projected.Sequence);

            var reportActor = Sys.GetReportingExtension().ReportingActor;

            //restart report actor to allow it to get latest projected sequence from DB
            Watch(reportActor);
            Sys.Stop(reportActor);
            FishForMessage <Terminated>(t => t.ActorRef == reportActor);
            await Task.Delay(1000);

            reportActor = Sys.ActorOf(Props.Create <ReportingActor>(), "reportingActor");
            reportActor.Tell(ReportingActor.Start.Instance);

            var usage = await new FunctionsTotalUsageQuery(dep.CreateFunctionUsageContext()).Execute();

            usage.Should().BeEquivalentTo(
                new FunctionTotalUsage {
                FunctionName = "AddChecked", InvocationsCount = 2
            },
                new FunctionTotalUsage {
                FunctionName = "SubtractChecked", InvocationsCount = 2
            },
                new FunctionTotalUsage {
                FunctionName = "MultiplyChecked", InvocationsCount = 1
            },
                new FunctionTotalUsage {
                FunctionName = "Divide", InvocationsCount = 1
            });

            //add new events
            calculationActor.Tell(new CalculatorActorProtocol.CalculateExpression("1+2-3*4/5"));

            await Task.Delay(5000);

            //check the results
            projected = new FindProjectionQuery(dep.CreateFunctionUsageContext()).ExecuteForFunctionsTotalUsage();
            Assert.Equal(4, projected.Sequence);

            usage = await new FunctionsTotalUsageQuery(dep.CreateFunctionUsageContext()).Execute();

            usage.Should().BeEquivalentTo(
                new FunctionTotalUsage {
                FunctionName = "AddChecked", InvocationsCount = 3
            },
                new FunctionTotalUsage {
                FunctionName = "SubtractChecked", InvocationsCount = 3
            },
                new FunctionTotalUsage {
                FunctionName = "MultiplyChecked", InvocationsCount = 2
            },
                new FunctionTotalUsage {
                FunctionName = "Divide", InvocationsCount = 2
            });
        }