Skip to content

Commit

Permalink
Merge pull request #22 from nkly-wolt/make-repository-interface-suspe…
Browse files Browse the repository at this point in the history
…nding

Make repository functions suspending
  • Loading branch information
nkly-wolt authored Aug 26, 2024
2 parents 82fa3dd + c18465b commit 8eb4d86
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/pre-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ jobs:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Check that the publish plugin works
env:
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGKEY }}
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGPASSWORD }}
uses: gradle/gradle-build-action@v2
with:
arguments: publishToMavenLocal
Expand All @@ -41,4 +38,4 @@ jobs:
- name: Check that the code is formatted
uses: gradle/gradle-build-action@v2
with:
arguments: spotlessCheck
arguments: spotlessCheck
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import kotlinx.serialization.Serializable
import java.time.OffsetDateTime

interface IdempotentResponseRepository {
fun storeResponse(
suspend fun storeResponse(
resource: String,
idempotencyKey: IdempotencyKey,
response: ByteArray,
)

fun getResponseOrLock(
suspend fun getResponseOrLock(
resource: String,
idempotencyKey: IdempotencyKey,
): IdempotencyResponse?

fun deleteExpiredResponses(lastValidDate: OffsetDateTime)
suspend fun deleteExpiredResponses(lastValidDate: OffsetDateTime)
}

data class IdempotencyResponse(
Expand Down
9 changes: 5 additions & 4 deletions src/test/kotlin/IdempotencyPluginTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import io.ktor.server.routing.post
import io.ktor.server.routing.put
import io.ktor.server.testing.ApplicationTestBuilder
import io.ktor.server.testing.testApplication
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
Expand Down Expand Up @@ -272,13 +273,13 @@ class IdempotencyPluginTests {
val response = sendPostRequest(path, idempotencyKey)
assertOkResponse(response, "Hello, world!")
assertServiceCalled(timesInTotal = 1)
verify(exactly = 1) { repository.storeResponse("POST $path", any(), any()) }
coVerify(exactly = 1) { repository.storeResponse("POST $path", any(), any()) }

val response2 = sendPostRequest(path, idempotencyKey)
assertOkResponse(response2, "Hello, world!")
assertServiceCalled(timesInTotal = 1)
// storeResponse should not be called again
verify(exactly = 1) { repository.storeResponse("POST $path", any(), any()) }
coVerify(exactly = 1) { repository.storeResponse("POST $path", any(), any()) }
}

@Test
Expand Down Expand Up @@ -323,7 +324,7 @@ class IdempotencyPluginTests {
assertOkResponse(response1, "Hello, world for get request!")
assertOkResponse(response2, "Hello, world for get request!")

verify(exactly = 0) { repository.storeResponse(any(), any(), any()) }
coVerify(exactly = 0) { repository.storeResponse(any(), any(), any()) }
assertNoEventTriggered()
assertServiceCalled(timesInTotal = 2)
}
Expand Down Expand Up @@ -373,7 +374,7 @@ class IdempotencyPluginTests {
}
}

private fun insertExpiredResponse(
private suspend fun insertExpiredResponse(
idempotencyKey: String,
path: String,
method: HttpMethod = HttpMethod.Post,
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/InMemoryResponseRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.concurrent.ConcurrentHashMap
class InMemoryResponseRepository : IdempotentResponseRepository {
private val responses = ConcurrentHashMap<String, IdempotencyResponse>()

override fun storeResponse(
override suspend fun storeResponse(
resource: String,
idempotencyKey: IdempotencyKey,
response: ByteArray,
Expand All @@ -17,7 +17,7 @@ class InMemoryResponseRepository : IdempotentResponseRepository {
responses[generateKey(resource, idempotencyKey)] = IdempotencyResponse(isInProgress = false, response = response)
}

override fun getResponseOrLock(
override suspend fun getResponseOrLock(
resource: String,
idempotencyKey: IdempotencyKey,
): IdempotencyResponse? {
Expand All @@ -30,7 +30,7 @@ class InMemoryResponseRepository : IdempotentResponseRepository {
return record
}

override fun deleteExpiredResponses(lastValidDate: java.time.OffsetDateTime) {
override suspend fun deleteExpiredResponses(lastValidDate: java.time.OffsetDateTime) {
responses.clear()
}

Expand Down

0 comments on commit 8eb4d86

Please sign in to comment.