|
1 |
| -import { describe, expect, it, vi } from 'vitest'; |
2 |
| -import { CircularMap, SizedItem, SizedSet } from '../../src/logBuffer.js'; |
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { Logger } from '../../src/Logger.js'; |
| 3 | +import { LogLevelThreshold } from '../../src/constants.js'; |
| 4 | + |
| 5 | +class TestLogger extends Logger { |
| 6 | + public enableBuffering() { |
| 7 | + this.isBufferEnabled = true; |
| 8 | + } |
| 9 | + public disableBuffering() { |
| 10 | + this.isBufferEnabled = false; |
| 11 | + } |
| 12 | + |
| 13 | + public flushBufferWrapper(): void { |
| 14 | + this.flushBuffer(); |
| 15 | + } |
| 16 | + |
| 17 | + public overrideBufferLogItem(): void { |
| 18 | + this.bufferLogItem = vi.fn().mockImplementation(() => { |
| 19 | + throw new Error('bufferLogItem error'); |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + public setbufferLevelThreshold(level: number): void { |
| 24 | + this.bufferLogThreshold = level; |
| 25 | + } |
| 26 | +} |
3 | 27 |
|
4 |
| -describe('SizedItem', () => { |
5 |
| - it('calculates the byteSize based on string value', () => { |
| 28 | +describe('bufferLog', () => { |
| 29 | + it('outputs a warning when there is an error buffering the log', () => { |
6 | 30 | // Prepare
|
7 |
| - const logEntry = 'hello world'; |
| 31 | + process.env.POWERTOOLS_DEV = 'true'; |
| 32 | + const logger = new TestLogger(); |
| 33 | + logger.enableBuffering(); |
| 34 | + logger.overrideBufferLogItem(); |
8 | 35 |
|
9 | 36 | // Act
|
10 |
| - const item = new SizedItem(logEntry, 1); |
| 37 | + logger.debug('This is a debug'); |
11 | 38 |
|
12 | 39 | // Assess
|
13 |
| - const expectedByteSize = Buffer.byteLength(logEntry); |
14 |
| - expect(item.byteSize).toBe(expectedByteSize); |
15 |
| - }); |
16 |
| - |
17 |
| - it('throws an error if value is not a string', () => { |
18 |
| - // Prepare |
19 |
| - const invalidValue = { message: 'not a string' }; |
20 |
| - |
21 |
| - // Act & Assess |
22 |
| - expect( |
23 |
| - () => new SizedItem(invalidValue as unknown as string, 1) |
24 |
| - ).toThrowError('Value should be a string'); |
| 40 | + expect(console.debug).toBeCalledTimes(1); |
| 41 | + expect(console.warn).toBeCalledTimes(1); |
25 | 42 | });
|
26 | 43 | });
|
27 | 44 |
|
28 |
| -describe('SizedSet', () => { |
29 |
| - it('adds an item and updates currentBytesSize correctly', () => { |
30 |
| - // Prepare |
31 |
| - const set = new SizedSet<string>(); |
32 |
| - const item = new SizedItem('value', 1); |
33 |
| - |
34 |
| - // Act |
35 |
| - set.add(item); |
| 45 | +describe('flushBuffer', () => { |
| 46 | + const ENVIRONMENT_VARIABLES = process.env; |
36 | 47 |
|
37 |
| - // Assess |
38 |
| - expect(set.currentBytesSize).toBe(item.byteSize); |
39 |
| - expect(set.has(item)).toBe(true); |
| 48 | + beforeEach(() => { |
| 49 | + process.env = { |
| 50 | + ...ENVIRONMENT_VARIABLES, |
| 51 | + POWERTOOLS_LOGGER_LOG_EVENT: 'true', |
| 52 | + POWERTOOLS_DEV: 'true', |
| 53 | + }; |
| 54 | + vi.clearAllMocks(); |
40 | 55 | });
|
41 | 56 |
|
42 |
| - it('deletes an item and updates currentBytesSize correctly', () => { |
| 57 | + it('outputs buffered logs', () => { |
43 | 58 | // Prepare
|
44 |
| - const set = new SizedSet<string>(); |
45 |
| - const item = new SizedItem('value', 1); |
46 |
| - set.add(item); |
47 |
| - const initialSize = set.currentBytesSize; |
| 59 | + const logger = new TestLogger({ logLevel: 'SILENT' }); |
| 60 | + logger.enableBuffering(); |
| 61 | + logger.setbufferLevelThreshold(LogLevelThreshold.CRITICAL); |
48 | 62 |
|
49 | 63 | // Act
|
50 |
| - const result = set.delete(item); |
| 64 | + logger.debug('This is a debug'); |
| 65 | + logger.warn('This is a warning'); |
| 66 | + logger.critical('this is a critical'); |
51 | 67 |
|
52 | 68 | // Assess
|
53 |
| - expect(result).toBe(true); |
54 |
| - expect(set.currentBytesSize).toBe(initialSize - item.byteSize); |
55 |
| - expect(set.has(item)).toBe(false); |
56 |
| - }); |
57 |
| - |
58 |
| - it('clears all items and resets currentBytesSize to 0', () => { |
59 |
| - // Prepare |
60 |
| - const set = new SizedSet<string>(); |
61 |
| - set.add(new SizedItem('b', 1)); |
62 |
| - set.add(new SizedItem('d', 1)); |
| 69 | + expect(console.warn).toHaveBeenCalledTimes(0); |
| 70 | + expect(console.error).toHaveBeenCalledTimes(0); |
63 | 71 |
|
64 | 72 | // Act
|
65 |
| - set.clear(); |
| 73 | + logger.flushBufferWrapper(); |
66 | 74 |
|
67 | 75 | // Assess
|
68 |
| - expect(set.currentBytesSize).toBe(0); |
69 |
| - expect(set.size).toBe(0); |
| 76 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 77 | + expect(console.error).toHaveBeenCalledTimes(1); |
70 | 78 | });
|
71 | 79 |
|
72 |
| - it('removes the first inserted item with shift', () => { |
| 80 | + it('handles an empty buffer', () => { |
73 | 81 | // Prepare
|
74 |
| - const set = new SizedSet<string>(); |
75 |
| - const item1 = new SizedItem('first', 1); |
76 |
| - const item2 = new SizedItem('second', 1); |
77 |
| - set.add(item1); |
78 |
| - set.add(item2); |
| 82 | + const logger = new TestLogger(); |
| 83 | + logger.enableBuffering(); |
79 | 84 |
|
80 | 85 | // Act
|
81 |
| - const shiftedItem = set.shift(); |
82 |
| - |
83 |
| - // Assess |
84 |
| - expect(shiftedItem).toEqual(item1); |
85 |
| - expect(set.has(item1)).toBe(false); |
86 |
| - expect(set.currentBytesSize).toBe(item2.byteSize); |
| 86 | + logger.flushBufferWrapper(); |
87 | 87 | });
|
88 |
| -}); |
89 | 88 |
|
90 |
| -describe('CircularMap', () => { |
91 |
| - it('adds items to a new buffer for a given key', () => { |
| 89 | + it('does not output buffered logs when trace id is not set', () => { |
92 | 90 | // Prepare
|
93 |
| - const maxBytes = 200; |
94 |
| - const circularMap = new CircularMap<string>({ |
95 |
| - maxBytesSize: maxBytes, |
96 |
| - }); |
| 91 | + process.env._X_AMZN_TRACE_ID = undefined; |
| 92 | + const logger = new TestLogger({}); |
| 93 | + logger.enableBuffering(); |
97 | 94 |
|
98 | 95 | // Act
|
99 |
| - circularMap.setItem('trace-1', 'first log', 1); |
| 96 | + logger.debug('This is a debug'); |
| 97 | + logger.warn('this is a warning'); |
100 | 98 |
|
101 | 99 | // Assess
|
102 |
| - const buffer = circularMap.get('trace-1'); |
103 |
| - expect(buffer).toBeDefined(); |
104 |
| - if (buffer) { |
105 |
| - expect(buffer.currentBytesSize).toBeGreaterThan(0); |
106 |
| - expect(buffer.size).toBe(1); |
107 |
| - } |
108 |
| - }); |
109 |
| - |
110 |
| - it('throws an error when an item exceeds maxBytesSize', () => { |
111 |
| - // Prepare |
112 |
| - const maxBytes = 10; |
113 |
| - const circularMap = new CircularMap<string>({ |
114 |
| - maxBytesSize: maxBytes, |
115 |
| - }); |
116 |
| - |
117 |
| - // Act & Assess |
118 |
| - expect(() => { |
119 |
| - circularMap.setItem('trace-1', 'a very long message', 1); |
120 |
| - }).toThrowError('Item too big'); |
121 |
| - }); |
122 |
| - |
123 |
| - it('evicts items when the buffer overflows and call the overflow callback', () => { |
124 |
| - // Prepare |
125 |
| - const options = { |
126 |
| - maxBytesSize: 15, |
127 |
| - onBufferOverflow: vi.fn(), |
128 |
| - }; |
129 |
| - const circularMap = new CircularMap<string>(options); |
130 |
| - const smallEntry = '12345'; |
131 |
| - |
132 |
| - const entryByteSize = Buffer.byteLength(smallEntry); |
133 |
| - const entriesCount = Math.ceil(options.maxBytesSize / entryByteSize); |
| 100 | + expect(console.debug).toHaveBeenCalledTimes(0); |
| 101 | + expect(console.warn).toHaveBeenCalledTimes(1); |
134 | 102 |
|
135 | 103 | // Act
|
136 |
| - for (let i = 0; i < entriesCount; i++) { |
137 |
| - circularMap.setItem('trace-1', smallEntry, 1); |
138 |
| - } |
| 104 | + logger.flushBufferWrapper(); |
139 | 105 |
|
140 | 106 | // Assess
|
141 |
| - expect(options.onBufferOverflow).toHaveBeenCalledTimes(1); |
142 |
| - expect(circularMap.get('trace-1')?.currentBytesSize).toBeLessThan( |
143 |
| - options.maxBytesSize |
144 |
| - ); |
| 107 | + expect(console.debug).toHaveBeenCalledTimes(0); |
| 108 | + expect(console.warn).toHaveBeenCalledTimes(1); |
145 | 109 | });
|
146 | 110 | });
|
0 commit comments