1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.file;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
public class FileContentCacheTest {
private final UnixPath unixPath = mock(UnixPath.class);
private final FileContentCache cache = new FileContentCache(unixPath);
private final byte[] content = "content".getBytes(StandardCharsets.UTF_8);
private final byte[] newContent = "new-content".getBytes(StandardCharsets.UTF_8);
@Test
void get() {
when(unixPath.readBytes()).thenReturn(content);
assertArrayEquals(content, cache.get(Instant.ofEpochMilli(0)));
verify(unixPath, times(1)).readBytes();
verifyNoMoreInteractions(unixPath);
// cache hit
assertArrayEquals(content, cache.get(Instant.ofEpochMilli(0)));
verify(unixPath, times(1)).readBytes();
verifyNoMoreInteractions(unixPath);
// cache miss
when(unixPath.readBytes()).thenReturn(newContent);
assertArrayEquals(newContent, cache.get(Instant.ofEpochMilli(1)));
verify(unixPath, times(1 + 1)).readBytes();
verifyNoMoreInteractions(unixPath);
// cache hit both at times 0 and 1
assertArrayEquals(newContent, cache.get(Instant.ofEpochMilli(0)));
verify(unixPath, times(1 + 1)).readBytes();
verifyNoMoreInteractions(unixPath);
assertArrayEquals(newContent, cache.get(Instant.ofEpochMilli(1)));
verify(unixPath, times(1 + 1)).readBytes();
verifyNoMoreInteractions(unixPath);
}
@Test
void updateWith() {
cache.updateWith(content, Instant.ofEpochMilli(2));
assertArrayEquals(content, cache.get(Instant.ofEpochMilli(2)));
verifyNoMoreInteractions(unixPath);
cache.updateWith(newContent, Instant.ofEpochMilli(4));
assertArrayEquals(newContent, cache.get(Instant.ofEpochMilli(4)));
verifyNoMoreInteractions(unixPath);
}
}
|