Skip to content

Commit

Permalink
test(ClearlyDefinedStorage): Add a test for a SocketTimeoutException
Browse files Browse the repository at this point in the history
Add a test for proper hanlding of a `SocketTimeoutException`. Slightly
refactor `ClearlyDefinedStorage` as needed to pass a custom URL and HTTP
client, similar to `ClearlyDefinedPackageCurationProvider`.

Resolves #6367.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
  • Loading branch information
sschuberth committed Feb 28, 2023
1 parent 3f55e6b commit db60879
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions scanner/src/main/kotlin/storages/ClearlyDefinedStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package org.ossreviewtoolkit.scanner.storages
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking

import okhttp3.OkHttpClient

import org.apache.logging.log4j.kotlin.Logging

import org.ossreviewtoolkit.clients.clearlydefined.ClearlyDefinedService
Expand Down Expand Up @@ -74,13 +76,18 @@ private fun findScanCodeVersion(tools: List<String>, coordinates: Coordinates):
*/
class ClearlyDefinedStorage(
/** The configuration for this storage implementation. */
config: ClearlyDefinedStorageConfiguration
config: ClearlyDefinedStorageConfiguration,
client: OkHttpClient? = null
) : ScanResultsStorage() {
companion object : Logging

constructor(serverUrl: String, client: OkHttpClient? = null) : this(
ClearlyDefinedStorageConfiguration(serverUrl), client
)

/** The service for interacting with ClearlyDefined. */
private val service by lazy {
ClearlyDefinedService.create(config.serverUrl, OkHttpClientHelper.buildClient())
ClearlyDefinedService.create(config.serverUrl, client ?: OkHttpClientHelper.buildClient())
}

override fun readInternal(id: Identifier): Result<List<ScanResult>> =
Expand Down
16 changes: 16 additions & 0 deletions scanner/src/test/kotlin/storages/ClearlyDefinedStorageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import io.kotest.matchers.string.shouldContain

import java.io.File
import java.net.ServerSocket
import java.time.Duration

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -61,6 +62,7 @@ import org.ossreviewtoolkit.model.VcsType
import org.ossreviewtoolkit.model.config.ClearlyDefinedStorageConfiguration
import org.ossreviewtoolkit.scanner.ScanStorageException
import org.ossreviewtoolkit.scanner.ScannerCriteria
import org.ossreviewtoolkit.utils.ort.OkHttpClientHelper

import org.semver4j.Semver

Expand Down Expand Up @@ -223,6 +225,20 @@ class ClearlyDefinedStorageTest : WordSpec({
}

"ClearlyDefinedStorage" should {
"handle a SocketTimeoutException" {
server.stubFor(
get(anyUrl())
.willReturn(aResponse().withFixedDelay(2000))
)
val client = OkHttpClientHelper.buildClient {
readTimeout(Duration.ofSeconds(1))
}

val storage = ClearlyDefinedStorage("http://localhost:${server.port()}", client)

storage.read(TEST_PACKAGE, SCANNER_CRITERIA).shouldBeFailure<ScanStorageException>()
}

"load existing scan results for a package from ClearlyDefined" {
stubHarvestTools(
server, COORDINATES,
Expand Down

0 comments on commit db60879

Please sign in to comment.