-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtest-failover.js
More file actions
207 lines (171 loc) · 7.02 KB
/
test-failover.js
File metadata and controls
207 lines (171 loc) · 7.02 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
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
const DatabaseFailoverManager = require('./database-failover');
// Mock environment variables for testing
process.env.DB_PRIMARY_HOST = 'localhost';
process.env.DB_PRIMARY_PORT = '5432';
process.env.DB_PRIMARY_NAME = 'test_vesting';
process.env.DB_PRIMARY_USER = 'postgres';
process.env.DB_PRIMARY_PASSWORD = 'test';
process.env.DB_SECONDARY_HOST = 'localhost';
process.env.DB_SECONDARY_PORT = '3306';
process.env.DB_SECONDARY_NAME = 'test_vesting_backup';
process.env.DB_SECONDARY_USER = 'root';
process.env.DB_SECONDARY_PASSWORD = 'test';
process.env.DB_SECONDARY_TYPE = 'mysql';
class FailoverTest {
constructor() {
this.testResults = [];
}
log(test, result, message = '') {
this.testResults.push({
test,
result: result ? 'PASS' : 'FAIL',
message,
timestamp: new Date().toISOString()
});
console.log(`${result ? '✅' : '❌'} ${test}: ${message}`);
}
async testInitialization() {
try {
const manager = new DatabaseFailoverManager();
const status = manager.getStatus();
this.log(
'Database Failover Initialization',
status && status.currentDB === 'primary',
`Current DB: ${status?.currentDB}, Read-only: ${status?.isReadOnly}`
);
await manager.close();
} catch (error) {
this.log('Database Failover Initialization', false, error.message);
}
}
async testHeartbeatMechanism() {
try {
const manager = new DatabaseFailoverManager();
// Wait a few seconds to let heartbeat run
await new Promise(resolve => setTimeout(resolve, 15000));
const status = manager.getStatus();
this.log(
'Heartbeat Mechanism',
status && status.lastHeartbeat > 0,
`Last heartbeat: ${new Date(status.lastHeartbeat).toISOString()}`
);
await manager.close();
} catch (error) {
this.log('Heartbeat Mechanism', false, error.message);
}
}
async testReadOnlyMode() {
try {
const manager = new DatabaseFailoverManager();
// Simulate failover by forcing read-only mode
manager.currentDB = 'secondary';
manager.isReadOnly = true;
try {
await manager.writeQuery('SELECT 1');
this.log('Read-Only Mode', false, 'Write query should have failed in read-only mode');
} catch (error) {
this.log(
'Read-Only Mode',
error.message.includes('read-only mode'),
'Correctly blocked write operations in read-only mode'
);
}
// Test read operations still work
try {
await manager.query('SELECT 1');
this.log('Read Operations in Read-Only', true, 'Read operations work in read-only mode');
} catch (error) {
this.log('Read Operations in Read-Only', false, error.message);
}
await manager.close();
} catch (error) {
this.log('Read-Only Mode', false, error.message);
}
}
async testStatusEndpoint() {
try {
const response = await fetch('http://localhost:3000/api/health');
const health = await response.json();
this.log(
'Health Check Endpoint',
health.status && health.database,
`Status: ${health.status}, DB: ${health.database.currentDB}`
);
} catch (error) {
this.log('Health Check Endpoint', false, error.message);
}
}
async testPortfolioEndpoint() {
try {
const testAddress = '0x1234567890abcdef1234567890abcdef12345678';
const response = await fetch(`http://localhost:3000/api/user/${testAddress}/portfolio`);
const portfolio = await response.json();
this.log(
'Portfolio Endpoint',
portfolio.total_locked !== undefined && portfolio.total_claimable !== undefined,
`Locked: ${portfolio.total_locked}, Claimable: ${portfolio.total_claimable}`
);
} catch (error) {
this.log('Portfolio Endpoint', false, error.message);
}
}
async testFailoverTimeout() {
try {
const manager = new DatabaseFailoverManager();
// Simulate primary database being down for more than 30 seconds
manager.lastHeartbeat = Date.now() - 35000; // 35 seconds ago
// Trigger health check
await manager.checkPrimaryHealth();
const status = manager.getStatus();
this.log(
'Failover Timeout',
status.currentDB === 'secondary' && status.isReadOnly,
`Failed over to secondary: ${status.currentDB}, Read-only: ${status.isReadOnly}`
);
await manager.close();
} catch (error) {
this.log('Failover Timeout', false, error.message);
}
}
async runAllTests() {
console.log('🧪 Starting Multi-Cloud Database Failover Tests\n');
await this.testInitialization();
await this.testHeartbeatMechanism();
await this.testReadOnlyMode();
await this.testFailoverTimeout();
// Server-dependent tests (only if server is running)
console.log('\n📡 Testing API Endpoints (server must be running on port 3000)...');
try {
await this.testStatusEndpoint();
await this.testPortfolioEndpoint();
} catch (error) {
console.log('⚠️ API endpoint tests skipped - server not running');
}
this.printResults();
}
printResults() {
console.log('\n📊 Test Results:');
console.log('=' .repeat(50));
const passed = this.testResults.filter(r => r.result === 'PASS').length;
const total = this.testResults.length;
this.testResults.forEach(result => {
console.log(`${result.result} ${result.test}`);
if (result.message) {
console.log(` ${result.message}`);
}
});
console.log('=' .repeat(50));
console.log(`Summary: ${passed}/${total} tests passed`);
if (passed === total) {
console.log('🎉 All tests passed! Multi-cloud failover system is working correctly.');
} else {
console.log('⚠️ Some tests failed. Please check the configuration and setup.');
}
}
}
// Run tests if this file is executed directly
if (require.main === module) {
const tester = new FailoverTest();
tester.runAllTests().catch(console.error);
}
module.exports = FailoverTest;