feat(framework): server testing utils, transaction storage simplify, pathfinding tests (#384)

## Server Testing Utils
- Add TestServer, TestClient, MockRoom for unit testing
- Export testing utilities from @esengine/server/testing

## Transaction Storage (BREAKING)
- Simplify RedisStorage/MongoStorage to factory pattern only
- Remove DI client injection option
- Add lazy connection and Symbol.asyncDispose support
- Add 161 unit tests with full coverage

## Pathfinding Tests
- Add 150 unit tests covering all components
- BinaryHeap, Heuristics, AStarPathfinder, GridMap, NavMesh, PathSmoother

## Docs
- Update storage.md for new factory pattern API
This commit is contained in:
YHH
2025-12-29 15:02:13 +08:00
committed by GitHub
parent 10c3891abd
commit 3b978384c7
50 changed files with 7591 additions and 660 deletions

View File

@@ -9,6 +9,9 @@ All storage implementations must implement the `ITransactionStorage` interface:
```typescript
interface ITransactionStorage {
// Lifecycle
close?(): Promise<void>;
// Distributed lock
acquireLock(key: string, ttl: number): Promise<string | null>;
releaseLock(key: string, token: string): Promise<boolean>;
@@ -62,21 +65,29 @@ console.log(storage.transactionCount);
## RedisStorage
Redis storage, suitable for production distributed systems.
Redis storage, suitable for production distributed systems. Uses factory pattern with lazy connection.
```typescript
import Redis from 'ioredis';
import { RedisStorage } from '@esengine/transaction';
const redis = new Redis('redis://localhost:6379');
// Factory pattern: lazy connection, connects on first operation
const storage = new RedisStorage({
client: redis,
factory: () => new Redis('redis://localhost:6379'),
prefix: 'tx:', // Key prefix
transactionTTL: 86400, // Transaction log TTL (seconds)
});
const manager = new TransactionManager({ storage });
// Close connection when done
await storage.close();
// Or use await using for automatic cleanup (TypeScript 5.2+)
await using storage = new RedisStorage({
factory: () => new Redis('redis://localhost:6379')
});
// Automatically closed when scope ends
```
### Characteristics
@@ -114,18 +125,20 @@ tx:data:{key} - Business data
## MongoStorage
MongoDB storage, suitable for scenarios requiring persistence and complex queries.
MongoDB storage, suitable for scenarios requiring persistence and complex queries. Uses factory pattern with lazy connection.
```typescript
import { MongoClient } from 'mongodb';
import { MongoStorage } from '@esengine/transaction';
const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
const db = client.db('game');
// Factory pattern: lazy connection, connects on first operation
const storage = new MongoStorage({
db,
factory: async () => {
const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
return client;
},
database: 'game',
transactionCollection: 'transactions', // Transaction log collection
dataCollection: 'transaction_data', // Business data collection
lockCollection: 'transaction_locks', // Lock collection
@@ -135,6 +148,12 @@ const storage = new MongoStorage({
await storage.ensureIndexes();
const manager = new TransactionManager({ storage });
// Close connection when done
await storage.close();
// Or use await using for automatic cleanup (TypeScript 5.2+)
await using storage = new MongoStorage({ ... });
```
### Characteristics

View File

@@ -9,6 +9,9 @@ description: "事务存储接口和实现MemoryStorage、RedisStorage、Mongo
```typescript
interface ITransactionStorage {
// 生命周期
close?(): Promise<void>;
// 分布式锁
acquireLock(key: string, ttl: number): Promise<string | null>;
releaseLock(key: string, token: string): Promise<boolean>;
@@ -62,21 +65,29 @@ console.log(storage.transactionCount);
## RedisStorage
Redis 存储,适用于生产环境的分布式系统。
Redis 存储,适用于生产环境的分布式系统。使用工厂模式实现惰性连接。
```typescript
import Redis from 'ioredis';
import { RedisStorage } from '@esengine/transaction';
const redis = new Redis('redis://localhost:6379');
// 工厂模式:惰性连接,首次操作时才创建连接
const storage = new RedisStorage({
client: redis,
factory: () => new Redis('redis://localhost:6379'),
prefix: 'tx:', // 键前缀
transactionTTL: 86400, // 事务日志过期时间(秒)
});
const manager = new TransactionManager({ storage });
// 使用后关闭连接
await storage.close();
// 或使用 await using 自动关闭 (TypeScript 5.2+)
await using storage = new RedisStorage({
factory: () => new Redis('redis://localhost:6379')
});
// 作用域结束时自动关闭
```
### 特点
@@ -114,18 +125,20 @@ tx:data:{key} - 业务数据
## MongoStorage
MongoDB 存储,适用于需要持久化和复杂查询的场景。
MongoDB 存储,适用于需要持久化和复杂查询的场景。使用工厂模式实现惰性连接。
```typescript
import { MongoClient } from 'mongodb';
import { MongoStorage } from '@esengine/transaction';
const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
const db = client.db('game');
// 工厂模式:惰性连接,首次操作时才创建连接
const storage = new MongoStorage({
db,
factory: async () => {
const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
return client;
},
database: 'game',
transactionCollection: 'transactions', // 事务日志集合
dataCollection: 'transaction_data', // 业务数据集合
lockCollection: 'transaction_locks', // 锁集合
@@ -135,6 +148,12 @@ const storage = new MongoStorage({
await storage.ensureIndexes();
const manager = new TransactionManager({ storage });
// 使用后关闭连接
await storage.close();
// 或使用 await using 自动关闭 (TypeScript 5.2+)
await using storage = new MongoStorage({ ... });
```
### 特点