Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix flaky OutOfTimeUseCase tests #7310

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class OutOfTimeUseCaseTest {

@Test
fun `account that expires without new expiry event should emit true`() =
runTest(dispatcher) {
scope.runTest {
// Arrange
val expiredAccountExpiry =
AccountData(mockk(relaxed = true), DateTime.now().plusSeconds(100))
Expand All @@ -169,67 +169,69 @@ class OutOfTimeUseCaseTest {
}

@Test
fun `account that is about to expire but is refilled should emit false`() = runTest {
// Arrange
val initialAccountExpiry =
AccountData(mockk(relaxed = true), DateTime.now().plusSeconds(100))
val updatedExpiry =
AccountData(mockk(relaxed = true), initialAccountExpiry.expiryDate.plusDays(30))

// Act, Assert
outOfTimeUseCase.isOutOfTime.test {
// Initial event
assertEquals(null, awaitItem())

expiry.emit(initialAccountExpiry)
assertEquals(false, awaitItem())
advanceTimeBy(90.seconds)
expectNoEvents()

// User fills up with more time 10 seconds before expiry.
expiry.emit(updatedExpiry)
advanceTimeBy(29.days)
expectNoEvents()

advanceTimeBy(2.days)
assertEquals(true, expectMostRecentItem())
expectNoEvents()
fun `account that is about to expire but is refilled should emit false`() =
scope.runTest {
// Arrange
val initialAccountExpiry =
AccountData(mockk(relaxed = true), DateTime.now().plusSeconds(100))
val updatedExpiry =
AccountData(mockk(relaxed = true), initialAccountExpiry.expiryDate.plusDays(30))

// Act, Assert
outOfTimeUseCase.isOutOfTime.test {
// Initial event
assertEquals(null, awaitItem())

expiry.emit(initialAccountExpiry)
assertEquals(false, awaitItem())
advanceTimeBy(90.seconds)
expectNoEvents()

// User fills up with more time 10 seconds before expiry.
expiry.emit(updatedExpiry)
advanceTimeBy(29.days)
expectNoEvents()

advanceTimeBy(2.days)
assertEquals(true, expectMostRecentItem())
expectNoEvents()
}
}
}

@Test
fun `expired account that is refilled should emit false`() = runTest {
// Arrange
val initialAccountExpiry =
AccountData(mockk(relaxed = true), DateTime.now().plusSeconds(100))
val updatedExpiry =
AccountData(mockk(relaxed = true), initialAccountExpiry.expiryDate.plusDays(30))
// Act, Assert
outOfTimeUseCase.isOutOfTime.test {
// Initial event
assertEquals(null, awaitItem())

expiry.emit(initialAccountExpiry)
assertEquals(false, awaitItem())

// After 100 seconds we expire
advanceTimeBy(100.seconds)
assertEquals(true, expectMostRecentItem())
expectNoEvents()

// We then fill up our account and should no longer be out of time
expiry.emit(updatedExpiry)
assertEquals(false, awaitItem())
expectNoEvents()

// Advance the time to before the updated expiry
advanceTimeBy(29.days + 59.minutes)
expectNoEvents()

// Advance the time to the updated expiry
advanceTimeBy(30.days + 2.minutes)
assertEquals(true, expectMostRecentItem())
expectNoEvents()
fun `expired account that is refilled should emit false`() =
scope.runTest {
// Arrange
val initialAccountExpiry =
AccountData(mockk(relaxed = true), DateTime.now().plusSeconds(100))
val updatedExpiry =
AccountData(mockk(relaxed = true), initialAccountExpiry.expiryDate.plusDays(30))
// Act, Assert
outOfTimeUseCase.isOutOfTime.test {
// Initial event
assertEquals(null, awaitItem())

expiry.emit(initialAccountExpiry)
assertEquals(false, awaitItem())

// After 100 seconds we expire
advanceTimeBy(100.seconds)
assertEquals(true, expectMostRecentItem())
expectNoEvents()

// We then fill up our account and should no longer be out of time
expiry.emit(updatedExpiry)
assertEquals(false, awaitItem())
expectNoEvents()

// Advance the time to before the updated expiry
advanceTimeBy(29.days + 59.minutes)
expectNoEvents()

// Advance the time to the updated expiry
advanceTimeBy(30.days + 2.minutes)
assertEquals(true, expectMostRecentItem())
expectNoEvents()
}
}
}
}
Loading