creating an injectable request executor to enable unit tests network request (without hitting the network)

This commit is contained in:
Adam Brown 2021-09-22 12:37:30 +01:00
parent 6672ab3966
commit 0a2d7d709b
2 changed files with 23 additions and 2 deletions

View File

@ -23,7 +23,6 @@ import org.matrix.android.sdk.internal.database.model.PusherEntity
import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.task.Task
import org.matrix.android.sdk.internal.util.awaitTransaction
import javax.inject.Inject
@ -35,6 +34,7 @@ internal interface AddPusherTask : Task<AddPusherTask.Params, Unit> {
internal class DefaultAddPusherTask @Inject constructor(
private val pushersAPI: PushersAPI,
@SessionDatabase private val monarchy: Monarchy,
private val requestExecutor: RequestExecutor,
private val globalErrorReceiver: GlobalErrorReceiver
) : AddPusherTask {
override suspend fun execute(params: AddPusherTask.Params) {
@ -52,7 +52,7 @@ internal class DefaultAddPusherTask @Inject constructor(
}
private suspend fun setPusher(pusher: JsonPusher) {
executeRequest(globalErrorReceiver) {
requestExecutor.executeRequest(globalErrorReceiver) {
pushersAPI.setPusher(pusher)
}
monarchy.awaitTransaction { realm ->
@ -75,3 +75,17 @@ internal class DefaultAddPusherTask @Inject constructor(
}
}
}
internal interface RequestExecutor {
suspend fun <DATA> executeRequest(globalErrorReceiver: GlobalErrorReceiver?,
canRetry: Boolean = false,
maxDelayBeforeRetry: Long = 32_000L,
maxRetriesCount: Int = 4,
requestBlock: suspend () -> DATA): DATA
}
internal object DefaultRequestExecutor : RequestExecutor {
override suspend fun <DATA> executeRequest(globalErrorReceiver: GlobalErrorReceiver?, canRetry: Boolean, maxDelayBeforeRetry: Long, maxRetriesCount: Int, requestBlock: suspend () -> DATA): DATA {
return executeRequest(globalErrorReceiver, canRetry, maxDelayBeforeRetry, maxRetriesCount, requestBlock)
}
}

View File

@ -30,6 +30,7 @@ import org.matrix.android.sdk.internal.session.pushers.gateway.PushGatewayNotify
import org.matrix.android.sdk.internal.session.room.notification.DefaultSetRoomNotificationStateTask
import org.matrix.android.sdk.internal.session.room.notification.SetRoomNotificationStateTask
import retrofit2.Retrofit
import javax.inject.Singleton
@Module
internal abstract class PushersModule {
@ -48,6 +49,12 @@ internal abstract class PushersModule {
fun providesPushRulesApi(retrofit: Retrofit): PushRulesApi {
return retrofit.create(PushRulesApi::class.java)
}
@Provides
@JvmStatic
fun providesRequestExecutor(): RequestExecutor {
return DefaultRequestExecutor
}
}
@Binds