diff --git a/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/endpoints/remoteaction/RetryingRemoteAction.kt b/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/endpoints/remoteaction/RetryingRemoteAction.kt index 27a417203..5759b6e50 100644 --- a/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/endpoints/remoteaction/RetryingRemoteAction.kt +++ b/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/endpoints/remoteaction/RetryingRemoteAction.kt @@ -46,11 +46,14 @@ internal class RetryingRemoteAction( 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))) + } }, ) } diff --git a/pubnub-core/pubnub-core-impl/src/test/kotlin/com/pubnub/api/legacy/endpoints/remoteaction/RetryingRemoteActionTest.kt b/pubnub-core/pubnub-core-impl/src/test/kotlin/com/pubnub/api/legacy/endpoints/remoteaction/RetryingRemoteActionTest.kt index 593bea5b1..154cbcabf 100644 --- a/pubnub-core/pubnub-core-impl/src/test/kotlin/com/pubnub/api/legacy/endpoints/remoteaction/RetryingRemoteActionTest.kt +++ b/pubnub-core/pubnub-core-impl/src/test/kotlin/com/pubnub/api/legacy/endpoints/remoteaction/RetryingRemoteActionTest.kt @@ -90,6 +90,7 @@ class RetryingRemoteActionTest { executorService, ) val asyncSynchronization = CountDownLatch(1) + val noFailureCallsSynchronization = CountDownLatch(1) // when retryingRemoteAction.async { result -> @@ -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