Skip to content

Commit

Permalink
fix: added test for non 100 or 0 start percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalaber committed Dec 2, 2024
1 parent 10b9833 commit bafa9f8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
75 changes: 75 additions & 0 deletions lib/shared/bucketing/__tests__/bucketing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,81 @@ describe('Rollout Logic', () => {
).toBeFalsy()
})

it('should handle stepped rollout with 0% start and 100% later stage', () => {
const rollout: PublicRollout = {
type: 'stepped',
startDate: new Date(),
startPercentage: 0,
stages: [
{
type: 'discrete',
date: new Date(
new Date().getTime() + 1000 * 60 * 60 * 24 * 7,
),
percentage: 1,
},
],
}

// Before next stage - should be 0%
jest.useFakeTimers().setSystemTime(new Date())
for (let i = 0; i < 100; i++) {
expect(
doesUserPassRollout({ rollout, boundedHash: i / 100 }),
).toBeFalsy()
}

// After 100% stage - should pass all users
jest.useFakeTimers().setSystemTime(
new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 8),
)
for (let i = 0; i < 100; i++) {
expect(
doesUserPassRollout({ rollout, boundedHash: i / 100 }),
).toBeTruthy()
}

jest.useRealTimers()
})

it('should handle stepped rollout with 50% start and 100% later stage', () => {
const rollout: PublicRollout = {
type: 'stepped',
startDate: new Date(),
startPercentage: 0.5,
stages: [
{
type: 'discrete',
date: new Date(
new Date().getTime() + 1000 * 60 * 60 * 24 * 7,
),
percentage: 1,
},
],
}

// Before next stage - should be 50%
jest.useFakeTimers().setSystemTime(new Date())
expect(
doesUserPassRollout({ rollout, boundedHash: 0.49 }),
).toBeTruthy()
expect(
doesUserPassRollout({ rollout, boundedHash: 0.51 }),
).toBeFalsy()

// After 100% stage - should pass all users
jest.useFakeTimers().setSystemTime(
new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 8),
)
for (let i = 0; i < 100; i++) {
expect(
doesUserPassRollout({ rollout, boundedHash: i / 100 }),
).toBeTruthy()
}

jest.useRealTimers()
})

it('should handle stepped rollout with 100% start and 0% later stage', () => {
const rollout: PublicRollout = {
type: 'stepped',
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/bucketing/src/bucketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const getCurrentRolloutPercentage = (
: null)

if (!currentStage) {
return 0
return start
}

if (!nextStage || nextStage.type === 'discrete') {
Expand Down

0 comments on commit bafa9f8

Please sign in to comment.