Skip to content

Commit

Permalink
[no ticket][risk=no] Add condition for initial credit eligibility. (#…
Browse files Browse the repository at this point in the history
…9003)

* Add condition for initial credit eligibility.

* Fix missing default credit limit

* Add tests to ensure that credit exhaustion prevents extensions.

* Lint
  • Loading branch information
evrii authored Dec 17, 2024
1 parent 6a33715 commit fcac632
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,11 @@ public DbUser extendInitialCreditsExpiration(DbUser user) {
public boolean checkInitialCreditsExtensionEligibility(DbUser dbUser) {
DbUserInitialCreditsExpiration initialCreditsExpiration =
dbUser.getUserInitialCreditsExpiration();
Instant now = Instant.now();
Instant now = clock.instant();
WorkbenchConfig.BillingConfig billingConfig = workbenchConfigProvider.get().billing;

return initialCreditsExpiration != null
return userHasRemainingFreeTierCredits(dbUser)
&& initialCreditsExpiration != null
&& initialCreditsExpiration.getExtensionTime() == null
&& initialCreditsExpiration.getCreditStartTime() != null
&& now.isAfter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,40 @@ public void test_extendInitialCreditsExpiration_notYetExtendedWithinWindow() {
assertEquals(actualExpirationRecord.getExtensionTime(), NOW);
}

@Test
public void test_checkInitialCreditsExtensionEligibility_noRemainingCredits() {
workbenchConfig.billing.defaultFreeCreditsDollarLimit = 100.0;

final DbUser user = createUser(SINGLE_WORKSPACE_TEST_USER);
user.setUserInitialCreditsExpiration(
new DbUserInitialCreditsExpiration()
.setCreditStartTime(NOW)
.setExpirationTime(NOW)
.setExtensionTime(null));

workspaceFreeTierUsageDao.save(
new DbWorkspaceFreeTierUsage(workspace).setUser(user).setCost(300.0));
boolean eligibility = initialCreditsService.checkInitialCreditsExtensionEligibility(user);
assertThat(eligibility).isFalse();
}

@Test
public void test_checkInitialCreditsExtensionEligibility_hasRemainingCredits() {
workbenchConfig.billing.defaultFreeCreditsDollarLimit = 100.0;

final DbUser user = createUser(SINGLE_WORKSPACE_TEST_USER);
user.setUserInitialCreditsExpiration(
new DbUserInitialCreditsExpiration()
.setCreditStartTime(NOW)
.setExpirationTime(NOW)
.setExtensionTime(null));

workspaceFreeTierUsageDao.save(
new DbWorkspaceFreeTierUsage(workspace).setUser(user).setCost(30.0));
boolean eligibility = initialCreditsService.checkInitialCreditsExtensionEligibility(user);
assertThat(eligibility).isTrue();
}

private TableResult mockBQTableResult(final Map<String, Double> costMap) {
Field idField = Field.of("id", LegacySQLTypeName.STRING);
Field costField = Field.of("cost", LegacySQLTypeName.FLOAT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public void setUp() {
.setGoogleProject(GOOGLE_PROJECT);

workbenchConfig = createEmptyConfig();
workbenchConfig.billing.defaultFreeCreditsDollarLimit = 100.0;
}

@Test
Expand Down

0 comments on commit fcac632

Please sign in to comment.