Skip to content

Commit

Permalink
Fix RetryRemoteAction double callback
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Jul 29, 2024
1 parent 4fc3e80 commit cbe0a5a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ internal class RetryingRemoteAction<T>(
for (i in 0 until maxNumberOfAutomaticRetries) {
try {
callback.accept(Result.success(remoteAction.sync()))
return@Runnable
} catch (e: Throwable) {
lastException = e
}
}
callback.accept(Result.failure(PubNubException.from(lastException!!).copy(remoteAction = this)))
lastException?.let { exception ->
callback.accept(Result.failure(PubNubException.from(exception).copy(remoteAction = this)))
}
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class RetryingRemoteActionTest {
executorService,
)
val asyncSynchronization = CountDownLatch(1)
val noFailureCallsSynchronization = CountDownLatch(1)

// when
retryingRemoteAction.async { result ->
Expand All @@ -98,11 +99,17 @@ class RetryingRemoteActionTest {
Assert.assertEquals(expectedValue, it)
verify(exactly = 1) { remoteAction.sync() }
asyncSynchronization.countDown()
}.onFailure {
noFailureCallsSynchronization.countDown()
}
}
if (!asyncSynchronization.await(3, TimeUnit.SECONDS)) {
Assert.fail("Callback have not been called")
}

if (noFailureCallsSynchronization.await(3, TimeUnit.SECONDS)) {
Assert.fail("onFailure has been called when it shouldn't!")
}
}

@Test
Expand Down

0 comments on commit cbe0a5a

Please sign in to comment.