UNPKG

als-path-tree

Version:

A Node.js library for managing a virtual file system structure, offering functionalities for file and directory operations with a shared storage system.

148 lines (121 loc) 5.43 kB
const MemoryMonitor = require('../lib/memory-monitor'); const { describe, it, beforeEach } = require('node:test'); const assert = require('node:assert'); const os = require('os'); const Directory = require('../lib/directory') describe('MemoryMonitor Class Tests', () => { let memoryMonitor; let fakeFile; beforeEach(() => { fakeFile = { accessCount: 3, age: 5, // Age in hours meta: { size: 1024 }, // 1KB _buffer: Buffer.from('test'), }; memoryMonitor = new MemoryMonitor({}); MemoryMonitor.options.thresholdPercentage = 10 MemoryMonitor.options.maxStoreTime = 1000 * 60 * 60 * 24 // 24 hours MemoryMonitor.options.thresholdRating = 3 MemoryMonitor.options.weights = { accessCount: 5, lastAccess: -3, size: -1, } }); describe('Memory Monitoring', () => { it('should calculate free memory percentage correctly', () => { const freeMemoryPercentage = (os.freemem() / os.totalmem()) * 100; assert.strictEqual(memoryMonitor.freeMemoryPercentage.toFixed(2), freeMemoryPercentage.toFixed(2)); }); it('should determine if memory is okay based on thresholdPercentage', () => { MemoryMonitor.options.thresholdPercentage = 50; // Set a custom threshold const isMemOk = memoryMonitor.freeMemoryPercentage > 50; assert.strictEqual(memoryMonitor.isMemOk, isMemOk); }); }); describe('clear() Method', () => { it('should not clear files if memory is okay', () => { memoryMonitor.filesWithContent.add(fakeFile); memoryMonitor.clear(); assert(memoryMonitor.filesWithContent.has(fakeFile)); }); it('should clear files exceeding maxStoreTime', () => { fakeFile.age = 25; // Set age beyond maxStoreTime (24 hours) MemoryMonitor.options.thresholdPercentage = 60 const logs = [] MemoryMonitor.options.logger = { warn: (msg) => logs.push(msg), log: () => {}, error:() => {} } memoryMonitor.filesWithContent.add(fakeFile); memoryMonitor.clear(true); assert.strictEqual(fakeFile._buffer, undefined); assert(!memoryMonitor.filesWithContent.has(fakeFile)); assert(logs.length === 1) }); it('should clear files with low ratings', () => { fakeFile.age = 1; fakeFile.accessCount = 0; // Low access count MemoryMonitor.options.thresholdPercentage = 70 const logs = [] memoryMonitor.logger = { warn: (msg) => logs.push(msg) } memoryMonitor.filesWithContent.add(fakeFile); memoryMonitor.clear(true); assert.strictEqual(fakeFile._buffer, undefined); assert(!memoryMonitor.filesWithContent.has(fakeFile)); }); it('should not clear files with high ratings', () => { fakeFile.age = 1; fakeFile.accessCount = 10; // High access count memoryMonitor.filesWithContent.add(fakeFile); memoryMonitor.clear(); assert.notStrictEqual(fakeFile._buffer, null); assert(memoryMonitor.filesWithContent.has(fakeFile)); }); }); describe('add() Method', () => { it('should add a file to filesWithContent and call clear()', () => { let clearHasBeenCalled = false memoryMonitor.filesWithContent.add = function (file) { this.hasBeenCalled = true; Set.prototype.add.call(this, file); }; memoryMonitor.clear = function () { clearHasBeenCalled = true; }; memoryMonitor.add(fakeFile); assert(memoryMonitor.filesWithContent.has(fakeFile)); assert(memoryMonitor.filesWithContent.hasBeenCalled, 'add() was not called on Set'); assert(clearHasBeenCalled, 'clear() was not called'); }); }); }); describe('MemoryMonitor', () => { let memoryMonitor; let rootDir; beforeEach(() => { memoryMonitor = new MemoryMonitor(); rootDir = new Directory('root'); }); it('adds a file to memory monitor and clears when memory is low', () => { const file = rootDir.addFile('file.txt', { size: 1024 }, 'test content'); memoryMonitor.add(file); assert.strictEqual(memoryMonitor.filesWithContent.has(file), true); // Force memory clearing memoryMonitor.clear(true); assert.strictEqual(memoryMonitor.filesWithContent.has(file), false); }); it('removes specific files from memory monitor', () => { const file1 = rootDir.addFile('file1.txt', { size: 512 }, 'content1'); const file2 = rootDir.addFile('file2.txt', { size: 1024 }, 'content2'); memoryMonitor.add(file1); memoryMonitor.add(file2); memoryMonitor.remove(file1); assert.strictEqual(memoryMonitor.filesWithContent.has(file1), false); assert.strictEqual(memoryMonitor.filesWithContent.has(file2), true); }); it('does not clear memory if memory is sufficient', () => { const file = rootDir.addFile('file.txt', { size: 512 }, 'test content'); memoryMonitor.add(file); memoryMonitor.clear(); assert.strictEqual(memoryMonitor.filesWithContent.has(file), true); }); });