-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHttpAgentSingleton.test.ts
More file actions
102 lines (81 loc) · 3.38 KB
/
HttpAgentSingleton.test.ts
File metadata and controls
102 lines (81 loc) · 3.38 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
import { HttpAgentSingleton } from './HttpAgentSingleton'
// Mock the createHttpAgent function (external dependency)
jest.mock('../../../HttpClient/agents', () => ({
createHttpAgent: jest.fn(() => ({
sockets: {},
freeSockets: {},
requests: {},
})),
}))
describe('HttpAgentSingleton', () => {
let recordedGaugeCalls: Map<string, { value: number; attributes?: any }[]>
beforeEach(() => {
// Reset call tracking
recordedGaugeCalls = new Map()
// Create a minimal stub that tracks gauge calls (not mocking DiagnosticsMetrics class)
global.diagnosticsMetrics = {
setGauge: (name: string, value: number, attributes?: any) => {
if (!recordedGaugeCalls.has(name)) {
recordedGaugeCalls.set(name, [])
}
recordedGaugeCalls.get(name)!.push({ value, attributes })
},
} as any
// Reset the agent's internal state
const agent = HttpAgentSingleton.getHttpAgent()
;(agent as any).sockets = {}
;(agent as any).freeSockets = {}
;(agent as any).requests = {}
})
afterEach(() => {
// Clean up global
delete (global as any).diagnosticsMetrics
})
describe('httpAgentStats', () => {
it('should return current HTTP agent statistics', () => {
const agent = HttpAgentSingleton.getHttpAgent()
// Mock some socket data
;(agent as any).sockets = { 'host1:80': [1, 2], 'host2:443': [1] }
;(agent as any).freeSockets = { 'host1:80': [1] }
;(agent as any).requests = { 'host1:80': [1, 2, 3] }
const stats = HttpAgentSingleton.httpAgentStats()
expect(stats).toEqual({
sockets: 3,
freeSockets: 1,
pendingRequests: 3,
})
})
it('should return zero counts for empty agent', () => {
const stats = HttpAgentSingleton.httpAgentStats()
expect(stats).toEqual({
sockets: 0,
freeSockets: 0,
pendingRequests: 0,
})
})
})
describe('updateHttpAgentMetrics', () => {
it('should report HTTP agent stats as gauges to diagnostics metrics', () => {
const agent = HttpAgentSingleton.getHttpAgent()
// Mock some socket data
;(agent as any).sockets = { 'host1:80': [1, 2] }
;(agent as any).freeSockets = { 'host1:80': [1] }
;(agent as any).requests = { 'host1:80': [1, 2, 3] }
HttpAgentSingleton.updateHttpAgentMetrics()
expect(recordedGaugeCalls.get('http_agent_sockets_current')).toEqual([{ value: 2, attributes: {} }])
expect(recordedGaugeCalls.get('http_agent_free_sockets_current')).toEqual([{ value: 1, attributes: {} }])
expect(recordedGaugeCalls.get('http_agent_pending_requests_current')).toEqual([{ value: 3, attributes: {} }])
})
it('should handle missing global.diagnosticsMetrics gracefully', () => {
delete (global as any).diagnosticsMetrics
// Should not throw
expect(() => HttpAgentSingleton.updateHttpAgentMetrics()).not.toThrow()
})
it('should report zero values when agent has no active connections', () => {
HttpAgentSingleton.updateHttpAgentMetrics()
expect(recordedGaugeCalls.get('http_agent_sockets_current')).toEqual([{ value: 0, attributes: {} }])
expect(recordedGaugeCalls.get('http_agent_free_sockets_current')).toEqual([{ value: 0, attributes: {} }])
expect(recordedGaugeCalls.get('http_agent_pending_requests_current')).toEqual([{ value: 0, attributes: {} }])
})
})
})