Skip to content

Commit

Permalink
Add more tests to Cache builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Jan 8, 2023
1 parent e036ce8 commit e814316
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,8 @@ public Consumer<T> finisher() {
public synchronized void close() throws IOException {
delegate.close();
}

Cache delegate() {
return delegate;
}
}
12 changes: 12 additions & 0 deletions src/test/kotlin/io/github/nstdio/http/ext/DiskCacheBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.assertj.core.api.Assertions.assertThatNullPointerException
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.nio.file.Path

internal class DiskCacheBuilderTest {
@ParameterizedTest
Expand All @@ -33,6 +34,17 @@ internal class DiskCacheBuilderTest {
assertThatIllegalArgumentException()
.isThrownBy { builder.maxItems(maxItems) }
}

@ParameterizedTest
@ValueSource(ints = [1, 2, 256])
fun `Should not throw when max items is positive`(maxItems: Int) {
//given
val builder = Cache.newDiskCacheBuilder()
.dir(Path.of("abc"))

//when + then
builder.maxItems(maxItems).build()
}

@Test
fun shouldThrowWhenBuildWithoutDir() {
Expand Down
18 changes: 7 additions & 11 deletions src/test/kotlin/io/github/nstdio/http/ext/InMemoryCacheTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import java.net.http.HttpRequest
import java.net.http.HttpResponse.ResponseInfo
import java.time.Clock
import java.util.stream.Collectors.toList
import java.util.stream.IntStream

internal class InMemoryCacheTest {
private lateinit var cache: InMemoryCache
Expand Down Expand Up @@ -70,11 +69,12 @@ internal class InMemoryCacheTest {
fun shouldWorkAsLRU() {
//given
val maxItems = 2
cache = InMemoryCache(maxItems, -1)
val requests = uris(10)
.stream()
.map { uri: URI? -> HttpRequest.newBuilder(uri).build() }
.collect(toList())
cache = (Cache.newInMemoryCacheBuilder()
.maxItems(maxItems)
.size(-1)
.build() as SynchronizedCache).delegate() as InMemoryCache

val requests = uris(10).map { HttpRequest.newBuilder(it).build() }
val expected = requests.subList(requests.size - maxItems, requests.size)
for (request in requests) {
val e = cacheEntry(java.util.Map.of(), request)
Expand Down Expand Up @@ -198,11 +198,7 @@ internal class InMemoryCacheTest {
}

fun uris(size: Int): List<URI> {
val baseUri = URI.create("http://example.com/")
return IntStream.rangeClosed(1, size)
.mapToObj { it.toString() }
.map { baseUri.resolve(it) }
.collect(toList())
return (1..size).map { "https://example.com/$it".toUri() }
}
}
}

0 comments on commit e814316

Please sign in to comment.