Skip to content

Commit

Permalink
Update logic for finding optimal keepalive (#41)
Browse files Browse the repository at this point in the history
* Reset state when optimal keepalive is failed

* Update keepalive handling logic on failure

* Reset state on network change without connected check

* Fix unit tests
  • Loading branch information
deepanshu42 authored Sep 13, 2022
1 parent cec6975 commit 01ebb11
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal class AdaptiveKeepAliveStateHandler(
)
} else {
val currentUpperBound = keepAlive.keepAliveMinutes - 1
if (state.lastSuccessfulKA == currentUpperBound) {
if (state.lastSuccessfulKA >= currentUpperBound) {
state = state.copy(
currentUpperBound = currentUpperBound,
isOptimalKeepAlive = true,
Expand Down Expand Up @@ -191,8 +191,7 @@ internal class AdaptiveKeepAliveStateHandler(
keepAlive.keepAliveMinutes == state.currentKA
}

@VisibleForTesting
internal fun resetState() {
fun resetState() {
state = state.copy(
lastSuccessfulKA = state.lowerBound - state.step,
isOptimalKeepAlive = false,
Expand All @@ -201,7 +200,8 @@ internal class AdaptiveKeepAliveStateHandler(
currentKA = -1,
currentKAFailureCount = 0,
probeCount = 0,
convergenceTime = 0
convergenceTime = 0,
optimalKAFailureCount = 0
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ internal class OptimalKeepAliveCalculator(
object : NetworkStateListener {
override fun onStateChanged(activeNetworkState: NetworkState) {
synchronized(this) {
if (activeNetworkState.isConnected) {
val networkType = networkUtils.getNetworkType(activeNetworkState.netInfo)
val networkName = networkUtils.getNetworkName(activeNetworkState.netInfo)
onNetworkStateChanged(networkType, networkName)
}
val networkType = networkUtils.getNetworkType(activeNetworkState.netInfo)
val networkName = networkUtils.getNetworkName(activeNetworkState.netInfo)
onNetworkStateChanged(networkType, networkName)
}
}
}
Expand Down Expand Up @@ -108,6 +106,7 @@ internal class OptimalKeepAliveCalculator(
stateHandler.updateOptimalKeepAliveFailureState()
if (stateHandler.isOptimalKeepAliveFailureLimitExceeded()) {
stateHandler.removeStateFromPersistence()
stateHandler.resetState()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class OptimalKeepAliveCalculatorTest {
}

@Test
fun `test onStateChanged should notify state handler when network is connected`() {
fun `test onStateChanged should notify state handler`() {
val networkState = mock<NetworkState>()
val netInfo = mock<NetworkInfo>()
whenever(networkState.isConnected).thenReturn(true)
whenever(networkState.netInfo).thenReturn(netInfo)
val networkType = 1
val networkName = "test-network"
Expand All @@ -52,23 +51,12 @@ class OptimalKeepAliveCalculatorTest {
optimalKeepAliveCalculator.networkStateListener.onStateChanged(networkState)

verify(stateHandler).onNetworkChanged(networkType, networkName)
verify(networkState).isConnected
verify(networkState, times(2)).netInfo
verify(networkUtils).getNetworkType(netInfo)
verify(networkUtils).getNetworkName(netInfo)
verifyNoMoreInteractions(networkState)
}

@Test
fun `test onStateChanged should not notify state handler when network is not connected`() {
val networkState = mock<NetworkState>()
whenever(networkState.isConnected).thenReturn(false)

optimalKeepAliveCalculator.networkStateListener.onStateChanged(networkState)

verify(networkState).isConnected
}

@Test
fun `test getUnderTrialKeepAlive when optimal keep alive is already found`() {
val optimalKeepAlive = mock<KeepAlive>()
Expand Down Expand Up @@ -271,6 +259,7 @@ class OptimalKeepAliveCalculatorTest {
verify(stateHandler).updateOptimalKeepAliveFailureState()
verify(stateHandler).isOptimalKeepAliveFailureLimitExceeded()
verify(stateHandler).removeStateFromPersistence()
verify(stateHandler).resetState()
}

@Test
Expand Down

0 comments on commit 01ebb11

Please sign in to comment.