-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest-balancer.ts
More file actions
103 lines (97 loc) · 2.89 KB
/
Copy pathrequest-balancer.ts
File metadata and controls
103 lines (97 loc) · 2.89 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
import fetch from '@root/index'
import { startServer, stopServer, handleError } from './utils'
/** HTTPBin.org base URL for testing load balancing */
const httpbinUrl: string = 'https://httpbin.org'
/** Local base URL for testing load balancing */
const localUrl: string = 'http://localhost:3000'
/**
* Tests basic load balancing functionality.
* @description Demonstrates load balancing across multiple endpoints using the fastest strategy.
* @returns Promise that resolves when test completes
*/
async function balancerBasic(): Promise<void> {
try {
console.log(
'-> Load Balanced Fastest',
await fetch.get('/anything', {
balancer: {
endpoints: [httpbinUrl, localUrl],
strategy: 'fastest'
}
})
)
} catch (error: unknown) {
handleError(error, 'Load Balanced Fastest')
}
}
/**
* Tests parallel load balancing functionality.
* @description Demonstrates parallel requests to multiple endpoints using the parallel strategy.
* @returns Promise that resolves when test completes
*/
async function balancerParallel(): Promise<void> {
try {
console.log(
'-> Load Balanced Parallel',
await fetch.get('/users/1', {
balancer: {
endpoints: [httpbinUrl, localUrl],
strategy: 'parallel'
}
})
)
} catch (error: unknown) {
handleError(error, 'Load Balanced Parallel')
}
}
/**
* Tests load balancing failover functionality.
* @description Demonstrates failover behavior when some endpoints fail using random/invalid URLs.
* @returns Promise that resolves when test completes
*/
async function balancerFailover(): Promise<void> {
try {
console.log(
'-> Load Balanced Failover',
await fetch.get('/anything', {
balancer: {
endpoints: [
'https://invalid-random-url-that-does-not-exist.com',
'https://another-fake-endpoint.net',
localUrl,
httpbinUrl
],
strategy: 'fastest'
}
})
)
} catch (error: unknown) {
handleError(error, 'Load Balanced Failover')
}
}
/**
* Runs all load balancing test functions in sequence.
* @description Executes all test functions with progress logging and error handling.
* @returns Promise that resolves when all tests complete
*/
async function runAllTests(): Promise<void> {
const listTests: Array<() => Promise<void>> = [
startServer,
balancerBasic,
balancerParallel,
balancerFailover,
stopServer
]
for (const [index, testTask] of listTests.entries()) {
console.log(`[${index + 1}] Working on "${testTask.name}" Function...`)
await testTask()
console.log('-\n-')
}
}
/**
* Main function to execute all tests when run directly.
* @description Checks if the script is being run directly and executes all tests.
*/
if (import.meta.url === `file://${process.argv[1]}`) {
runAllTests().catch(console.error)
}