mm_redis 1.9.0 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +14 -20
- package/package.json +1 -1
- package/test.js +1 -2
- package/compatibility_table.md +0 -63
- package/test_all_methods.js +0 -317
- package/test_crud_events.js +0 -441
- package/test_real_redis.js +0 -104
package/index.js
CHANGED
|
@@ -17,24 +17,13 @@ class Redis extends eventEmitter {
|
|
|
17
17
|
/**
|
|
18
18
|
* @description 创建Redis帮助类函数 (构造函数)
|
|
19
19
|
* @param {String} scope 作用域
|
|
20
|
-
* @param {
|
|
20
|
+
* @param {Object} config 配置参数
|
|
21
21
|
* @constructor
|
|
22
22
|
*/
|
|
23
|
-
constructor(scope,
|
|
23
|
+
constructor(scope, config) {
|
|
24
24
|
super();
|
|
25
25
|
// 作用域
|
|
26
|
-
this.scope;
|
|
27
|
-
if (scope) {
|
|
28
|
-
this.scope = scope;
|
|
29
|
-
} else {
|
|
30
|
-
this.scope = $.val.scope + '';
|
|
31
|
-
}
|
|
32
|
-
// 当前目录
|
|
33
|
-
this.dir = __dirname;
|
|
34
|
-
if (dir) {
|
|
35
|
-
this.dir = dir;
|
|
36
|
-
}
|
|
37
|
-
|
|
26
|
+
this.scope = scope;
|
|
38
27
|
// 配置参数
|
|
39
28
|
this.config = {
|
|
40
29
|
// 服务器地址
|
|
@@ -42,7 +31,7 @@ class Redis extends eventEmitter {
|
|
|
42
31
|
// 端口号
|
|
43
32
|
"port": 6379,
|
|
44
33
|
// 密码
|
|
45
|
-
"password": "
|
|
34
|
+
"password": "",
|
|
46
35
|
// 数据库
|
|
47
36
|
"database": 0,
|
|
48
37
|
// 前缀
|
|
@@ -54,6 +43,7 @@ class Redis extends eventEmitter {
|
|
|
54
43
|
// 重试间隔(ms)
|
|
55
44
|
"retry_delay": 1000
|
|
56
45
|
};
|
|
46
|
+
this.setConfig(config);
|
|
57
47
|
// 连接池配置
|
|
58
48
|
this.connectionPool = {
|
|
59
49
|
available: [], // 可用连接数组
|
|
@@ -183,8 +173,12 @@ Redis.prototype.setConfig = function (cg) {
|
|
|
183
173
|
let obj = {};
|
|
184
174
|
if (typeof cg === 'string') {
|
|
185
175
|
obj = cg.loadJson(__dirname);
|
|
186
|
-
} else {
|
|
187
|
-
|
|
176
|
+
} else if (cg) {
|
|
177
|
+
// 合并配置参数
|
|
178
|
+
Object.assign(this.config, cg);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
return;
|
|
188
182
|
}
|
|
189
183
|
// 使用Object.assign替代$.push来合并配置
|
|
190
184
|
this.config = Object.assign({}, this.config || {}, obj);
|
|
@@ -2926,16 +2920,16 @@ if (!$.pool.redis) {
|
|
|
2926
2920
|
/**
|
|
2927
2921
|
* @description Redis管理器,用于创建缓存
|
|
2928
2922
|
* @param {String} scope 作用域
|
|
2929
|
-
* @param {
|
|
2923
|
+
* @param {Object} config 配置参数
|
|
2930
2924
|
* @return {Object} 返回一个Redis类实例
|
|
2931
2925
|
*/
|
|
2932
|
-
function redis_admin(scope,
|
|
2926
|
+
function redis_admin(scope, config) {
|
|
2933
2927
|
if (!scope) {
|
|
2934
2928
|
scope = 'sys';
|
|
2935
2929
|
}
|
|
2936
2930
|
var obj = $.pool.redis[scope];
|
|
2937
2931
|
if (!obj) {
|
|
2938
|
-
$.pool.redis[scope] = new Redis(scope,
|
|
2932
|
+
$.pool.redis[scope] = new Redis(scope, config);
|
|
2939
2933
|
obj = $.pool.redis[scope];
|
|
2940
2934
|
}
|
|
2941
2935
|
return obj;
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -3,9 +3,8 @@ const { redis_admin } = require('./index.js');
|
|
|
3
3
|
async function test() {
|
|
4
4
|
try {
|
|
5
5
|
var bl, num, arr = [];
|
|
6
|
-
var rs = redis_admin('sys');
|
|
7
6
|
var config = './config.json'.loadJson(__dirname);
|
|
8
|
-
rs
|
|
7
|
+
var rs = redis_admin('sys', config);
|
|
9
8
|
await rs.open();
|
|
10
9
|
|
|
11
10
|
// 测试基本的set和get操作
|
package/compatibility_table.md
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
```markdown
|
|
2
|
-
# 三种缓存方式方法兼容性对比表格
|
|
3
|
-
|
|
4
|
-
## 核心缓存方法
|
|
5
|
-
|
|
6
|
-
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
7
|
-
|-------|-------|---------|-----------|----------|
|
|
8
|
-
| set | ✅ | ✅ | ✅ | 完全兼容 |
|
|
9
|
-
| get | ✅ | ✅ | ✅ | 完全兼容 |
|
|
10
|
-
| del | ✅ | ✅ | ✅ | 完全兼容 |
|
|
11
|
-
| has | ✅ | ✅ | ✅ | 完全兼容 |
|
|
12
|
-
| clear | ✅ | ✅ | ✅ | 完全兼容 |
|
|
13
|
-
| keys | ✅ | ✅ | ✅ | 完全兼容 |
|
|
14
|
-
| ttl | ✅ | ✅ | ✅ | 完全兼容 |
|
|
15
|
-
| add | ✅ | ✅ | ✅ | 完全兼容 |
|
|
16
|
-
| addInt | ✅ | ✅ | ✅ | 完全兼容 |
|
|
17
|
-
| addStr | ✅ | ✅ | ✅ | 完全兼容 |
|
|
18
|
-
| sort | ✅ | ✅ | ✅ | 模拟实现 |
|
|
19
|
-
|
|
20
|
-
## Redis兼容方法 - 哈希表操作
|
|
21
|
-
|
|
22
|
-
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
23
|
-
|-------|-------|---------|-----------|----------|
|
|
24
|
-
| hset | ✅ | ✅ | ✅ | 模拟实现,通过set对象方式 |
|
|
25
|
-
| hget | ✅ | ✅ | ✅ | 模拟实现,通过get对象方式 |
|
|
26
|
-
| hgetall | ✅ | ✅ | ✅ | 模拟实现,返回完整对象 |
|
|
27
|
-
| hdel | ✅ | ✅ | ✅ | 模拟实现,删除对象属性 |
|
|
28
|
-
| hexists | ✅ | ❌ | ❌ | 完全实现 |
|
|
29
|
-
| hkeys | ✅ | ❌ | ❌ | 完全实现 |
|
|
30
|
-
| hvals | ✅ | ❌ | ❌ | 完全实现 |
|
|
31
|
-
|
|
32
|
-
## Redis兼容方法 - 集合操作
|
|
33
|
-
|
|
34
|
-
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
35
|
-
|-------|-------|---------|-----------|----------|
|
|
36
|
-
| sadd | ✅ | ❌ | ❌ | 完全实现 |
|
|
37
|
-
| srem | ✅ | ❌ | ❌ | 完全实现 |
|
|
38
|
-
| sismember | ✅ | ❌ | ❌ | 完全实现 |
|
|
39
|
-
| smembers | ✅ | ❌ | ❌ | 完全实现 |
|
|
40
|
-
| scard | ✅ | ❌ | ❌ | 完全实现 |
|
|
41
|
-
|
|
42
|
-
## Redis兼容方法 - 有序集合操作
|
|
43
|
-
|
|
44
|
-
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
45
|
-
|-------|-------|---------|-----------|----------|
|
|
46
|
-
| zadd | ✅ | ❌ | ❌ | 完全实现 |
|
|
47
|
-
| zrem | ✅ | ❌ | ❌ | 完全实现 |
|
|
48
|
-
| zrange | ✅ | ❌ | ❌ | 完全实现 |
|
|
49
|
-
| zscore | ✅ | ❌ | ❌ | 完全实现 |
|
|
50
|
-
|
|
51
|
-
## Redis兼容方法 - 其他常用命令
|
|
52
|
-
|
|
53
|
-
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
54
|
-
|-------|-------|---------|-----------|----------|
|
|
55
|
-
| expire | ✅ | ✅ | ✅ | 兼容,内部映射到ttl |
|
|
56
|
-
| incr | ✅ | ⚠️ | ✅ | Redis原生支持,MongoDB返回undefined,CacheBase通过addInt模拟 |
|
|
57
|
-
| decr | ✅ | ⚠️ | ✅ | Redis原生支持,MongoDB返回undefined,CacheBase通过addInt模拟 |
|
|
58
|
-
| exists | ✅ | ✅ | ✅ | 兼容,内部映射到has |
|
|
59
|
-
| mget | ✅ | ⚠️ | ✅ | 已修复为返回数组格式,与原生Redis一致 |
|
|
60
|
-
| mset | ✅ | ✅ | ✅ | 已修复参数格式处理,添加了格式验证 |
|
|
61
|
-
|
|
62
|
-
注:✅ 完全兼容/功能正常 | ⚠️ 部分兼容/有异常 | ❌ 不兼容/未实现
|
|
63
|
-
```
|
package/test_all_methods.js
DELETED
|
@@ -1,317 +0,0 @@
|
|
|
1
|
-
// 全面测试Redis模块的所有方法和事件
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
// 简单的日志函数
|
|
6
|
-
function log(message, data = null) {
|
|
7
|
-
const timestamp = new Date().toISOString();
|
|
8
|
-
if (data) {
|
|
9
|
-
console.log(`[${timestamp}] ${message}`, typeof data === 'object' ? JSON.stringify(data, null, 2) : data);
|
|
10
|
-
} else {
|
|
11
|
-
console.log(`[${timestamp}] ${message}`);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// 加载配置文件的函数
|
|
16
|
-
function loadConfig(configPath) {
|
|
17
|
-
try {
|
|
18
|
-
const configContent = fs.readFileSync(configPath, 'utf8');
|
|
19
|
-
return JSON.parse(configContent);
|
|
20
|
-
} catch (error) {
|
|
21
|
-
log(`错误:无法加载配置文件 ${configPath}: ${error.message}`);
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// 测试结果统计
|
|
27
|
-
const testResults = {
|
|
28
|
-
total: 0,
|
|
29
|
-
passed: 0,
|
|
30
|
-
failed: 0,
|
|
31
|
-
skipped: 0,
|
|
32
|
-
failedTests: [] // 记录失败的测试名称
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// 测试函数包装器
|
|
36
|
-
async function runTest(name, testFunc) {
|
|
37
|
-
testResults.total++;
|
|
38
|
-
log(`\n🔍 测试 ${name} 开始...`);
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
await testFunc();
|
|
42
|
-
log(`✅ 测试 ${name} 通过!`);
|
|
43
|
-
testResults.passed++;
|
|
44
|
-
return true;
|
|
45
|
-
} catch (error) {
|
|
46
|
-
log(`❌ 测试 ${name} 失败: ${error.message}`);
|
|
47
|
-
log(error.stack);
|
|
48
|
-
testResults.failed++;
|
|
49
|
-
testResults.failedTests.push(name); // 记录失败的测试名称
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// 主测试函数
|
|
55
|
-
async function runComprehensiveTest() {
|
|
56
|
-
log('开始全面测试Redis模块的所有方法和事件...');
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
// 加载Redis模块
|
|
60
|
-
const RedisModule = require('./index.js');
|
|
61
|
-
const Redis = RedisModule.default || RedisModule;
|
|
62
|
-
const redis_admin = RedisModule.redis_admin;
|
|
63
|
-
|
|
64
|
-
log(`Redis类类型: ${typeof Redis}`);
|
|
65
|
-
log(`redis_admin函数类型: ${typeof redis_admin}`);
|
|
66
|
-
|
|
67
|
-
// 加载配置
|
|
68
|
-
const configPath = path.join(__dirname, 'config.json');
|
|
69
|
-
const config = loadConfig(configPath);
|
|
70
|
-
|
|
71
|
-
if (!config) {
|
|
72
|
-
log('无法继续测试,配置文件加载失败');
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
log('加载配置成功:', config);
|
|
77
|
-
|
|
78
|
-
// 创建测试实例
|
|
79
|
-
const redisInstance = redis_admin('test_comprehensive');
|
|
80
|
-
redisInstance.setConfig(config);
|
|
81
|
-
|
|
82
|
-
// 测试连接
|
|
83
|
-
await runTest('Redis连接', async () => {
|
|
84
|
-
await redisInstance.open();
|
|
85
|
-
log('✅ Redis连接成功');
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
// 基础键值操作测试
|
|
89
|
-
await runTest('基础set/get操作', async () => {
|
|
90
|
-
const testKey = 'comprehensive_test_key';
|
|
91
|
-
const testValue = { name: '测试对象', value: 123, timestamp: new Date().toISOString() };
|
|
92
|
-
|
|
93
|
-
await redisInstance.set(testKey, testValue);
|
|
94
|
-
const retrievedValue = await redisInstance.get(testKey);
|
|
95
|
-
|
|
96
|
-
if (JSON.stringify(retrievedValue) !== JSON.stringify(testValue)) {
|
|
97
|
-
throw new Error('获取的值与设置的值不匹配');
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// 清理测试数据
|
|
101
|
-
await redisInstance.del([testKey]);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// 过期时间测试
|
|
105
|
-
await runTest('过期时间设置', async () => {
|
|
106
|
-
const testKey = 'comprehensive_test_ttl';
|
|
107
|
-
await redisInstance.set(testKey, '临时值', 10); // 10秒过期
|
|
108
|
-
|
|
109
|
-
const ttl = await redisInstance.ttl(testKey);
|
|
110
|
-
if (typeof ttl !== 'number' || ttl <= 0) {
|
|
111
|
-
throw new Error('过期时间设置失败: ' + ttl);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 清理测试数据
|
|
115
|
-
await redisInstance.del([testKey]);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
// 数值操作测试
|
|
119
|
-
await runTest('数值增减操作', async () => {
|
|
120
|
-
const counterKey = 'comprehensive_counter';
|
|
121
|
-
|
|
122
|
-
// 设置初始值
|
|
123
|
-
await redisInstance.set(counterKey, 10);
|
|
124
|
-
log(`设置初始值为10`);
|
|
125
|
-
|
|
126
|
-
// 增加
|
|
127
|
-
const afterAdd = await redisInstance.addInt(counterKey, 5);
|
|
128
|
-
log(`增加5后的值: ${afterAdd}`);
|
|
129
|
-
if (afterAdd !== 15) {
|
|
130
|
-
throw new Error(`数值增加失败,预期15,实际${afterAdd}`);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// 减少
|
|
134
|
-
const afterSub = await redisInstance.addInt(counterKey, -3);
|
|
135
|
-
log(`减少3后的值: ${afterSub}`);
|
|
136
|
-
if (afterSub !== 12) {
|
|
137
|
-
throw new Error(`数值减少失败,预期12,实际${afterSub}`);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// 清理测试数据
|
|
141
|
-
await redisInstance.del([counterKey]);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// 列表操作测试
|
|
145
|
-
await runTest('列表操作', async () => {
|
|
146
|
-
const listKey = 'comprehensive_list';
|
|
147
|
-
const testList = ['item1', 'item2', 'item3']; // 简化列表,避免对象序列化问题
|
|
148
|
-
|
|
149
|
-
log(`设置列表数据: ${JSON.stringify(testList)}`);
|
|
150
|
-
|
|
151
|
-
// 设置列表
|
|
152
|
-
await redisInstance.list_set(listKey, testList);
|
|
153
|
-
log('列表设置完成');
|
|
154
|
-
|
|
155
|
-
// 获取列表
|
|
156
|
-
const retrievedList = await redisInstance.list_get(listKey);
|
|
157
|
-
log(`获取到的列表: ${JSON.stringify(retrievedList)}`);
|
|
158
|
-
log(`预期长度: ${testList.length}, 实际长度: ${retrievedList.length}`);
|
|
159
|
-
|
|
160
|
-
if (!retrievedList || !Array.isArray(retrievedList)) {
|
|
161
|
-
throw new Error('获取的列表不是有效的数组');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (retrievedList.length !== testList.length) {
|
|
165
|
-
throw new Error(`列表长度不匹配,预期${testList.length},实际${retrievedList.length}`);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// 验证内容
|
|
169
|
-
for (let i = 0; i < testList.length; i++) {
|
|
170
|
-
if (retrievedList[i] !== testList[i]) {
|
|
171
|
-
throw new Error(`列表内容不匹配,索引${i}:预期"${testList[i]}",实际"${retrievedList[i]}"`);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
log('列表内容验证通过');
|
|
176
|
-
|
|
177
|
-
// 清理测试数据
|
|
178
|
-
await redisInstance.del([listKey]);
|
|
179
|
-
log('测试数据清理完成');
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
// 键前缀测试
|
|
183
|
-
await runTest('键前缀支持', async () => {
|
|
184
|
-
const originalPrefix = redisInstance.config.prefix;
|
|
185
|
-
const testKey = 'prefixed_test';
|
|
186
|
-
|
|
187
|
-
// 设置临时前缀
|
|
188
|
-
redisInstance.config.prefix = 'test_prefix:';
|
|
189
|
-
|
|
190
|
-
// 设置带前缀的键
|
|
191
|
-
await redisInstance.set(testKey, '带前缀的值');
|
|
192
|
-
|
|
193
|
-
// 验证获取
|
|
194
|
-
const value = await redisInstance.get(testKey);
|
|
195
|
-
if (value !== '带前缀的值') {
|
|
196
|
-
throw new Error('带前缀的键获取失败');
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// 恢复原始前缀
|
|
200
|
-
redisInstance.config.prefix = originalPrefix;
|
|
201
|
-
|
|
202
|
-
// 清理测试数据(使用完整键名)
|
|
203
|
-
await redisInstance.del(['test_prefix:' + testKey]);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
// 批量操作测试
|
|
207
|
-
await runTest('批量操作', async () => {
|
|
208
|
-
const batchData = {
|
|
209
|
-
'batch_key1': '值1',
|
|
210
|
-
'batch_key2': 123,
|
|
211
|
-
'batch_key3': { test: '对象' }
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
// 批量设置
|
|
215
|
-
await redisInstance.mset(batchData);
|
|
216
|
-
|
|
217
|
-
// 批量获取
|
|
218
|
-
const keys = Object.keys(batchData);
|
|
219
|
-
const results = await redisInstance.mget(keys);
|
|
220
|
-
|
|
221
|
-
if (results.length !== keys.length) {
|
|
222
|
-
throw new Error(`批量获取结果数量不匹配,预期${keys.length},实际${results.length}`);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// 清理测试数据
|
|
226
|
-
await redisInstance.del(keys);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
// 键存在检查测试
|
|
230
|
-
await runTest('键存在检查', async () => {
|
|
231
|
-
const existKey = 'comprehensive_exist_key';
|
|
232
|
-
|
|
233
|
-
// 设置键
|
|
234
|
-
await redisInstance.set(existKey, '测试值');
|
|
235
|
-
|
|
236
|
-
// 检查存在
|
|
237
|
-
const exists = await redisInstance.has(existKey);
|
|
238
|
-
if (!exists) {
|
|
239
|
-
throw new Error('已存在的键检查失败');
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// 检查不存在的键
|
|
243
|
-
const notExists = await redisInstance.has('non_existent_key_' + Date.now());
|
|
244
|
-
if (notExists) {
|
|
245
|
-
throw new Error('不存在的键检查失败');
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// 清理测试数据
|
|
249
|
-
await redisInstance.del([existKey]);
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
// 发布订阅测试(暂时跳过,因为可能存在环境兼容性问题)
|
|
253
|
-
// 我们可以将这个测试标记为跳过,专注于测试核心功能
|
|
254
|
-
log('\n🔍 测试 发布订阅功能 开始...');
|
|
255
|
-
log('⚠️ 发布订阅测试已跳过,因为可能存在环境兼容性问题');
|
|
256
|
-
testResults.skipped++;
|
|
257
|
-
|
|
258
|
-
// 如果需要,可以在这里添加注释,说明如何在合适的环境中测试发布订阅功能
|
|
259
|
-
log('📝 注意:在支持发布订阅的环境中,可以取消以下代码的注释进行测试:');
|
|
260
|
-
log('// const testChannel = "test_channel";');
|
|
261
|
-
log('// await redisInstance.publish(testChannel, "测试消息");');
|
|
262
|
-
|
|
263
|
-
// 前缀方法测试
|
|
264
|
-
await runTest('前缀方法测试', async () => {
|
|
265
|
-
const originalPrefix = redisInstance.config.prefix;
|
|
266
|
-
redisInstance.config.prefix = 'test_prefix:';
|
|
267
|
-
|
|
268
|
-
const testKey = 'test_key';
|
|
269
|
-
const prefixedKey = redisInstance.applyPrefix(testKey);
|
|
270
|
-
|
|
271
|
-
if (prefixedKey !== 'test_prefix:test_key') {
|
|
272
|
-
throw new Error(`前缀应用失败,预期'test_prefix:test_key',实际'${prefixedKey}'`);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const removedPrefixKey = redisInstance.removePrefix(prefixedKey);
|
|
276
|
-
if (removedPrefixKey !== testKey) {
|
|
277
|
-
throw new Error(`前缀移除失败,预期'test_key',实际'${removedPrefixKey}'`);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// 恢复原始前缀
|
|
281
|
-
redisInstance.config.prefix = originalPrefix;
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
// 关闭连接测试
|
|
285
|
-
await runTest('关闭连接', async () => {
|
|
286
|
-
await redisInstance.close();
|
|
287
|
-
log('✅ 连接已成功关闭');
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
// 打印测试结果统计
|
|
291
|
-
log('\n======= 测试结果统计 =======');
|
|
292
|
-
log(`总测试数: ${testResults.total}`);
|
|
293
|
-
log(`通过: ${testResults.passed}`);
|
|
294
|
-
log(`失败: ${testResults.failed}`);
|
|
295
|
-
log(`跳过: ${testResults.skipped}`);
|
|
296
|
-
|
|
297
|
-
if (testResults.failed > 0) {
|
|
298
|
-
log(`❌ 有测试失败,失败的测试: ${testResults.failedTests.join(', ')}`);
|
|
299
|
-
} else {
|
|
300
|
-
log('🎉 所有测试通过!Redis模块功能正常!');
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
} catch (error) {
|
|
304
|
-
log(`❌ 测试过程中出现严重错误: ${error.message}`);
|
|
305
|
-
log(error.stack);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
log('\n全面测试完成!');
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// 运行测试
|
|
312
|
-
runComprehensiveTest().then(() => {
|
|
313
|
-
process.exit(testResults.failed > 0 ? 1 : 0);
|
|
314
|
-
}).catch(err => {
|
|
315
|
-
log(`❌ 测试执行失败: ${err.message}`);
|
|
316
|
-
process.exit(1);
|
|
317
|
-
});
|
package/test_crud_events.js
DELETED
|
@@ -1,441 +0,0 @@
|
|
|
1
|
-
const { Redis } = require('./index');
|
|
2
|
-
|
|
3
|
-
// Redis连接配置
|
|
4
|
-
const REDIS_CONFIG = {
|
|
5
|
-
host: 'localhost', // Redis主机地址
|
|
6
|
-
port: 6379, // Redis端口
|
|
7
|
-
password: 'asd159357', // Redis密码,如果没有密码则为空字符串
|
|
8
|
-
database: 0, // Redis数据库索引
|
|
9
|
-
prefix: 'test_mm_redis_'
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// 测试结果统计
|
|
13
|
-
const testResults = {
|
|
14
|
-
total: 0,
|
|
15
|
-
passed: 0,
|
|
16
|
-
failed: 0,
|
|
17
|
-
skipped: 0,
|
|
18
|
-
failedTests: []
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// 测试用的键名前缀,避免冲突
|
|
22
|
-
const TEST_PREFIX = 'test_mm_redis_';
|
|
23
|
-
|
|
24
|
-
// 创建Redis实例的辅助函数,使用统一配置
|
|
25
|
-
function createRedisInstance() {
|
|
26
|
-
// 尝试连接并测试,如果需要认证错误,则尝试不带密码的连接
|
|
27
|
-
const redis = new Redis('test');
|
|
28
|
-
// 使用setConfig方法正确设置配置
|
|
29
|
-
redis.setConfig(REDIS_CONFIG);
|
|
30
|
-
return redis;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 生成测试键名
|
|
34
|
-
function getTestKey(name) {
|
|
35
|
-
return `${TEST_PREFIX}${name}`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// 清理测试数据
|
|
39
|
-
async function cleanup() {
|
|
40
|
-
console.log('开始清理测试数据...');
|
|
41
|
-
const redis = new Redis('test');
|
|
42
|
-
try {
|
|
43
|
-
// 获取所有测试键并删除
|
|
44
|
-
const keys = await redis.keys(`${TEST_PREFIX}*`);
|
|
45
|
-
if (keys.length > 0) {
|
|
46
|
-
console.log(`找到 ${keys.length} 个测试键,准备删除...`);
|
|
47
|
-
for (const key of keys) {
|
|
48
|
-
await redis.del(key);
|
|
49
|
-
}
|
|
50
|
-
console.log('测试数据清理完成');
|
|
51
|
-
} else {
|
|
52
|
-
console.log('没有找到测试数据需要清理');
|
|
53
|
-
}
|
|
54
|
-
} catch (error) {
|
|
55
|
-
console.error('清理测试数据时出错:', error);
|
|
56
|
-
} finally {
|
|
57
|
-
await redis.close();
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 运行单个测试
|
|
62
|
-
async function runTest(name, testFn) {
|
|
63
|
-
testResults.total++;
|
|
64
|
-
console.log(`\n正在运行测试: ${name}...`);
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
await testFn();
|
|
68
|
-
console.log(`✅ 测试通过: ${name}`);
|
|
69
|
-
testResults.passed++;
|
|
70
|
-
return true;
|
|
71
|
-
} catch (error) {
|
|
72
|
-
console.error(`❌ 测试失败: ${name}`, error);
|
|
73
|
-
testResults.failed++;
|
|
74
|
-
testResults.failedTests.push(name);
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// 测试字符串操作方法
|
|
80
|
-
async function testStringOperations() {
|
|
81
|
-
const redis = createRedisInstance();
|
|
82
|
-
const key = getTestKey('string_op');
|
|
83
|
-
|
|
84
|
-
// 测试set和get方法
|
|
85
|
-
await redis.set(key, 'hello world');
|
|
86
|
-
const value = await redis.get(key);
|
|
87
|
-
if (value !== 'hello world') {
|
|
88
|
-
throw new Error(`set/get 测试失败,期望值: 'hello world', 实际值: ${value}`);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// 测试addInt方法
|
|
92
|
-
await redis.set(key, '10');
|
|
93
|
-
const incrValue = await redis.addInt(key, 5);
|
|
94
|
-
if (incrValue !== 15) {
|
|
95
|
-
throw new Error(`addInt 测试失败,期望值: 15, 实际值: ${incrValue}`);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// 测试del方法
|
|
99
|
-
const delResult = await redis.del(key);
|
|
100
|
-
if (delResult !== true) {
|
|
101
|
-
throw new Error(`del 测试失败,期望值: true, 实际值: ${delResult}`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// 测试has方法
|
|
105
|
-
const hasResult = await redis.has(key);
|
|
106
|
-
if (hasResult !== false) {
|
|
107
|
-
throw new Error(`has 测试失败,期望值: false, 实际值: ${hasResult}`);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
await redis.close();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// 测试事件处理
|
|
114
|
-
async function testEvents() {
|
|
115
|
-
const redis = createRedisInstance();
|
|
116
|
-
const key = getTestKey('event_test');
|
|
117
|
-
let beforeEventTriggered = false;
|
|
118
|
-
let afterEventTriggered = false;
|
|
119
|
-
|
|
120
|
-
// 监听before事件
|
|
121
|
-
redis.on('set_before', (data) => {
|
|
122
|
-
console.log('set_before 事件触发:', data);
|
|
123
|
-
beforeEventTriggered = true;
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// 监听after事件
|
|
127
|
-
redis.on('set_after', (data) => {
|
|
128
|
-
console.log('set_after 事件触发:', data);
|
|
129
|
-
afterEventTriggered = true;
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// 触发事件
|
|
133
|
-
await redis.set(key, 'event test value');
|
|
134
|
-
|
|
135
|
-
// 验证事件是否触发
|
|
136
|
-
if (!beforeEventTriggered) {
|
|
137
|
-
throw new Error('set_before 事件未触发');
|
|
138
|
-
}
|
|
139
|
-
if (!afterEventTriggered) {
|
|
140
|
-
throw new Error('set_after 事件未触发');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
await redis.close();
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// 测试列表操作方法
|
|
147
|
-
async function testListOperations() {
|
|
148
|
-
const redis = createRedisInstance();
|
|
149
|
-
const key = getTestKey('list_op');
|
|
150
|
-
|
|
151
|
-
// 测试list_set和list_get方法
|
|
152
|
-
const listData = ['item1', 'item2', 'item3'];
|
|
153
|
-
const setLength = await redis.list_set(key, listData);
|
|
154
|
-
if (setLength !== 3) {
|
|
155
|
-
throw new Error(`list_set 测试失败,期望值: 3, 实际值: ${setLength}`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const list = await redis.list_get(key);
|
|
159
|
-
if (JSON.stringify(list) !== JSON.stringify(listData)) {
|
|
160
|
-
throw new Error(`list_get 测试失败,期望值: ${JSON.stringify(listData)}, 实际值: ${JSON.stringify(list)}`);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// 测试list_push方法
|
|
164
|
-
const pushLength = await redis.list_push(key, 'item4');
|
|
165
|
-
if (pushLength !== 4) {
|
|
166
|
-
throw new Error(`list_push 测试失败,期望值: 4, 实际值: ${pushLength}`);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// 测试list_has方法
|
|
170
|
-
const hasResult = await redis.list_has(key, 'item2');
|
|
171
|
-
if (hasResult !== true) {
|
|
172
|
-
throw new Error(`list_has 测试失败,期望值: true, 实际值: ${hasResult}`);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// 测试list_clear方法
|
|
176
|
-
const clearResult = await redis.list_clear(key);
|
|
177
|
-
if (clearResult !== true) {
|
|
178
|
-
throw new Error(`list_clear 测试失败,期望值: true, 实际值: ${clearResult}`);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const clearedList = await redis.list_get(key);
|
|
182
|
-
if (clearedList.length !== 0) {
|
|
183
|
-
throw new Error(`list_clear 测试失败,期望值: [], 实际值: ${JSON.stringify(clearedList)}`);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
await redis.close();
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// 测试哈希操作方法
|
|
190
|
-
async function testHashOperations() {
|
|
191
|
-
const redis = createRedisInstance();
|
|
192
|
-
const key = getTestKey('hash_op');
|
|
193
|
-
|
|
194
|
-
// 测试hset和hget方法
|
|
195
|
-
const setResult1 = await redis.hset(key, 'field1', 'value1');
|
|
196
|
-
if (!setResult1) {
|
|
197
|
-
throw new Error(`hset 测试失败,设置field1返回: ${setResult1}`);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const setResult2 = await redis.hset(key, 'field2', '123');
|
|
201
|
-
if (!setResult2) {
|
|
202
|
-
throw new Error(`hset 测试失败,设置field2返回: ${setResult2}`);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const field1Value = await redis.hget(key, 'field1');
|
|
206
|
-
if (field1Value !== 'value1') {
|
|
207
|
-
throw new Error(`hget 测试失败,期望值: 'value1', 实际值: ${field1Value}`);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const field2Value = await redis.hget(key, 'field2');
|
|
211
|
-
if (field2Value !== '123') {
|
|
212
|
-
throw new Error(`hget 测试失败,期望值: '123', 实际值: ${field2Value}`);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// 测试hexists方法
|
|
216
|
-
const existsResult = await redis.hexists(key, 'field1');
|
|
217
|
-
if (!existsResult) {
|
|
218
|
-
throw new Error(`hexists 测试失败,field1应该存在`);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// 测试hdel方法
|
|
222
|
-
const delResult = await redis.hdel(key, 'field1');
|
|
223
|
-
if (!delResult) {
|
|
224
|
-
throw new Error(`hdel 测试失败,返回: ${delResult}`);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// 验证删除后不存在
|
|
228
|
-
const notExistsResult = await redis.hexists(key, 'field1');
|
|
229
|
-
if (notExistsResult) {
|
|
230
|
-
throw new Error(`hexists 测试失败,field1应该不存在`);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
await redis.close();
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// 测试批量操作方法
|
|
237
|
-
async function testBatchOperations() {
|
|
238
|
-
const redis = createRedisInstance();
|
|
239
|
-
|
|
240
|
-
// 简化批量操作测试,只测试基本功能
|
|
241
|
-
const key1 = getTestKey('batch1');
|
|
242
|
-
const key2 = getTestKey('batch2');
|
|
243
|
-
|
|
244
|
-
// 先单独设置值
|
|
245
|
-
await redis.set(key1, 'value1');
|
|
246
|
-
await redis.set(key2, 'value2');
|
|
247
|
-
|
|
248
|
-
// 测试mget方法
|
|
249
|
-
const mgetResult = await redis.mget([key1, key2]);
|
|
250
|
-
if (mgetResult[0] !== 'value1' || mgetResult[1] !== 'value2') {
|
|
251
|
-
throw new Error(`mget 测试失败,期望值: ['value1', 'value2'], 实际值: ${JSON.stringify(mgetResult)}`);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// 测试pipeline的简单操作
|
|
255
|
-
const pipeline = redis.pipeline();
|
|
256
|
-
pipeline.set(getTestKey('pipeline1'), 'pipeline_value1');
|
|
257
|
-
pipeline.get(getTestKey('pipeline1'));
|
|
258
|
-
|
|
259
|
-
const pipelineResults = await pipeline.exec();
|
|
260
|
-
if (!pipelineResults || pipelineResults[1] !== 'pipeline_value1') {
|
|
261
|
-
throw new Error(`pipeline 测试失败,期望值: 'pipeline_value1', 实际值: ${pipelineResults[1]}`);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
await redis.close();
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// 测试过期时间设置
|
|
268
|
-
async function testExpireOperations() {
|
|
269
|
-
const redis = createRedisInstance();
|
|
270
|
-
const key = getTestKey('expire_op');
|
|
271
|
-
|
|
272
|
-
// 设置键并设置过期时间
|
|
273
|
-
await redis.set(key, 'expire test', 2); // 2秒过期
|
|
274
|
-
|
|
275
|
-
// 立即检查键是否存在
|
|
276
|
-
const hasBeforeExpire = await redis.has(key);
|
|
277
|
-
if (hasBeforeExpire !== true) {
|
|
278
|
-
throw new Error(`expire 设置前测试失败,期望值: true, 实际值: ${hasBeforeExpire}`);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// 等待过期
|
|
282
|
-
console.log('等待键过期...');
|
|
283
|
-
await new Promise(resolve => setTimeout(resolve, 2500));
|
|
284
|
-
|
|
285
|
-
// 检查键是否已过期
|
|
286
|
-
const hasAfterExpire = await redis.has(key);
|
|
287
|
-
if (hasAfterExpire !== false) {
|
|
288
|
-
throw new Error(`expire 过期后测试失败,期望值: false, 实际值: ${hasAfterExpire}`);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
await redis.close();
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// 测试集合操作方法
|
|
295
|
-
async function testSetOperations() {
|
|
296
|
-
const redis = createRedisInstance();
|
|
297
|
-
const key = getTestKey('set_op');
|
|
298
|
-
|
|
299
|
-
// 测试sadd方法 - 简化,只添加一个成员
|
|
300
|
-
const addCount = await redis.sadd(key, 'member1');
|
|
301
|
-
if (addCount !== 1) {
|
|
302
|
-
throw new Error(`sadd 测试失败,期望值: 1, 实际值: ${addCount}`);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// 测试sismember方法
|
|
306
|
-
const isMember = await redis.sismember(key, 'member1');
|
|
307
|
-
if (!isMember) {
|
|
308
|
-
throw new Error(`sismember 测试失败,member1应该存在于集合中`);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// 测试scard方法
|
|
312
|
-
const cardCount = await redis.scard(key);
|
|
313
|
-
if (cardCount !== 1) {
|
|
314
|
-
throw new Error(`scard 测试失败,期望值: 1, 实际值: ${cardCount}`);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// 测试srem方法
|
|
318
|
-
const remCount = await redis.srem(key, 'member1');
|
|
319
|
-
if (remCount !== 1) {
|
|
320
|
-
throw new Error(`srem 测试失败,期望值: 1, 实际值: ${remCount}`);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
await redis.close();
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// 测试有序集合操作方法
|
|
327
|
-
async function testSortedSetOperations() {
|
|
328
|
-
const redis = createRedisInstance();
|
|
329
|
-
const key = getTestKey('zset_op');
|
|
330
|
-
|
|
331
|
-
// 测试zadd方法
|
|
332
|
-
const addResult = await redis.zadd(key, 1, 'member1');
|
|
333
|
-
if (!addResult) {
|
|
334
|
-
throw new Error(`zadd 测试失败,返回: ${addResult}`);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// 测试zscore方法
|
|
338
|
-
const score = await redis.zscore(key, 'member1');
|
|
339
|
-
if (score !== 1) {
|
|
340
|
-
throw new Error(`zscore 测试失败,期望值: 1, 实际值: ${score}`);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
// 测试zrange方法
|
|
344
|
-
const range = await redis.zrange(key, 0, -1);
|
|
345
|
-
if (!range || range[0] !== 'member1') {
|
|
346
|
-
throw new Error(`zrange 测试失败,期望值: ['member1'], 实际值: ${JSON.stringify(range)}`);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
await redis.close();
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
// 测试错误处理事件
|
|
353
|
-
async function testErrorEvents() {
|
|
354
|
-
const redis = createRedisInstance();
|
|
355
|
-
const key = getTestKey('error_event');
|
|
356
|
-
let errorEventTriggered = false;
|
|
357
|
-
|
|
358
|
-
// 监听错误事件(使用一个不会触发真正错误的操作来验证事件机制)
|
|
359
|
-
redis.on('get_error', (data) => {
|
|
360
|
-
console.log('get_error 事件触发:', data);
|
|
361
|
-
errorEventTriggered = true;
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
// 正常获取一个不存在的键不会触发错误事件,但可以验证错误处理机制
|
|
365
|
-
const value = await redis.get(key); // 正常操作,不应该触发错误
|
|
366
|
-
|
|
367
|
-
// 由于我们无法直接在测试环境中触发错误(会导致测试失败),这里只是确认机制存在
|
|
368
|
-
console.log('错误事件测试:验证事件监听器已正确注册');
|
|
369
|
-
|
|
370
|
-
await redis.close();
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// 测试JSON数据序列化和反序列化
|
|
374
|
-
async function testJsonSerialization() {
|
|
375
|
-
const redis = createRedisInstance();
|
|
376
|
-
const key = getTestKey('json_test');
|
|
377
|
-
|
|
378
|
-
// 测试对象序列化和反序列化
|
|
379
|
-
const objData = { name: 'test', value: 123, active: true };
|
|
380
|
-
await redis.set(key, objData);
|
|
381
|
-
const retrievedObj = await redis.get(key);
|
|
382
|
-
|
|
383
|
-
if (JSON.stringify(retrievedObj) !== JSON.stringify(objData)) {
|
|
384
|
-
throw new Error(`JSON 序列化测试失败,期望值: ${JSON.stringify(objData)}, 实际值: ${JSON.stringify(retrievedObj)}`);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
await redis.close();
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// 运行所有测试
|
|
391
|
-
async function runAllTests() {
|
|
392
|
-
try {
|
|
393
|
-
console.log('开始测试 mm_redis 模块的数据增删改查方法和事件...');
|
|
394
|
-
console.log('='.repeat(60));
|
|
395
|
-
|
|
396
|
-
// 清理之前的测试数据
|
|
397
|
-
await cleanup();
|
|
398
|
-
|
|
399
|
-
// 运行各项测试
|
|
400
|
-
await runTest('字符串操作', testStringOperations);
|
|
401
|
-
await runTest('事件处理', testEvents);
|
|
402
|
-
await runTest('列表操作', testListOperations);
|
|
403
|
-
await runTest('哈希操作', testHashOperations);
|
|
404
|
-
await runTest('批量操作', testBatchOperations);
|
|
405
|
-
await runTest('过期时间设置', testExpireOperations);
|
|
406
|
-
await runTest('集合操作', testSetOperations);
|
|
407
|
-
await runTest('有序集合操作', testSortedSetOperations);
|
|
408
|
-
await runTest('错误处理事件', testErrorEvents);
|
|
409
|
-
await runTest('JSON序列化', testJsonSerialization);
|
|
410
|
-
|
|
411
|
-
// 再次清理测试数据
|
|
412
|
-
await cleanup();
|
|
413
|
-
|
|
414
|
-
// 输出测试结果统计
|
|
415
|
-
console.log('\n' + '='.repeat(60));
|
|
416
|
-
console.log('测试结果统计:');
|
|
417
|
-
console.log(`总测试数: ${testResults.total}`);
|
|
418
|
-
console.log(`通过测试: ${testResults.passed}`);
|
|
419
|
-
console.log(`失败测试: ${testResults.failed}`);
|
|
420
|
-
console.log(`跳过测试: ${testResults.skipped}`);
|
|
421
|
-
|
|
422
|
-
if (testResults.failed > 0) {
|
|
423
|
-
console.log(`\n❌ 有测试失败,失败的测试: ${testResults.failedTests.join(', ')}`);
|
|
424
|
-
} else {
|
|
425
|
-
console.log('\n🎉 所有测试通过!Redis模块的数据增删改查方法和事件功能正常!');
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
const passRate = (testResults.passed / testResults.total * 100).toFixed(2);
|
|
429
|
-
console.log(`测试通过率: ${passRate}%`);
|
|
430
|
-
|
|
431
|
-
// 退出代码反映测试结果
|
|
432
|
-
process.exit(testResults.failed > 0 ? 1 : 0);
|
|
433
|
-
|
|
434
|
-
} catch (error) {
|
|
435
|
-
console.error('测试过程中发生未处理的错误:', error);
|
|
436
|
-
process.exit(1);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// 启动测试
|
|
441
|
-
runAllTests();
|
package/test_real_redis.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
// 真实Redis环境测试脚本
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
// 简单的日志函数
|
|
6
|
-
function log(message, data = null) {
|
|
7
|
-
const timestamp = new Date().toISOString();
|
|
8
|
-
if (data) {
|
|
9
|
-
console.log(`[${timestamp}] ${message}`, typeof data === 'object' ? JSON.stringify(data, null, 2) : data);
|
|
10
|
-
} else {
|
|
11
|
-
console.log(`[${timestamp}] ${message}`);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// 加载配置文件的函数
|
|
16
|
-
function loadConfig(configPath) {
|
|
17
|
-
try {
|
|
18
|
-
const configContent = fs.readFileSync(configPath, 'utf8');
|
|
19
|
-
return JSON.parse(configContent);
|
|
20
|
-
} catch (error) {
|
|
21
|
-
log(`错误:无法加载配置文件 ${configPath}: ${error.message}`);
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// 主测试函数
|
|
27
|
-
async function runRealRedisTest() {
|
|
28
|
-
log('开始真实Redis环境测试...');
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
// 加载Redis模块
|
|
32
|
-
const RedisModule = require('./index.js');
|
|
33
|
-
const Redis = RedisModule.default || RedisModule;
|
|
34
|
-
const redis_admin = RedisModule.redis_admin;
|
|
35
|
-
|
|
36
|
-
log(`Redis类类型: ${typeof Redis}`);
|
|
37
|
-
log(`redis_admin函数类型: ${typeof redis_admin}`);
|
|
38
|
-
|
|
39
|
-
// 加载配置
|
|
40
|
-
const configPath = path.join(__dirname, 'config.json');
|
|
41
|
-
const config = loadConfig(configPath);
|
|
42
|
-
|
|
43
|
-
if (!config) {
|
|
44
|
-
log('无法继续测试,配置文件加载失败');
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
log('加载配置成功:', config);
|
|
49
|
-
|
|
50
|
-
// 测试1: 使用redis_admin
|
|
51
|
-
log('\n测试1: 使用redis_admin创建实例');
|
|
52
|
-
const redisInstance = redis_admin('sys');
|
|
53
|
-
redisInstance.setConfig(config);
|
|
54
|
-
|
|
55
|
-
// 尝试连接
|
|
56
|
-
log('尝试连接到Redis服务器...');
|
|
57
|
-
try {
|
|
58
|
-
await redisInstance.open();
|
|
59
|
-
log('✅ 成功连接到Redis服务器!');
|
|
60
|
-
|
|
61
|
-
// 测试基本操作
|
|
62
|
-
log('\n测试基本set/get操作:');
|
|
63
|
-
const testKey = 'test_real_redis_key';
|
|
64
|
-
const testValue = { message: 'Hello from real Redis test', timestamp: new Date().toISOString() };
|
|
65
|
-
|
|
66
|
-
await redisInstance.set(testKey, testValue);
|
|
67
|
-
log(`✅ 设置键 ${testKey} 成功`);
|
|
68
|
-
|
|
69
|
-
const retrievedValue = await redisInstance.get(testKey);
|
|
70
|
-
log(`✅ 获取键 ${testKey} 成功,值为:`, retrievedValue);
|
|
71
|
-
|
|
72
|
-
// 清理测试数据
|
|
73
|
-
await redisInstance.del([testKey]);
|
|
74
|
-
log(`✅ 清理测试数据成功`);
|
|
75
|
-
|
|
76
|
-
// 关闭连接
|
|
77
|
-
await redisInstance.close();
|
|
78
|
-
log('✅ 关闭连接成功');
|
|
79
|
-
|
|
80
|
-
} catch (connectError) {
|
|
81
|
-
log(`❌ 连接Redis服务器失败: ${connectError.message}`);
|
|
82
|
-
log('请确保Redis服务器正在运行,并且配置正确');
|
|
83
|
-
log('检查点:');
|
|
84
|
-
log(`- Redis服务器地址: ${config.host}:${config.port}`);
|
|
85
|
-
log(`- 密码是否正确: ${config.password ? '已设置' : '未设置'}`);
|
|
86
|
-
log(`- 数据库索引: ${config.database}`);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
} catch (error) {
|
|
90
|
-
log(`❌ 测试过程中出现错误: ${error.message}`);
|
|
91
|
-
log(error.stack);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
log('\n测试完成!');
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// 运行测试
|
|
98
|
-
runRealRedisTest().then(() => {
|
|
99
|
-
// 确保测试成功完成后以退出码0结束
|
|
100
|
-
process.exit(0);
|
|
101
|
-
}).catch(err => {
|
|
102
|
-
log(`❌ 测试执行失败: ${err.message}`);
|
|
103
|
-
process.exit(1);
|
|
104
|
-
});
|