-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheClassSpec.js
More file actions
138 lines (115 loc) · 4.98 KB
/
Copy pathCacheClassSpec.js
File metadata and controls
138 lines (115 loc) · 4.98 KB
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
describe("JSCacheClass", function() {
var cache;
var store;
beforeEach(function() {
// before each test create new hash map
store = new Store()
// before each test create new caches
cache2 = new jsCache(6,6, store);
cache = new jsCache(3, 3, store, cache2);
})
it('should create a cache instance', function() {
expect(cache).to.be.an('object');
})
describe('CacheHits', function() {
describe('write(cache hit)', function() {
it('should write to the cache', function() {
cache.writeToCache('abc', 'foobar');
var checkCache = cache.checkCacheSet('abc');
expect(checkCache["data"]).to.deep.equal(['abc', 'foobar']);
expect(checkCache["matchFound"]).to.equal(true);
})
it('should reupdate value in l1', function() {
cache.writeToCache('abc', 'foobar');
cache.writeToCache('abc', 'helloworld');
var checkCache = cache.checkCacheSet('abc');
expect(checkCache["data"]).to.deep.equal(['abc', 'helloworld']);
expect(checkCache["matchFound"]).to.equal(true);
})
it('should update l2 when l1 cache is updated', function() {
// write data to our cache
cache.writeToCache('123', '321');
cache.writeToCache('456', '654');
// use cache method to check for data
var checkCache2 = [
cache2.checkCacheSet('123'),
cache2.checkCacheSet('456')
];
// writing to cache1 should update cache2
expect(checkCache2[0]["data"]).to.deep.equal(['123', '321']);
expect(checkCache2[0]["matchFound"]).to.equal(true);
expect(checkCache2[1]["data"]).to.deep.equal(['456', '654']);
expect(checkCache2[1]["matchFound"]).to.equal(true);
// should not update store
expect(store['123']).to.equal(undefined);
expect(store['456']).to.equal(undefined);
})
})
describe('fetch(cache hit)', function() {
it('should fetch data from the cache', function() {
cache.writeToCache('abc', 'foobar1');
expect(cache.fetchData('abc')).to.equal('foobar1');
})
})
})
describe('CacheMisses', function() {
describe('write(cache miss)', function() {
it('should evict data from the block when full and update the store with the evicted data (writeback)', function() {
// add data to cache
cache.writeToCache('1', 'foobar');
cache.writeToCache('2', 'foobar');
cache.writeToCache('3', 'foobar');
cache.writeToCache('4', 'helloworld');
var checkCacheKey1 = cache.checkCacheSet('1');
var checkCacheKey4 = cache.checkCacheSet('4');
// adding 4 should evict the first item in queue
// 1 [2,3,4]
expect(store['4']).to.equal('helloworld');
// after evicting should not be in cache
expect(checkCacheKey1['matchFound']).to.equal(false);
expect(checkCacheKey1['data']).to.equal(undefined);
})
it('should write to l2 and then allocate to l1 if cache miss on l1', function() {
// write only to l2
cache2.writeToCache('1a', 'a1');
// simulate a cache miss by fetching from data that l1 doesnt have but l2 does
cache.fetchData('1a');
// get the state of the set
var checkCache1 = cache.checkCacheSet('1a');
// check to see if the data is in the l1 cache
expect(checkCache1['data']).to.deep.equal(['1a','a1']);
})
})
describe('fetch(cache miss)', function() {
it('should fetch from store if not in cache', function() {
// put item into store
store['cde'] = 'edc123';
// cache is empty should get the information from the cache
expect(cache.fetchData('cde')).to.equal('edc123');
})
it('should fetch from parent cache if not in current cache', function() {
// write data only to l2 cache
cache2.writeToCache('def', '123fed');
// request to l1 cache
expect(cache.fetchData('def')).to.equal('123fed');
})
it('should allocate to l2 and l1 if fetched from store', function() {
// set value in store
store['c3'] = '3c';
// initial state of our cache should be empty
var checkCache1 = cache.checkCacheSet('c3');
var checkCache2 = cache2.checkCacheSet('c3');
expect(checkCache1['data']).to.equal(undefined);
expect(checkCache2['data']).to.equal(undefined);
// simulate a l1 and l2 cache miss
cache.fetchData('c3');
// recheck the state of our caches
checkCache1 = cache.checkCacheSet('c3');
checkCache2 = cache2.checkCacheSet('c3');
// data should be allocated to l1 and l2 cache after cache
expect(checkCache2['data']).to.deep.equal(['c3', '3c']);
expect(checkCache1['data']).to.deep.equal(['c3', '3c']);
})
})
})
})