コード例 #1
0
    public async Task CanIncrementUsageAsync()
    {
        var messageBus = GetService <IMessageBus>();

        var countdown = new AsyncCountdownEvent(2);
        await messageBus.SubscribeAsync <PlanOverage>(po => {
            _logger.LogInformation("Plan Overage for {organization} (Hourly: {IsHourly})", po.OrganizationId, po.IsHourly);
            countdown.Signal();
        });

        var organization = await _organizationRepository.AddAsync(new Organization { Name = "Test", MaxEventsPerMonth = 750, PlanId = _plans.SmallPlan.Id }, o => o.ImmediateConsistency());

        var project = await _projectRepository.AddAsync(new Project { Name = "Test", OrganizationId = organization.Id, NextSummaryEndOfDayTicks = SystemClock.UtcNow.Ticks }, o => o.ImmediateConsistency());

        Assert.InRange(organization.GetHourlyEventLimit(_plans), 1, 750);

        int totalToIncrement = organization.GetHourlyEventLimit(_plans) - 1;

        Assert.False(await _usageService.IncrementUsageAsync(organization, project, totalToIncrement));
        organization = await _organizationRepository.GetByIdAsync(organization.Id);

        await countdown.WaitAsync(TimeSpan.FromMilliseconds(150));

        Assert.Equal(2, countdown.CurrentCount);
        var organizationUsage = await _usageService.GetUsageAsync(organization);

        var projectUsage = await _usageService.GetUsageAsync(organization, project);

        Assert.Equal(totalToIncrement, organizationUsage.HourlyTotal);
        Assert.Equal(totalToIncrement, projectUsage.HourlyTotal);
        Assert.Equal(totalToIncrement, organizationUsage.MonthlyTotal);
        Assert.Equal(totalToIncrement, projectUsage.MonthlyTotal);
        Assert.Equal(0, organizationUsage.HourlyBlocked);
        Assert.Equal(0, projectUsage.HourlyBlocked);
        Assert.Equal(0, organizationUsage.MonthlyBlocked);
        Assert.Equal(0, projectUsage.MonthlyBlocked);

        Assert.True(await _usageService.IncrementUsageAsync(organization, project, 2));
        await countdown.WaitAsync(TimeSpan.FromMilliseconds(150));

        Assert.Equal(1, countdown.CurrentCount);

        organizationUsage = await _usageService.GetUsageAsync(organization);

        projectUsage = await _usageService.GetUsageAsync(organization, project);

        Assert.Equal(totalToIncrement + 2, organizationUsage.HourlyTotal);
        Assert.Equal(totalToIncrement + 2, projectUsage.HourlyTotal);
        Assert.Equal(totalToIncrement + 2, organizationUsage.MonthlyTotal);
        Assert.Equal(totalToIncrement + 2, projectUsage.MonthlyTotal);
        Assert.Equal(1, organizationUsage.HourlyBlocked);
        Assert.Equal(1, projectUsage.HourlyBlocked);
        Assert.Equal(1, organizationUsage.MonthlyBlocked);
        Assert.Equal(1, projectUsage.MonthlyBlocked);

        organization = await _organizationRepository.AddAsync(new Organization { Name = "Test", MaxEventsPerMonth = 750, PlanId = _plans.SmallPlan.Id }, o => o.ImmediateConsistency());

        project = await _projectRepository.AddAsync(new Project { Name = "Test", OrganizationId = organization.Id, NextSummaryEndOfDayTicks = SystemClock.UtcNow.Ticks }, o => o.ImmediateConsistency());

        await _cache.RemoveAllAsync();

        totalToIncrement = organization.GetHourlyEventLimit(_plans) + 20;
        Assert.True(await _usageService.IncrementUsageAsync(organization, project, totalToIncrement));

        await countdown.WaitAsync(TimeSpan.FromMilliseconds(150));

        Assert.Equal(0, countdown.CurrentCount);

        organizationUsage = await _usageService.GetUsageAsync(organization);

        projectUsage = await _usageService.GetUsageAsync(organization, project);

        Assert.Equal(totalToIncrement, organizationUsage.HourlyTotal);
        Assert.Equal(totalToIncrement, projectUsage.HourlyTotal);
        Assert.Equal(totalToIncrement, organizationUsage.MonthlyTotal);
        Assert.Equal(totalToIncrement, projectUsage.MonthlyTotal);
        Assert.Equal(20, organizationUsage.HourlyBlocked);
        Assert.Equal(20, projectUsage.HourlyBlocked);
        Assert.Equal(20, organizationUsage.MonthlyBlocked);
        Assert.Equal(20, projectUsage.MonthlyBlocked);
    }
コード例 #2
0
    public async Task CanRunJobWithDiscardedEventUsage()
    {
        Log.MinimumLevel = LogLevel.Debug;

        var organization = await _organizationRepository.GetByIdAsync(TestConstants.OrganizationId);

        var usage = await _usageService.GetUsageAsync(organization);

        Assert.Equal(0, usage.MonthlyTotal);

        usage = await _usageService.GetUsageAsync(organization);

        Assert.Equal(0, usage.MonthlyTotal);
        Assert.Equal(0, usage.MonthlyBlocked);

        var ev = GenerateEvent(type: Event.KnownTypes.Log, source: "test", userIdentity: "test1");

        Assert.NotNull(await EnqueueEventPostAsync(ev));

        var result = await _job.RunAsync();

        Assert.True(result.IsSuccess);

        await RefreshDataAsync();

        var events = await _eventRepository.GetAllAsync();

        Assert.Equal(2, events.Total);
        var logEvent = events.Documents.Single(e => String.Equals(e.Type, Event.KnownTypes.Log));

        Assert.NotNull(logEvent);
        var sessionEvent = events.Documents.Single(e => String.Equals(e.Type, Event.KnownTypes.Session));

        Assert.NotNull(sessionEvent);

        usage = await _usageService.GetUsageAsync(organization);

        Assert.Equal(1, usage.MonthlyTotal);
        Assert.Equal(0, usage.MonthlyBlocked);

        // Mark the stack as discarded
        var logStack = await _stackRepository.GetByIdAsync(logEvent.StackId);

        logStack.Status = StackStatus.Discarded;
        await _stackRepository.SaveAsync(logStack, o => o.ImmediateConsistency());

        var sessionStack = await _stackRepository.GetByIdAsync(sessionEvent.StackId);

        sessionStack.Status = StackStatus.Discarded;
        await _stackRepository.SaveAsync(sessionStack, o => o.ImmediateConsistency());

        // Verify job processed discarded events.
        Assert.NotNull(await EnqueueEventPostAsync(new List <PersistentEvent> {
            GenerateEvent(type: Event.KnownTypes.Session, sessionId: "abcdefghi"),
            GenerateEvent(type: Event.KnownTypes.Log, source: "test", sessionId: "abcdefghi"),
            GenerateEvent(type: Event.KnownTypes.Log, source: "test", userIdentity: "test3")
        }));

        result = await _job.RunAsync();

        Assert.True(result.IsSuccess);

        await RefreshDataAsync();

        events = await _eventRepository.GetAllAsync();

        Assert.Equal(3, events.Total);

        usage = await _usageService.GetUsageAsync(organization);

        Assert.Equal(1, usage.MonthlyTotal);
        Assert.Equal(0, usage.MonthlyBlocked);
    }