-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-refresh-functionality.js
More file actions
97 lines (79 loc) · 3.41 KB
/
Copy pathtest-refresh-functionality.js
File metadata and controls
97 lines (79 loc) · 3.41 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
const ProxyPool = require('./proxy-pool');
/**
* 测试 ProxyPool 的 refresh 功能
* 使用模拟的 scraper 来避免网络依赖
*/
// 创建模拟的 ProxyScraper
class MockProxyScraper {
constructor() {
this.mockProxies = [
{ ip: '192.168.1.10', port: 8080, protocol: 'http', country: 'US', anonymity: 'elite', source: 'mock' },
{ ip: '192.168.1.11', port: 3128, protocol: 'https', country: 'UK', anonymity: 'anonymous', source: 'mock' },
{ ip: '192.168.1.12', port: 8888, protocol: 'http', country: 'CN', anonymity: 'transparent', source: 'mock' }
];
this.validProxies = [
{ ip: '192.168.1.10', port: 8080, protocol: 'http', country: 'US', anonymity: 'elite', source: 'mock' },
{ ip: '192.168.1.11', port: 3128, protocol: 'https', country: 'UK', anonymity: 'anonymous', source: 'mock' }
];
}
async scrapeFromFreeProxyList() {
console.log('模拟抓取代理...');
return [...this.mockProxies];
}
async validateProxies(proxies) {
console.log(`模拟验证 ${proxies.length} 个代理...`);
// 模拟验证过程,返回部分有效代理
return [...this.validProxies];
}
}
async function testRefreshFunctionality() {
console.log('🧪 测试 ProxyPool refresh 功能\n');
// 创建 ProxyPool 实例
const pool = new ProxyPool();
// 替换为模拟的 scraper
pool.scraper = new MockProxyScraper();
console.log('📊 初始状态:');
console.log(` 代理池大小: ${pool.proxies.length}`);
// 手动添加一些旧代理
console.log('\n➕ 添加一些旧代理...');
pool.addProxy({ ip: '10.0.0.1', port: 8080, protocol: 'http', country: 'OLD', anonymity: 'old', source: 'old' });
pool.addProxy({ ip: '10.0.0.2', port: 3128, protocol: 'http', country: 'OLD', anonymity: 'old', source: 'old' });
console.log(` 添加旧代理后池大小: ${pool.proxies.length}`);
// 执行刷新
console.log('\n🔄 执行代理池刷新...');
const addedCount = await pool.refresh();
console.log('\n📊 刷新后状态:');
console.log(` 新添加的代理数量: ${addedCount}`);
console.log(` 代理池总大小: ${pool.proxies.length}`);
// 验证旧代理被替换
console.log('\n🔍 验证代理池内容:');
const hasOldProxy = pool.proxies.some(p => p.source === 'old');
const hasMockProxy = pool.proxies.some(p => p.source === 'mock');
console.log(` 包含旧代理: ${hasOldProxy ? '是 (错误)' : '否 (正确)'}`);
console.log(` 包含新代理: ${hasMockProxy ? '是 (正确)' : '否 (错误)'}`);
// 显示当前代理池内容
console.log('\n📋 当前代理池内容:');
pool.proxies.forEach((proxy, index) => {
console.log(` ${index + 1}. ${proxy.ip}:${proxy.port} (${proxy.source})`);
});
// 测试获取代理
console.log('\n🎯 测试获取代理:');
const proxy = pool.getProxy();
if (proxy) {
console.log(` 获取到代理: ${proxy.ip}:${proxy.port} (${proxy.source})`);
} else {
console.log(' 未获取到代理');
}
// 验证结果
if (!hasOldProxy && hasMockProxy && addedCount === 2 && pool.proxies.length === 2) {
console.log('\n✅ refresh 功能测试通过 - 旧代理被成功替换为新代理');
} else {
console.log('\n❌ refresh 功能测试失败');
process.exit(1);
}
}
// 运行测试
testRefreshFunctionality().catch(error => {
console.error('❌ refresh 功能测试失败:', error.message);
process.exit(1);
});