Repository navigation
feat(sync): automatic one-shot staging reset (#296) #328
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
Open
TimeToBuildBob
wants to merge
2
commits into
ActivityWatch:master
Choose a base branch
from
TimeToBuildBob:fix/reset-staging-one-shot
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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
49 changes: 49 additions & 0 deletions
49
mobile/src/main/java/net/activitywatch/android/StagingReset.kt
This file contains hidden or 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,49 @@ | ||
| package net.activitywatch.android | ||
|
|
||
| import org.json.JSONObject | ||
|
|
||
| /** | ||
| * One-shot reset of this device's push staging db (ActivityWatch/aw-android#296). | ||
| * | ||
| * Pushes made before ActivityWatch/aw-server-rust#713 left duplicate copies in | ||
| * `{sync_dir}/{hostname}/{device_id}/test.db`. Staging is derived data, so the | ||
| * native side deletes it and the next push rebuilds it from the local datastore. | ||
| * | ||
| * Kept free of Android types so the flag logic is unit-testable on the JVM. | ||
| */ | ||
| object StagingReset { | ||
| fun prefKey(hostname: String) = "stagingResetDone_$hostname" | ||
|
|
||
| sealed class Result { | ||
| object AlreadyDone : Result() | ||
| object Done : Result() | ||
| data class Failed(val reason: String) : Result() | ||
| } | ||
|
|
||
| /** | ||
| * Runs [reset] unless [isDone], and calls [markDone] only when the native | ||
| * side reports `{"success": true}`. Never throws: a failed reset must not | ||
| * block the sync that follows it. | ||
| * | ||
| * [LinkageError] is caught as well as [Exception] because a native library | ||
| * built without `resetStaging` throws [UnsatisfiedLinkError]; letting that | ||
| * escape would kill the sync executor before its callback clears | ||
| * `syncInFlight`, rejecting every later sync as "already in flight". | ||
| */ | ||
| fun runOnce(isDone: () -> Boolean, markDone: () -> Unit, reset: () -> String): Result { | ||
| if (isDone()) return Result.AlreadyDone | ||
| return try { | ||
| val json = JSONObject(reset()) | ||
| if (json.optBoolean("success", false)) { | ||
| markDone() | ||
| Result.Done | ||
| } else { | ||
| Result.Failed(json.optString("error", "unknown error")) | ||
| } | ||
| } catch (e: Exception) { | ||
| Result.Failed("${e.javaClass.simpleName}: ${e.message}") | ||
| } catch (e: LinkageError) { | ||
| Result.Failed("${e.javaClass.simpleName}: ${e.message}") | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
72 changes: 72 additions & 0 deletions
72
mobile/src/test/java/net/activitywatch/android/StagingResetTest.kt
This file contains hidden or 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,72 @@ | ||
| package net.activitywatch.android | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNotEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| class StagingResetTest { | ||
| private class Prefs(var done: Boolean = false) { | ||
| var markCalls = 0 | ||
| fun mark() { | ||
| markCalls++ | ||
| done = true | ||
| } | ||
| } | ||
|
|
||
| private fun attempt(prefs: Prefs, reset: () -> String) = | ||
| StagingReset.runOnce(isDone = { prefs.done }, markDone = prefs::mark, reset = reset) | ||
|
|
||
| @Test | ||
| fun runsResetOnceThenSkips() { | ||
| val prefs = Prefs() | ||
| var resetCalls = 0 | ||
| val reset = { resetCalls++; """{"success": true}""" } | ||
|
|
||
| assertEquals(StagingReset.Result.Done, attempt(prefs, reset)) | ||
| assertEquals(StagingReset.Result.AlreadyDone, attempt(prefs, reset)) | ||
|
|
||
| assertEquals(1, resetCalls) | ||
| assertEquals(1, prefs.markCalls) | ||
| } | ||
|
|
||
| @Test | ||
| fun alreadyDoneNeverCallsNative() { | ||
| val prefs = Prefs(done = true) | ||
| val result = attempt(prefs) { throw AssertionError("reset must not run") } | ||
| assertEquals(StagingReset.Result.AlreadyDone, result) | ||
| assertEquals(0, prefs.markCalls) | ||
| } | ||
|
|
||
| @Test | ||
| fun nativeFailureLeavesFlagUnsetForRetry() { | ||
| val prefs = Prefs() | ||
| val result = attempt(prefs) { """{"success": false, "error": "path outside sync dir"}""" } | ||
| assertEquals(StagingReset.Result.Failed("path outside sync dir"), result) | ||
| assertEquals(0, prefs.markCalls) | ||
| } | ||
|
|
||
| @Test | ||
| fun malformedResponseIsFailure() { | ||
| val prefs = Prefs() | ||
| assertTrue(attempt(prefs) { "not json" } is StagingReset.Result.Failed) | ||
| assertTrue(attempt(prefs) { "{}" } is StagingReset.Result.Failed) | ||
| assertEquals(0, prefs.markCalls) | ||
| } | ||
|
|
||
| @Test | ||
| fun missingNativeSymbolDoesNotEscape() { | ||
| // A native lib built before resetStaging existed throws UnsatisfiedLinkError, | ||
| // which is not an Exception; it must not escape into the sync executor. | ||
| val prefs = Prefs() | ||
| val result = attempt(prefs) { throw UnsatisfiedLinkError("resetStaging") } | ||
| assertTrue(result is StagingReset.Result.Failed) | ||
| assertEquals(0, prefs.markCalls) | ||
| } | ||
|
|
||
| @Test | ||
| fun prefKeyIsPerHostname() { | ||
| assertEquals("stagingResetDone_poco_f8_ultra", StagingReset.prefKey("poco_f8_ultra")) | ||
| assertNotEquals(StagingReset.prefKey("a"), StagingReset.prefKey("b")) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.