Skip to content

Commit e814316

Browse files
committed
Add more tests to Cache builder.
1 parent e036ce8 commit e814316

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/main/java/io/github/nstdio/http/ext/SynchronizedCache.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,8 @@ public Consumer<T> finisher() {
8282
public synchronized void close() throws IOException {
8383
delegate.close();
8484
}
85+
86+
Cache delegate() {
87+
return delegate;
88+
}
8589
}

src/test/kotlin/io/github/nstdio/http/ext/DiskCacheBuilderTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.assertj.core.api.Assertions.assertThatNullPointerException
2121
import org.junit.jupiter.api.Test
2222
import org.junit.jupiter.params.ParameterizedTest
2323
import org.junit.jupiter.params.provider.ValueSource
24+
import java.nio.file.Path
2425

2526
internal class DiskCacheBuilderTest {
2627
@ParameterizedTest
@@ -33,6 +34,17 @@ internal class DiskCacheBuilderTest {
3334
assertThatIllegalArgumentException()
3435
.isThrownBy { builder.maxItems(maxItems) }
3536
}
37+
38+
@ParameterizedTest
39+
@ValueSource(ints = [1, 2, 256])
40+
fun `Should not throw when max items is positive`(maxItems: Int) {
41+
//given
42+
val builder = Cache.newDiskCacheBuilder()
43+
.dir(Path.of("abc"))
44+
45+
//when + then
46+
builder.maxItems(maxItems).build()
47+
}
3648

3749
@Test
3850
fun shouldThrowWhenBuildWithoutDir() {

src/test/kotlin/io/github/nstdio/http/ext/InMemoryCacheTest.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import java.net.http.HttpRequest
2525
import java.net.http.HttpResponse.ResponseInfo
2626
import java.time.Clock
2727
import java.util.stream.Collectors.toList
28-
import java.util.stream.IntStream
2928

3029
internal class InMemoryCacheTest {
3130
private lateinit var cache: InMemoryCache
@@ -70,11 +69,12 @@ internal class InMemoryCacheTest {
7069
fun shouldWorkAsLRU() {
7170
//given
7271
val maxItems = 2
73-
cache = InMemoryCache(maxItems, -1)
74-
val requests = uris(10)
75-
.stream()
76-
.map { uri: URI? -> HttpRequest.newBuilder(uri).build() }
77-
.collect(toList())
72+
cache = (Cache.newInMemoryCacheBuilder()
73+
.maxItems(maxItems)
74+
.size(-1)
75+
.build() as SynchronizedCache).delegate() as InMemoryCache
76+
77+
val requests = uris(10).map { HttpRequest.newBuilder(it).build() }
7878
val expected = requests.subList(requests.size - maxItems, requests.size)
7979
for (request in requests) {
8080
val e = cacheEntry(java.util.Map.of(), request)
@@ -198,11 +198,7 @@ internal class InMemoryCacheTest {
198198
}
199199

200200
fun uris(size: Int): List<URI> {
201-
val baseUri = URI.create("http://example.com/")
202-
return IntStream.rangeClosed(1, size)
203-
.mapToObj { it.toString() }
204-
.map { baseUri.resolve(it) }
205-
.collect(toList())
201+
return (1..size).map { "https://example.com/$it".toUri() }
206202
}
207203
}
208204
}

0 commit comments

Comments
 (0)