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
63
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.logging;
import org.junit.Test;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;
public class AccessLogSamplerTest {
private final List<String> uris = new ArrayList<>();
private CircularArrayAccessLogKeeper circularArrayAccessLogKeeperMock = new CircularArrayAccessLogKeeper() {
@Override
public void addUri(String uri) {
uris.add(uri);
}
};
private AccessLogSampler accessLogSampler = new AccessLogSampler(circularArrayAccessLogKeeperMock);
@Test
public void testAFewLines() {
accessLogSampler.log(createLogEntry(200, "/search/foo"));
accessLogSampler.log(createLogEntry(500, "/search/bar"));
accessLogSampler.log(createLogEntry(500, "bar"));
accessLogSampler.log(createLogEntry(200, "/search/what"));
assertThat(uris, contains("/search/foo", "/search/what"));
}
@Test
public void testSubSampling() {
for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
accessLogSampler.log(createLogEntry(200, "/search/" + String.valueOf(i)));
}
assertThat(uris.size(), is(CircularArrayAccessLogKeeper.SIZE));
assertThat(uris, hasItems("/search/0", "/search/1", "/search/2",
"/search/" + String.valueOf(CircularArrayAccessLogKeeper.SIZE - 1)));
uris.clear();
for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
accessLogSampler.log(createLogEntry(200, "/search/fuzz"));
}
assertThat(uris, hasItem("/search/fuzz"));
assertThat(uris.size(), is(1));
for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
accessLogSampler.log(createLogEntry(200, "/search/ball"));
}
assertThat(uris, hasItem("/search/ball"));
assertThat(uris.size(), is(2));
}
private AccessLogEntry createLogEntry(int statusCode, String uri) {
AccessLogEntry accessLogEntry = new AccessLogEntry();
accessLogEntry.setStatusCode(statusCode);
accessLogEntry.setURI(URI.create(uri));
return accessLogEntry;
}
}
|