-
Notifications
You must be signed in to change notification settings - Fork 759
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
Improve CountUpTimer ticks precision #8058
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b904548
Cancel and start a new timer on pause & resume
757bd5f
Remove clock from core-utils module
9a31aa3
Move clock to core-utils module
3f6b60c
Replace ticker flow with simple coroutine
6f18c02
Add unit test on count up timer
524680f
changelog
9383319
Fix import
912c37e
Reset elapsed time on stop action and add a start method to the Count…
43ecb63
Remove trailing space
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve the `CountUpTimer` implementation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
library/core-utils/src/test/java/im/vector/lib/core/utils/timer/CountUpTimerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (c) 2023 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package im.vector.lib.core.utils.timer | ||
|
||
import im.vector.lib.core.utils.test.fakes.FakeClock | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verifySequence | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.advanceTimeBy | ||
import kotlinx.coroutines.test.currentTime | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
|
||
private const val AN_INTERVAL = 500L | ||
private const val AN_INITIAL_TIME = 2_333L | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
internal class CountUpTimerTest { | ||
|
||
private val fakeClock = FakeClock() | ||
|
||
@Test | ||
fun `when pausing and resuming the timer, the timer ticks the right values at the right moments`() = runTest { | ||
every { fakeClock.epochMillis() } answers { currentTime } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively we could define a TestClock like this: @OptIn(ExperimentalCoroutinesApi::class)
class TestClock(private val ts: TestScope) : Clock {
override fun epochMillis(): Long {
return ts.currentTime
}
} (do not change, it's just to share my idea :) ). |
||
val tickListener = mockk<CountUpTimer.TickListener>(relaxed = true) | ||
val timer = CountUpTimer( | ||
coroutineScope = this, | ||
clock = fakeClock, | ||
intervalInMs = AN_INTERVAL, | ||
).also { it.tickListener = tickListener } | ||
|
||
timer.start() | ||
advanceTimeBy(AN_INTERVAL / 2) // no tick | ||
timer.pause() // tick | ||
advanceTimeBy(AN_INTERVAL * 10) // no tick | ||
timer.resume() // no tick | ||
advanceTimeBy(AN_INTERVAL * 4) // tick * 4 | ||
timer.stop() // tick | ||
|
||
verifySequence { | ||
tickListener.onTick(AN_INTERVAL / 2) | ||
tickListener.onTick(AN_INTERVAL) | ||
tickListener.onTick(AN_INTERVAL * 2) | ||
tickListener.onTick(AN_INTERVAL * 3) | ||
tickListener.onTick(AN_INTERVAL * 4) | ||
tickListener.onTick(AN_INTERVAL * 4 + AN_INTERVAL / 2) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given an initial time, the timer ticks the right values at the right moments`() = runTest { | ||
every { fakeClock.epochMillis() } answers { currentTime } | ||
val tickListener = mockk<CountUpTimer.TickListener>(relaxed = true) | ||
val timer = CountUpTimer( | ||
coroutineScope = this, | ||
clock = fakeClock, | ||
intervalInMs = AN_INTERVAL, | ||
).also { it.tickListener = tickListener } | ||
|
||
timer.start(AN_INITIAL_TIME) | ||
advanceTimeBy(AN_INTERVAL) // tick | ||
timer.pause() // tick | ||
advanceTimeBy(AN_INTERVAL * 10) // no tick | ||
timer.resume() // no tick | ||
advanceTimeBy(AN_INTERVAL * 4) // tick * 4 | ||
timer.stop() // tick | ||
|
||
val offset = AN_INITIAL_TIME % AN_INTERVAL | ||
verifySequence { | ||
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL - offset) | ||
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL) | ||
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL * 2 - offset) | ||
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL * 3 - offset) | ||
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL * 4 - offset) | ||
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL * 5 - offset) | ||
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL * 5) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are not using
tickerFlow
anymore, I think the@OptIn
can be removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
912c37e#diff-33cd92dcbf345017e1653f4fe78ab0483508972ba90fc29e8277e8a5e5624d0fR26