summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/cache/test/CacheTestCase.java
blob: bd1777bd75e29931dc56679a04aa08d849318be9 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.cache.test;

import junit.framework.TestCase;

import com.yahoo.search.result.Hit;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.statistics.Statistics;
import com.yahoo.prelude.cache.Cache;
import com.yahoo.prelude.cache.QueryCacheKey;

@SuppressWarnings({"rawtypes", "unchecked"})
public class CacheTestCase extends TestCase {

    private Result getSomeResult(Query q, String id) {
        Result r = new Result(q);
        r.hits().add(new Hit(id, 10));
        return r;
    }

    public void testBasicGet() {
        Cache<QueryCacheKey, Result> cache=new Cache<>(100*1024,3600, 100000, Statistics.nullImplementation);
        Query q = new Query("/std_xmls_a00?hits=5&offset=5&query=flowers+shop&tracelevel=4&objid=ffffffffffffffff");
        Query q2 = new Query("/std_xmls_a00?hits=5&offset=5&query=flowers+shop&tracelevel=4&objid=ffffffffffffffff");
        QueryCacheKey qk = new QueryCacheKey(q);
        QueryCacheKey qk2 = new QueryCacheKey(q2);
        Result r = getSomeResult(q, "foo");
        Result r2 = getSomeResult(q, "bar");
        assertNull(cache.get(qk));
        cache.put(qk, r);
        assertNotNull(cache.get(qk));
        assertEquals(cache.get(qk), r);
        cache.put(qk2, r);
        assertEquals(cache.get(qk2), r);
        cache.put(qk, r2);
        assertEquals(cache.get(qk), r2);
    }

    public void testPutTooLarge() {
        byte[] tenKB = new byte[10*1024];
        for (int i = 0 ; i <10*1024 ; i++) {
                tenKB[i]=127;
        }
        byte[] sevenKB = new byte[7*1024];
        for (int i = 0 ; i <7*1024 ; i++) {
                sevenKB[i]=127;
        }
        Cache cache=new Cache(9*1024,3600, 100*1024, Statistics.nullImplementation); // 9 KB
        assertFalse(cache.put("foo", tenKB));
        assertTrue(cache.put("foo", sevenKB));
        assertEquals(cache.get("foo"), sevenKB);
    }

    public void testInvalidate() {
        byte[] tenKB = new byte[10*1024];
        for (int i = 0 ; i <10*1024 ; i++) {
                tenKB[i]=127;
        }
        byte[] sevenKB = new byte[7*1024];
        for (int i = 0 ; i <7*1024 ; i++) {
                sevenKB[i]=127;
        }
        Cache cache=new Cache(11*1024,3600, 100*1024, Statistics.nullImplementation); // 11 KB
        assertTrue(cache.put("foo", sevenKB));
        assertTrue(cache.put("bar", tenKB));
        assertNull(cache.get("foo"));
        assertEquals(cache.get("bar"), tenKB);
    }

    public void testInvalidateLRU() {
        Cache cache=new Cache(10*1024,3600, 100*1024, Statistics.nullImplementation); // 10 MB
        byte[] fiveKB = new byte[5*1024];
        for (int i = 0 ; i <5*1024 ; i++) {
                fiveKB[i]=127;
        }

        byte[] twoKB = new byte[2*1024];
        for (int i = 0 ; i <2*1024 ; i++) {
                twoKB[i]=127;
        }

        byte[] fourKB = new byte[4*1024];
        for (int i = 0 ; i <4*1024 ; i++) {
                fourKB[i]=127;
        }
        assertTrue(cache.put("five", fiveKB));
        assertTrue(cache.put("two", twoKB));
        Object dummy = cache.get("five"); // Makes two LRU
        assertEquals(dummy, fiveKB);
        assertTrue(cache.put("four", fourKB));
        assertNull(cache.get("two"));
        assertEquals(cache.get("five"), fiveKB);
        assertEquals(cache.get("four"), fourKB);

        // Same, without the access, just to check
        cache=new Cache(10*1024,3600, 100*1024, Statistics.nullImplementation); // 10 KB
        assertTrue(cache.put("five", fiveKB));
        assertTrue(cache.put("two", twoKB));
        assertTrue(cache.put("four", fourKB));
        assertEquals(cache.get("two"), twoKB);
        assertNull(cache.get("five"));
        assertEquals(cache.get("four"), fourKB);
    }

    public void testPutSameKey() {
        Cache cache=new Cache(10*1024,3600, 100*1024, Statistics.nullImplementation); // 10 MB
        byte[] fiveKB = new byte[5*1024];
        for (int i = 0 ; i <5*1024 ; i++) {
            fiveKB[i]=127;
        }

        byte[] twoKB = new byte[2*1024];
        for (int i = 0 ; i <2*1024 ; i++) {
            twoKB[i]=127;
        }

        byte[] fourKB = new byte[4*1024];
        for (int i = 0 ; i <4*1024 ; i++) {
            fourKB[i]=127;
        }
        assertTrue(cache.put("five", fiveKB));
        assertTrue(cache.put("two", twoKB));
        assertEquals(cache.get("two"), twoKB);
        assertEquals(cache.get("five"), fiveKB);
        assertTrue(cache.put("five", twoKB));
        assertEquals(cache.get("five"), twoKB);
        assertEquals(cache.get("two"), twoKB);
    }

    public void testExpire() throws InterruptedException {
        Cache cache=new Cache(10*1024,50, 10000, Statistics.nullImplementation); // 10 KB, 50ms expire
        boolean success = false;
        for (int tries = 0; tries < 10; tries++) {
            long before = System.currentTimeMillis();
            cache.put("foo", "bar");
            cache.put("hey", "ho");
            Object got1 = cache.get("foo");
            Object got2 = cache.get("hey");
            long after = System.currentTimeMillis();
            if (after - before < 50) {
                assertEquals(got1, "bar");
                assertEquals(got2, "ho");
                success = true;
                break;
            }
        }
        assertTrue(success);
        Thread.sleep(100);
        assertNull(cache.get("foo"));
        assertNull(cache.get("hey"));
    }

    public void testInsertSame() {
        Cache cache=new Cache(100*1024,500, 100000, Statistics.nullImplementation); // 100 KB, .5 sec expire
        Query q =  new Query("/std_xmls_a00?hits=5&offset=5&query=flowers+shop&tracelevel=4&objid=ffffffffffffffff");
        Result r = getSomeResult(q, "foo");
        QueryCacheKey k = new QueryCacheKey(q);
        cache.put(k, r);
        assertEquals(1, cache.size());
        q =  new Query("/std_xmls_a00?hits=5&offset=5&query=flowers+shop&tracelevel=4&objid=ffffffffffffffff");
        k = new QueryCacheKey(q);
        cache.put(k, r);
        assertEquals(1, cache.size());
    }

    public void testMaxSize() {
        Cache cache=new Cache(20*1024,500, 3*1024, Statistics.nullImplementation);
        byte[] fourKB = new byte[4*1024];
        for (int i = 0 ; i <4*1024 ; i++) {
            fourKB[i]=127;
        }
        byte[] twoKB = new byte[2*1024];
        for (int i = 0 ; i <2*1024 ; i++) {
            twoKB[i]=127;
        }
        assertFalse(cache.put("four", fourKB));
        assertTrue(cache.put("two", twoKB));
        assertNull(cache.get("four"));
        assertNotNull(cache.get("two"));
    }

}