Skip to content

Commit 46d0a20

Browse files
Fixed init connection being blocked
1 parent 4524dc3 commit 46d0a20

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212

1313
allprojects {
1414
group = "com.kape.android"
15-
version = "0.4.9"
15+
version = "0.4.10"
1616

1717
apply plugin: "org.jlleitschuh.gradle.ktlint"
1818

vpnmanager/vpnservicemanager/src/main/java/com/kape/vpnservicemanager/domain/controllers/StartConnectionController.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ internal class StartConnectionController(
6666
override suspend fun invoke(
6767
protocolConfiguration: VPNServiceManagerConfiguration,
6868
): Result<VPNServiceServerPeerInformation> {
69-
return isServiceCleared()
69+
val precondition = if (isServiceCleared().isFailure) {
70+
stopConnection(DisconnectReason.CLIENT_INITIATED)
71+
.mapCatching { clearCache().getOrThrow() }
72+
} else {
73+
Result.success(Unit)
74+
}
75+
76+
return precondition
7077
.mapCatching {
7178
setProtocolConfiguration(
7279
protocolConfiguration = protocolConfiguration

vpnmanager/vpnservicemanager/src/test/java/com/kape/vpnservicemanager/domain/controllers/StartConnectionControllerTest.kt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ internal class StartConnectionControllerTest {
127127
}
128128

129129
@Test
130-
fun `fail to start when there is a service present`() = runTest {
130+
fun `start successfully when there is a service present and stop succeeds`() = runTest {
131131
// given
132132
val context: Context = ApplicationProvider.getApplicationContext()
133133
val protocolConfiguration: VPNServiceManagerConfiguration =
@@ -160,6 +160,46 @@ internal class StartConnectionControllerTest {
160160
// when
161161
val result = startConnectionController(protocolConfiguration = protocolConfiguration)
162162

163+
// then
164+
assert(result.isSuccess)
165+
assert((stopConnectionMock as StopConnectionMock).invocationsCounter == 1)
166+
assert((clearCacheMock as ClearCacheMock).invocationsCounter == 1)
167+
}
168+
169+
@Test
170+
fun `fail to start when there is a service present and stop fails`() = runTest {
171+
// given
172+
val context: Context = ApplicationProvider.getApplicationContext()
173+
val protocolConfiguration: VPNServiceManagerConfiguration =
174+
GivenModel.vpnServiceManagerConfiguration(context = context)
175+
val isServiceClearedMock: IIsServiceCleared = IsServiceClearedMock(shouldSucceed = false)
176+
val setProtocolConfigurationMock: ISetProtocolConfiguration =
177+
SetProtocolConfigurationMock(shouldSucceed = true)
178+
val setServerPeerInformationMock: ISetServerPeerInformation =
179+
SetServerPeerInformationMock(shouldSucceed = true)
180+
val startConnectionMock: IStartConnection = StartConnectionMock(shouldSucceed = true)
181+
val startReconnectionHandlerMock: IStartReconnectionHandler =
182+
StartReconnectionHandlerMock(shouldSucceed = true)
183+
val getServerPeerInformationMock: IGetServerPeerInformation =
184+
GetServerPeerInformationMock(shouldSucceed = true)
185+
val stopConnectionMock: IStopConnection = StopConnectionMock(shouldSucceed = false)
186+
val clearCacheMock: IClearCache = ClearCacheMock()
187+
val startConnectionController: IStartConnectionController =
188+
GivenController.startConnectionController(
189+
context = context,
190+
isServiceCleared = isServiceClearedMock,
191+
setProtocolConfiguration = setProtocolConfigurationMock,
192+
setServerPeerInformation = setServerPeerInformationMock,
193+
startConnection = startConnectionMock,
194+
startReconnectionHandler = startReconnectionHandlerMock,
195+
getServerPeerInformation = getServerPeerInformationMock,
196+
stopConnection = stopConnectionMock,
197+
clearCache = clearCacheMock
198+
)
199+
200+
// when
201+
val result = startConnectionController(protocolConfiguration = protocolConfiguration)
202+
163203
// then
164204
assert(result.isFailure)
165205
}

0 commit comments

Comments
 (0)