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

Use Clock interface #5907

Merged
merged 12 commits into from
May 4, 2022
Merged

Use Clock interface #5907

merged 12 commits into from
May 4, 2022

Conversation

bmarty
Copy link
Member

@bmarty bmarty commented May 3, 2022

Type of change

  • Feature
  • Bugfix
  • Technical
  • Other :

Content

  • Use a Clock interface instead of direct usage of System.currentTimeMillis()
  • Sometimes replace by a UUID or Random.nextLong(), when this is not strictly required to have a timestamp.
  • Some formatting fix also (this is polluting a bit sadly)
  • Not sure that was necessary (?) but I also replaced the usage in the tests, by implementing a DefaultClock.

Closes #4562

Motivation and context

Be able to speed up test! See #4562

Screenshots / GIFs

Tests

  • Quick smoke test
  • Run sanity test (in progress)

Tested devices

  • Physical
  • Emulator
  • OS version(s):

Reviewers

CC @ouchadam who opened the original issue.

@bmarty bmarty requested review from a team, ganfra, Florian14 and ouchadam and removed request for a team May 3, 2022 10:52
@github-actions
Copy link

github-actions bot commented May 3, 2022

Unit Test Results

122 files  ±0  122 suites  ±0   2m 6s ⏱️ +6s
205 tests ±0  205 ✔️ ±0  0 💤 ±0  0 ±0 
690 runs  ±0  690 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 2fb5f42. ± Comparison against base commit 9c558f1.

♻️ This comment has been updated with latest results.

@@ -48,13 +50,18 @@ class AttachmentEncryptionTest {
inputStream = if (inputAsByteArray.isEmpty()) {
inputAsByteArray.inputStream()
} else {
val memoryFile = MemoryFile("file" + System.currentTimeMillis(), inputAsByteArray.size)
val memoryFile = MemoryFile("file_" + UUID.randomUUID(), inputAsByteArray.size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

// The unique queue id will stay the same as long as this object is instanciated
val queueSuffixApp = System.currentTimeMillis()
// The unique queue id will stay the same as long as this object is instantiated
private val queueSuffixApp = UUID.randomUUID()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


### Use the Clock interface, or use `measureTimeMillis`
System\.currentTimeMillis\(\)===2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 nice check!

override fun doWork(): Result {
Timber.d("## Sync: RestartWhenNetworkOn.doWork()")
val sessionId = inputData.getString(KEY_SESSION_ID) ?: return Result.failure()
val syncTimeoutSeconds = inputData.getInt(KEY_SYNC_TIMEOUT_SECONDS, BackgroundSyncMode.DEFAULT_SYNC_TIMEOUT_SECONDS)
val syncDelaySeconds = inputData.getInt(KEY_SYNC_DELAY_SECONDS, BackgroundSyncMode.DEFAULT_SYNC_DELAY_SECONDS)
val isPeriodic = inputData.getBoolean(KEY_IS_PERIODIC, false)

// Not sure how to inject a Clock here
val clock = DefaultClock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we would need to provide this worker via factory like https://github.com/vector-im/element-android/blob/develop/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/worker/MatrixWorkerFactory.kt#L46

I'm not sure it's worth the effort for just the clock dependency

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I do not think it deserves it. The factory does not exist yet, since this is the only Worker we have on the app side (I think).

MEDIA_SAVING_3_DAYS -> System.currentTimeMillis() / 1000 - 3 * 24 * 60 * 60
MEDIA_SAVING_1_WEEK -> System.currentTimeMillis() / 1000 - 7 * 24 * 60 * 60
MEDIA_SAVING_1_MONTH -> System.currentTimeMillis() / 1000 - 30 * 24 * 60 * 60
MEDIA_SAVING_3_DAYS -> clock.epochMillis() / 1000 - 3 * 24 * 60 * 60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not for this PR, I could imagine in the future a ClockExtensions for these types of helpers

Copy link
Contributor

@ouchadam ouchadam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from a static review everything looks good to me 👍

would be good to double check the keybackup (as I know that area can be a little brittle~)

Copy link
Contributor

@Florian14 Florian14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor comments, not blocking for the merge. Also, there are some tests failure in DownloadMediaUseCaseTest because of the new param

@bmarty bmarty force-pushed the feature/bma/currentTimeMillis branch from e35de99 to 8602cbb Compare May 3, 2022 15:43
Copy link
Member

@ganfra ganfra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one remark, otherwise ok.

@bmarty bmarty requested a review from Florian14 May 4, 2022 15:52
Copy link
Contributor

@Florian14 Florian14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, thanks

@bmarty bmarty enabled auto-merge May 4, 2022 15:59
@bmarty bmarty merged commit 330d802 into develop May 4, 2022
@bmarty bmarty deleted the feature/bma/currentTimeMillis branch May 4, 2022 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace System.currentTimeMillis usage with injectable Clock
4 participants