-
Notifications
You must be signed in to change notification settings - Fork 2
fix: add network error retry for transfer polling #763
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -263,19 +263,40 @@ class TransferViewModel @Inject constructor( | |||||
| } | ||||||
|
|
||||||
| private suspend fun pollUntil(orderId: String, condition: (IBtOrder) -> Boolean): IBtOrder? { | ||||||
| var consecutiveErrors = 0 | ||||||
|
|
||||||
| while (true) { | ||||||
| val order = blocktankRepo.getOrder(orderId, refresh = true).getOrNull() | ||||||
| if (order == null) { | ||||||
| Logger.error("Order not found: '$orderId'", context = TAG) | ||||||
| return null | ||||||
| } | ||||||
| if (order.state2 == BtOrderState2.EXPIRED) { | ||||||
| Logger.error("Order expired: '$orderId'", context = TAG) | ||||||
| return null | ||||||
| } | ||||||
| if (condition(order)) { | ||||||
| return order | ||||||
| } | ||||||
| blocktankRepo.getOrder(orderId, refresh = true).fold( | ||||||
| onSuccess = { order -> | ||||||
| consecutiveErrors = 0 | ||||||
|
|
||||||
| if (order == null) { | ||||||
| Logger.error("Order not found: '$orderId'", context = TAG) | ||||||
| return null | ||||||
| } | ||||||
| if (order.state2 == BtOrderState2.EXPIRED) { | ||||||
| Logger.error("Order expired: '$orderId'", context = TAG) | ||||||
| return null | ||||||
| } | ||||||
| if (condition(order)) { | ||||||
| return order | ||||||
| } | ||||||
| }, | ||||||
| onFailure = { | ||||||
| consecutiveErrors++ | ||||||
| Logger.warn( | ||||||
| "Failed to fetch order '$orderId' (attempt $consecutiveErrors/$MAX_CONSECUTIVE_ERRORS)", | ||||||
| e = it, | ||||||
|
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. The named parameter CLAUDE.md Rule:
Reference: CLAUDE.md:180 Fix:
Suggested change
|
||||||
| context = TAG | ||||||
| ) | ||||||
|
|
||||||
| if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) { | ||||||
| Logger.error("Too many consecutive errors polling order '$orderId', giving up", context = TAG) | ||||||
| return null | ||||||
| } | ||||||
| } | ||||||
| ) | ||||||
|
|
||||||
| delay(POLL_INTERVAL_MS) | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -539,6 +560,7 @@ class TransferViewModel @Inject constructor( | |||||
| private const val TAG = "TransferViewModel" | ||||||
| private const val MIN_STEP_DELAY_MS = 500L | ||||||
| private const val POLL_INTERVAL_MS = 2_500L | ||||||
| private const val MAX_CONSECUTIVE_ERRORS = 5 | ||||||
| const val LN_SETUP_STEP_0 = 0 | ||||||
| const val LN_SETUP_STEP_1 = 1 | ||||||
| const val LN_SETUP_STEP_2 = 2 | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,4 +172,24 @@ class BlocktankRepoTest : BaseUnitTest() { | |
| assertTrue(result.isSuccess) | ||
| assertNull(result.getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `refreshOrders returns failure when server throws`() = test { | ||
| sut = createSut() | ||
| wheneverBlocking { coreService.blocktank.orders(refresh = true) }.thenThrow(RuntimeException("Network error")) | ||
|
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. According to CLAUDE.md:
Move the mock setup to the 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. The CLAUDE.md Rule:
Reference: CLAUDE.md:205-206 Suggested Fix: Example pattern from existing tests: @Before
fun setUp() = runBlocking {
wheneverBlocking { coreService.blocktank.orders(refresh = true) }.thenThrow(RuntimeException("Network error"))
} |
||
|
|
||
| val result = sut.refreshOrders() | ||
|
|
||
| assertTrue(result.isFailure) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getOrder returns failure when refresh fails`() = test { | ||
| sut = createSut() | ||
| wheneverBlocking { coreService.blocktank.orders(refresh = true) }.thenThrow(RuntimeException("Network error")) | ||
|
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. According to CLAUDE.md:
Move the mock setup to the |
||
|
|
||
| val result = sut.getOrder(testOrder1.id, refresh = true) | ||
|
|
||
| assertTrue(result.isFailure) | ||
| } | ||
| } | ||
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.
According to CLAUDE.md:
The Logger.warn call should pass the throwable without the named parameter syntax.