Spaces:
Sleeping
Sleeping
File size: 10,521 Bytes
1070765 | 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | import { describe, it, expect } from "vitest";
import { ChunkCache } from "./ChunkCache";
describe("ChunkCache", () => {
describe("basic operations", () => {
it("should create a cache with specified max size", () => {
const cache = new ChunkCache(5);
expect(cache.maxSize).toBe(5);
expect(cache.index).toBe(0);
});
it("should add and retrieve chunks", () => {
const cache = new ChunkCache(5);
cache.addChunkToCache("hash1", 100, 10, null);
cache.addChunkToCache("hash2", 200, 20, null);
const chunk1 = cache.getChunk("hash1", null);
const chunk2 = cache.getChunk("hash2", null);
expect(chunk1).toEqual({ xorbIndex: 100, chunkIndex: 10 });
expect(chunk2).toEqual({ xorbIndex: 200, chunkIndex: 20 });
});
it("should return undefined for non-existent chunks", () => {
const cache = new ChunkCache(5);
const chunk = cache.getChunk("nonexistent", null);
expect(chunk).toBeUndefined();
});
it("should remove chunks from cache", () => {
const cache = new ChunkCache(5);
cache.addChunkToCache("hash1", 100, 10, null);
expect(cache.getChunk("hash1", null)).toBeDefined();
cache.removeChunkFromCache("hash1");
expect(cache.getChunk("hash1", null)).toBeUndefined();
});
});
describe("duplicate handling", () => {
it("should ignore duplicate hashes", () => {
const cache = new ChunkCache(5);
// Add initial chunk
cache.addChunkToCache("hash1", 100, 10, null);
expect(cache.index).toBe(1);
expect(cache.map.size).toBe(1);
// Try to add same hash again - should be ignored
cache.addChunkToCache("hash1", 999, 99, null);
expect(cache.index).toBe(1); // index should not increment
expect(cache.map.size).toBe(1); // map size should not increase
// Original data should be preserved
const chunk = cache.getChunk("hash1", null);
expect(chunk).toEqual({ xorbIndex: 100, chunkIndex: 10 });
});
it("should maintain consistency when adding duplicates mixed with new hashes", () => {
const cache = new ChunkCache(5);
// Add some chunks
cache.addChunkToCache("hash1", 100, 10, null);
cache.addChunkToCache("hash2", 200, 20, null);
cache.addChunkToCache("hash3", 300, 30, null);
expect(cache.index).toBe(3);
expect(cache.map.size).toBe(3);
// Try to add duplicates
cache.addChunkToCache("hash1", 999, 99, null); // duplicate
cache.addChunkToCache("hash4", 400, 40, null); // new
cache.addChunkToCache("hash2", 888, 88, null); // duplicate
expect(cache.index).toBe(4); // only incremented for hash4
expect(cache.map.size).toBe(4);
// Verify all chunks are accessible and have correct data
expect(cache.getChunk("hash1", null)).toEqual({ xorbIndex: 100, chunkIndex: 10 });
expect(cache.getChunk("hash2", null)).toEqual({ xorbIndex: 200, chunkIndex: 20 });
expect(cache.getChunk("hash3", null)).toEqual({ xorbIndex: 300, chunkIndex: 30 });
expect(cache.getChunk("hash4", null)).toEqual({ xorbIndex: 400, chunkIndex: 40 });
});
});
describe("cache overflow and circular buffer behavior", () => {
it("should handle cache overflow correctly", () => {
const cache = new ChunkCache(3); // Small cache size
// Fill the cache to capacity
cache.addChunkToCache("hash1", 100, 10, null);
cache.addChunkToCache("hash2", 200, 20, null);
cache.addChunkToCache("hash3", 300, 30, null);
expect(cache.index).toBe(0); // wrapped around (3 % 3 = 0)
expect(cache.map.size).toBe(3);
// All chunks should be accessible
expect(cache.getChunk("hash1", null)).toBeDefined();
expect(cache.getChunk("hash2", null)).toBeDefined();
expect(cache.getChunk("hash3", null)).toBeDefined();
// Add one more chunk - should evict the oldest (hash1)
cache.addChunkToCache("hash4", 400, 40, null);
expect(cache.index).toBe(1); // wrapped around (4 % 3 = 1)
expect(cache.map.size).toBe(3); // size should remain the same
// hash1 should be evicted, others should remain
expect(cache.getChunk("hash1", null)).toBeUndefined();
expect(cache.getChunk("hash2", null)).toEqual({ xorbIndex: 200, chunkIndex: 20 });
expect(cache.getChunk("hash3", null)).toEqual({ xorbIndex: 300, chunkIndex: 30 });
expect(cache.getChunk("hash4", null)).toEqual({ xorbIndex: 400, chunkIndex: 40 });
});
it("should continue evicting oldest entries as new ones are added", () => {
const cache = new ChunkCache(3);
// Fill cache
cache.addChunkToCache("hash1", 100, 10, null);
cache.addChunkToCache("hash2", 200, 20, null);
cache.addChunkToCache("hash3", 300, 30, null);
// Add more chunks to test multiple evictions
cache.addChunkToCache("hash4", 400, 40, null); // evicts hash1
cache.addChunkToCache("hash5", 500, 50, null); // evicts hash2
cache.addChunkToCache("hash6", 600, 60, null); // evicts hash3
expect(cache.map.size).toBe(3);
// Only the last 3 should remain
expect(cache.getChunk("hash1", null)).toBeUndefined();
expect(cache.getChunk("hash2", null)).toBeUndefined();
expect(cache.getChunk("hash3", null)).toBeUndefined();
expect(cache.getChunk("hash4", null)).toEqual({ xorbIndex: 400, chunkIndex: 40 });
expect(cache.getChunk("hash5", null)).toEqual({ xorbIndex: 500, chunkIndex: 50 });
expect(cache.getChunk("hash6", null)).toEqual({ xorbIndex: 600, chunkIndex: 60 });
});
it("should handle removals during overflow correctly", () => {
const cache = new ChunkCache(3);
// Fill cache
cache.addChunkToCache("hash1", 100, 10, null);
cache.addChunkToCache("hash2", 200, 20, null);
cache.addChunkToCache("hash3", 300, 30, null);
// Remove middle element
cache.removeChunkFromCache("hash2");
expect(cache.map.size).toBe(2);
// Add new elements
cache.addChunkToCache("hash4", 400, 40, null);
cache.addChunkToCache("hash5", 500, 50, null);
// The removal should not affect the eviction logic
expect(cache.getChunk("hash1", null)).toBeUndefined(); // evicted
expect(cache.getChunk("hash2", null)).toBeUndefined(); // removed
expect(cache.getChunk("hash3", null)).toEqual({ xorbIndex: 300, chunkIndex: 30 });
expect(cache.getChunk("hash4", null)).toEqual({ xorbIndex: 400, chunkIndex: 40 });
expect(cache.getChunk("hash5", null)).toEqual({ xorbIndex: 500, chunkIndex: 50 });
});
});
describe("consistency after operations", () => {
it("should maintain consistent state after mixed operations", () => {
const cache = new ChunkCache(4);
// Add initial chunks
cache.addChunkToCache("a", 1, 10, null);
cache.addChunkToCache("b", 2, 20, null);
cache.addChunkToCache("c", 3, 30, null);
// Mix of operations
cache.addChunkToCache("a", 999, 999, null); // duplicate (ignored)
cache.removeChunkFromCache("b"); // removal
cache.addChunkToCache("d", 4, 40, null); // new addition
cache.addChunkToCache("e", 5, 50, null); // new addition - this triggers overflow
cache.addChunkToCache("c", 888, 888, null); // duplicate (ignored)
// Verify final state
// With cache size 4: a(0), b(1, removed), c(2), d(3), e(4 -> 0, wraps and evicts a)
expect(cache.getChunk("a", null)).toBeUndefined(); // evicted by e
expect(cache.getChunk("b", null)).toBeUndefined(); // removed
expect(cache.getChunk("c", null)).toEqual({ xorbIndex: 3, chunkIndex: 30 });
expect(cache.getChunk("d", null)).toEqual({ xorbIndex: 4, chunkIndex: 40 });
expect(cache.getChunk("e", null)).toEqual({ xorbIndex: 5, chunkIndex: 50 });
// Map size should be 3 (a evicted, b removed)
expect(cache.map.size).toBe(3);
});
it("should maintain consistency after cache overflow with mixed operations", () => {
const cache = new ChunkCache(3);
// Fill cache
cache.addChunkToCache("first", 1, 1, null);
cache.addChunkToCache("second", 2, 2, null);
cache.addChunkToCache("third", 3, 3, null);
// Cause overflow with duplicates and removals mixed in
cache.addChunkToCache("fourth", 4, 4, null); // evicts "first"
cache.addChunkToCache("second", 999, 999, null); // duplicate (ignored)
cache.removeChunkFromCache("third"); // removal
cache.addChunkToCache("fifth", 5, 5, null); // evicts "second"
// Final state verification
expect(cache.getChunk("first", null)).toBeUndefined(); // evicted
expect(cache.getChunk("second", null)).toBeUndefined(); // evicted
expect(cache.getChunk("third", null)).toBeUndefined(); // removed
expect(cache.getChunk("fourth", null)).toEqual({ xorbIndex: 4, chunkIndex: 4 });
expect(cache.getChunk("fifth", null)).toEqual({ xorbIndex: 5, chunkIndex: 5 });
cache.addChunkToCache("sixth", 6, 6, null); // new, takes "third" place
expect(cache.getChunk("sixth", null)).toEqual({ xorbIndex: 6, chunkIndex: 6 });
expect(cache.getChunk("fourth", null)).toEqual({ xorbIndex: 4, chunkIndex: 4 });
expect(cache.getChunk("fifth", null)).toEqual({ xorbIndex: 5, chunkIndex: 5 });
cache.addChunkToCache("seventh", 7, 7, null); // new, takes "fourth" place
expect(cache.getChunk("seventh", null)).toEqual({ xorbIndex: 7, chunkIndex: 7 });
expect(cache.getChunk("fourth", null)).toBeUndefined(); // evicted
expect(cache.getChunk("fifth", null)).toEqual({ xorbIndex: 5, chunkIndex: 5 });
expect(cache.map.size).toBe(3);
});
});
describe("negative xorbIndex handling", () => {
it("should handle negative xorbIndex values (remote xorbs)", () => {
const cache = new ChunkCache(5);
cache.addChunkToCache("remote1", -1, 10, null);
cache.addChunkToCache("local1", 1, 20, null);
cache.addChunkToCache("remote2", -100, 30, null);
expect(cache.getChunk("remote1", null)).toEqual({ xorbIndex: -1, chunkIndex: 10 });
expect(cache.getChunk("local1", null)).toEqual({ xorbIndex: 1, chunkIndex: 20 });
expect(cache.getChunk("remote2", null)).toEqual({ xorbIndex: -100, chunkIndex: 30 });
});
});
describe("edge cases", () => {
it("should handle cache with max size of 1", () => {
const cache = new ChunkCache(1);
cache.addChunkToCache("hash1", 1, 1, null);
expect(cache.getChunk("hash1", null)).toBeDefined();
cache.addChunkToCache("hash2", 2, 2, null);
expect(cache.getChunk("hash1", null)).toBeUndefined(); // evicted
expect(cache.getChunk("hash2", null)).toEqual({ xorbIndex: 2, chunkIndex: 2 });
});
it("should handle empty cache operations", () => {
const cache = new ChunkCache(5);
expect(cache.getChunk("nonexistent", null)).toBeUndefined();
cache.removeChunkFromCache("nonexistent"); // should not throw
expect(cache.map.size).toBe(0);
});
});
});
|