Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests to EmbraceApiService #60

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package io.embrace.android.embracesdk.comms.api
import io.embrace.android.embracesdk.config.remote.RemoteConfig

internal class CachedConfig(
val config: RemoteConfig? = null,
val remoteConfig: RemoteConfig? = null,
val eTag: String? = null
) {
fun isValid() = config != null && eTag != null
fun isValid() = remoteConfig != null && eTag != null
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class EmbraceApiService(
init {
networkConnectivityService.addNetworkConnectivityListener(this)
lastNetworkStatus = networkConnectivityService.getCurrentNetworkStatus()
deliveryRetryManager.setPostExecutor(this::executePost)
deliveryRetryManager.setRetryMethod(this::executePost)
}

/**
Expand All @@ -57,31 +57,10 @@ internal class EmbraceApiService(
override fun getConfig(): RemoteConfig? {
var request = prepareConfigRequest(configUrl)
val cachedResponse = cachedConfigProvider(configUrl, request)

if (cachedResponse.isValid()) { // only bother if we have a useful response.
request = request.copy(eTag = cachedResponse.eTag)
}
val response = apiClient.executeGet(request)
return handleRemoteConfigResponse(response, cachedResponse.config)
}

override fun getCachedConfig(): CachedConfig {
val request = prepareConfigRequest(configUrl)
return cachedConfigProvider(configUrl, request)
}

private fun prepareConfigRequest(url: String) = ApiRequest(
contentType = "application/json",
userAgent = "Embrace/a/" + BuildConfig.VERSION_NAME,
accept = "application/json",
url = EmbraceUrl.create(url),
httpMethod = HttpMethod.GET,
)

private fun handleRemoteConfigResponse(
response: ApiResponse<String>,
cachedConfig: RemoteConfig?
): RemoteConfig? {
return when (response.statusCode) {
HttpURLConnection.HTTP_OK -> {
logger.logInfo("Fetched new config successfully.")
Expand All @@ -91,7 +70,7 @@ internal class EmbraceApiService(

HttpURLConnection.HTTP_NOT_MODIFIED -> {
logger.logInfo("Confirmed config has not been modified.")
cachedConfig
cachedResponse.remoteConfig
}

ApiClient.NO_HTTP_RESPONSE -> {
Expand All @@ -106,6 +85,19 @@ internal class EmbraceApiService(
}
}

override fun getCachedConfig(): CachedConfig {
val request = prepareConfigRequest(configUrl)
return cachedConfigProvider(configUrl, request)
}

private fun prepareConfigRequest(url: String) = ApiRequest(
contentType = "application/json",
userAgent = "Embrace/a/" + BuildConfig.VERSION_NAME,
accept = "application/json",
url = EmbraceUrl.create(url),
httpMethod = HttpMethod.GET,
)

override fun onNetworkConnectivityStatusChanged(status: NetworkStatus) {
lastNetworkStatus = status
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal interface DeliveryRetryManager {
fun scheduleForRetry(request: ApiRequest, payload: ByteArray)

/**
* Sets the executor that will be used to retry failed API calls.
* Sets the method to run to retry an [ApiRequest]
*/
fun setPostExecutor(postExecutor: (request: ApiRequest, payload: ByteArray) -> Unit)
fun setRetryMethod(retryMethod: (request: ApiRequest, payload: ByteArray) -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class EmbraceDeliveryRetryManager(
private val retryQueue: DeliveryFailedApiCalls by lazy { cacheManager.loadFailedApiCalls() }
private var lastRetryTask: ScheduledFuture<*>? = null
private var lastNetworkStatus: NetworkStatus = NetworkStatus.UNKNOWN
private lateinit var postExecutor: (request: ApiRequest, payload: ByteArray) -> Unit
private lateinit var retryMethod: (request: ApiRequest, payload: ByteArray) -> Unit

init {
logger.logDeveloper(TAG, "Starting DeliveryRetryManager")
Expand All @@ -32,10 +32,10 @@ internal class EmbraceDeliveryRetryManager(
}

/**
* Sets the executor that will be used to retry failed API calls.
* Sets the method to execute to retry requests
*/
override fun setPostExecutor(postExecutor: (request: ApiRequest, payload: ByteArray) -> Unit) {
this.postExecutor = postExecutor
override fun setRetryMethod(retryMethod: (request: ApiRequest, payload: ByteArray) -> Unit) {
this.retryMethod = retryMethod
}

/**
Expand Down Expand Up @@ -175,7 +175,7 @@ internal class EmbraceDeliveryRetryManager(
if (payload != null) {
try {
logger.logDeveloper(TAG, "Retrying failed API call")
postExecutor(call.apiRequest, payload)
retryMethod(call.apiRequest, payload)
cacheManager.deletePayload(call.cachedPayload)
} catch (ex: Exception) {
logger.logDeveloper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ internal class EmbraceConfigService @JvmOverloads constructor(
fun loadConfigFromCache() {
logger.logDeveloper("EmbraceConfigService", "Attempting to load config from cache")
val cachedConfig = apiService.getCachedConfig()
val obj = cachedConfig.config
val obj = cachedConfig.remoteConfig

if (obj != null) {
val oldConfig = configProp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.embrace.android.embracesdk.fakes.FakeClock
import io.embrace.android.embracesdk.worker.ExecutorName
import io.embrace.android.embracesdk.worker.WorkerThreadModule
import kotlin.reflect.KFunction1
import kotlin.reflect.KFunction2

/**
* Version of [WorkerThreadModule] used for tests that uses and exposes [BlockableExecutorService] and [BlockingScheduledExecutorService]
Expand All @@ -16,7 +17,7 @@ import kotlin.reflect.KFunction1
*/
internal class FakeWorkerThreadModule(
executorProvider: KFunction1<Boolean, BlockableExecutorService> = ::BlockableExecutorService,
scheduledExecutorProvider: KFunction1<FakeClock, BlockingScheduledExecutorService> = ::BlockingScheduledExecutorService,
scheduledExecutorProvider: KFunction2<FakeClock, Boolean, BlockingScheduledExecutorService> = ::BlockingScheduledExecutorService,
private val clock: FakeClock = FakeClock(),
private val blockingMode: Boolean = false
) : WorkerThreadModule {
Expand All @@ -27,7 +28,7 @@ internal class FakeWorkerThreadModule(

private val scheduledExecutorServices =
ExecutorName.values().associateWith {
scheduledExecutorProvider(clock)
scheduledExecutorProvider(clock, blockingMode)
}

override fun backgroundExecutor(executorName: ExecutorName): BlockableExecutorService {
Expand Down
Loading