PrometheusBasedResourceLimitChecks implements ResourceLimitChecks { Duration calculateResourceUsageDuration( final Instant effectiveSince, final Instant targetDateTime, final String periodType, final long periodLength) { Objects.requireNonNull(effectiveSince, "effective since"); Objects.requireNonNull(targetDateTime, "target date-time"); Objects.requireNonNull(periodType, "period type"); if (targetDateTime.isBefore(effectiveSince)) { return Duration.ZERO; } final ZonedDateTime targetZonedDateTime = ZonedDateTime.ofInstant(targetDateTime, ZoneOffset.UTC); final ZonedDateTime beginningOfMostRecentAccountingPeriod = getBeginningOfMostRecentAccountingPeriod( ZonedDateTime.ofInstant(effectiveSince, ZoneOffset.UTC), targetZonedDateTime, periodType, periodLength); return Duration.between(beginningOfMostRecentAccountingPeriod, targetZonedDateTime); } PrometheusBasedResourceLimitChecks(
            final WebClient webClient,
            final PrometheusBasedResourceLimitChecksConfig config,
            final AsyncCache<String, LimitedResource<Long>> connectionCountCache,
            final AsyncCache<String, LimitedResource<Duration>> connectionDurationCache,
            final AsyncCache<String, LimitedResource<Long>> dataVolumeCache);  PrometheusBasedResourceLimitChecks(
            final WebClient webClient,
            final PrometheusBasedResourceLimitChecksConfig config,
            final AsyncCache<String, LimitedResource<Long>> connectionCountCache,
            final AsyncCache<String, LimitedResource<Duration>> connectionDurationCache,
            final AsyncCache<String, LimitedResource<Long>> dataVolumeCache,
            final Tracer tracer); @Override Future<Boolean> isConnectionLimitReached(final TenantObject tenant, final SpanContext spanContext); @Override Future<Boolean> isMessageLimitReached(
            final TenantObject tenant,
            final long payloadSize,
            final SpanContext spanContext); @Override Future<Boolean> isConnectionDurationLimitReached(
            final TenantObject tenant,
            final SpanContext spanContext);  }
@Test public void verifyResourceUsagePeriodCalculation() { Instant since = Instant.parse("2019-09-06T07:15:00Z"); Instant now = Instant.parse("2019-09-10T14:30:00Z"); Instant currentAccountingPeriodStart = since; assertEquals(Duration.between(currentAccountingPeriodStart, now), limitChecksImpl.calculateResourceUsageDuration( since, now, ResourceLimitsPeriod.PERIOD_MODE_MONTHLY, 30)); since = Instant.parse("2019-08-06T05:11:00Z"); now = Instant.parse("2019-09-06T14:30:00Z"); currentAccountingPeriodStart = Instant.parse("2019-09-01T00:00:00Z"); assertEquals(Duration.between(currentAccountingPeriodStart, now), limitChecksImpl.calculateResourceUsageDuration( since, now, ResourceLimitsPeriod.PERIOD_MODE_MONTHLY, 30)); since = Instant.parse("2019-08-20T19:03:59Z"); now = Instant.parse("2019-09-10T09:32:17Z"); currentAccountingPeriodStart = since; assertEquals(Duration.between(currentAccountingPeriodStart, now), limitChecksImpl.calculateResourceUsageDuration( since, now, ResourceLimitsPeriod.PERIOD_MODE_DAYS, 30)); since = Instant.parse("2019-07-03T11:49:22Z"); now = Instant.parse("2019-09-10T14:30:11Z"); currentAccountingPeriodStart = Instant.parse("2019-09-01T11:49:22Z"); assertEquals(Duration.between(currentAccountingPeriodStart, now), limitChecksImpl.calculateResourceUsageDuration( since, now, ResourceLimitsPeriod.PERIOD_MODE_DAYS, 30)); }