Skip to content

Commit 16fa735

Browse files
committed
fix: fixed acceptance tests
Signed-off-by: Logan Nguyen <[email protected]>
1 parent a59d8a1 commit 16fa735

File tree

3 files changed

+16
-137
lines changed

3 files changed

+16
-137
lines changed

packages/server/tests/acceptance/rateLimiter.spec.ts

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -41,56 +41,16 @@ describe('@ratelimiter Shared Rate Limiting Acceptance Tests', function () {
4141
registryA = new Registry();
4242
registryB = new Registry();
4343

44+
// Create Redis stores
45+
const storeA = new RedisRateLimitStore(redisClient, logger.child({ service: 'A' }), DURATION);
46+
const storeB = new RedisRateLimitStore(redisClient, logger.child({ service: 'B' }), DURATION);
47+
4448
// Create two IPRateLimiterService instances pointing to the same Redis
45-
serviceA = new IPRateLimiterService(logger.child({ service: 'A' }), registryA, DURATION);
46-
serviceB = new IPRateLimiterService(logger.child({ service: 'B' }), registryB, DURATION);
47-
48-
// Wait for Redis connections to establish and verify they're connected
49-
let retries = 10;
50-
while (retries > 0) {
51-
const storeA = serviceA.rateLimitStore;
52-
const storeB = serviceB.rateLimitStore;
53-
54-
// Check if stores are Redis stores before accessing Redis methods
55-
if (storeA instanceof RedisRateLimitStore && storeB instanceof RedisRateLimitStore) {
56-
const connectedA = await storeA.isConnected();
57-
const connectedB = await storeB.isConnected();
58-
if (connectedA && connectedB) {
59-
break;
60-
}
61-
} else {
62-
// If not Redis stores, just break - LRU stores don't need connection setup
63-
break;
64-
}
65-
await new Promise((resolve) => setTimeout(resolve, 100));
66-
retries--;
67-
}
68-
69-
// Verify both services are connected to Redis (only if using Redis stores)
70-
const storeA = serviceA.rateLimitStore;
71-
const storeB = serviceB.rateLimitStore;
72-
if (storeA instanceof RedisRateLimitStore && storeB instanceof RedisRateLimitStore) {
73-
const finalConnectedA = await storeA.isConnected();
74-
const finalConnectedB = await storeB.isConnected();
75-
if (!finalConnectedA || !finalConnectedB) {
76-
throw new Error(`Redis connection failed: serviceA=${finalConnectedA}, serviceB=${finalConnectedB}`);
77-
}
78-
}
49+
serviceA = new IPRateLimiterService(storeA, logger.child({ service: 'A' }), registryA);
50+
serviceB = new IPRateLimiterService(storeB, logger.child({ service: 'B' }), registryB);
7951
});
8052

8153
after(async function () {
82-
// Clean up services and Redis connections
83-
const storeA = serviceA.rateLimitStore;
84-
const storeB = serviceB.rateLimitStore;
85-
86-
// Only disconnect if stores are Redis stores
87-
if (storeA instanceof RedisRateLimitStore) {
88-
await storeA.disconnect();
89-
}
90-
if (storeB instanceof RedisRateLimitStore) {
91-
await storeB.disconnect();
92-
}
93-
9454
await redisClient.quit();
9555

9656
// Clean up environment variables
@@ -107,16 +67,6 @@ describe('@ratelimiter Shared Rate Limiting Acceptance Tests', function () {
10767

10868
describe('Shared Rate Limiting Between Services', function () {
10969
it('should share rate limit counters between two service instances', async function () {
110-
// Verify both services are using Redis
111-
const storeA = serviceA.rateLimitStore;
112-
const storeB = serviceB.rateLimitStore;
113-
if (storeA instanceof RedisRateLimitStore && storeB instanceof RedisRateLimitStore) {
114-
const redisA = await storeA.isConnected();
115-
const redisB = await storeB.isConnected();
116-
expect(redisA).to.be.true;
117-
expect(redisB).to.be.true;
118-
}
119-
12070
let rateLimited = false;
12171

12272
// Make exactly LIMIT requests through serviceA to hit the limit
@@ -244,18 +194,6 @@ describe('@ratelimiter Shared Rate Limiting Acceptance Tests', function () {
244194
});
245195

246196
describe('Service Independence and Failover', function () {
247-
it('should maintain independent Redis connections', async function () {
248-
// Both services should report connected to Redis
249-
const storeA = serviceA.rateLimitStore;
250-
const storeB = serviceB.rateLimitStore;
251-
if (storeA instanceof RedisRateLimitStore && storeB instanceof RedisRateLimitStore) {
252-
const connectedA = await storeA.isConnected();
253-
const connectedB = await storeB.isConnected();
254-
expect(connectedA).to.be.true;
255-
expect(connectedB).to.be.true;
256-
}
257-
});
258-
259197
it('should handle concurrent requests from both services', async function () {
260198
// Send concurrent requests from both services
261199
const promises: Promise<boolean>[] = [];

packages/ws-server/tests/acceptance/rateLimiter.spec.ts

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -42,56 +42,16 @@ describe('@web-socket-ratelimiter Shared Rate Limiting Acceptance Tests', functi
4242
registryA = new Registry();
4343
registryB = new Registry();
4444

45+
// Create Redis stores
46+
const storeA = new RedisRateLimitStore(redisClient, logger.child({ service: 'WS-A' }), DURATION);
47+
const storeB = new RedisRateLimitStore(redisClient, logger.child({ service: 'WS-B' }), DURATION);
48+
4549
// Create two IPRateLimiterService instances for WebSocket servers pointing to the same Redis
46-
wsServiceA = new IPRateLimiterService(logger.child({ service: 'WS-A' }), registryA, DURATION);
47-
wsServiceB = new IPRateLimiterService(logger.child({ service: 'WS-B' }), registryB, DURATION);
48-
49-
// Wait for Redis connections to establish and verify they're connected
50-
let retries = 10;
51-
while (retries > 0) {
52-
const storeA = wsServiceA.rateLimitStore;
53-
const storeB = wsServiceB.rateLimitStore;
54-
55-
// Check if stores are Redis stores before accessing Redis methods
56-
if (storeA instanceof RedisRateLimitStore && storeB instanceof RedisRateLimitStore) {
57-
const connectedA = await storeA.isConnected();
58-
const connectedB = await storeB.isConnected();
59-
if (connectedA && connectedB) {
60-
break;
61-
}
62-
} else {
63-
// If not Redis stores, just break - LRU stores don't need connection setup
64-
break;
65-
}
66-
await new Promise((resolve) => setTimeout(resolve, 100));
67-
retries--;
68-
}
69-
70-
// Verify both WebSocket services are connected to Redis (only if using Redis stores)
71-
const storeA = wsServiceA.rateLimitStore;
72-
const storeB = wsServiceB.rateLimitStore;
73-
if (storeA instanceof RedisRateLimitStore && storeB instanceof RedisRateLimitStore) {
74-
const finalConnectedA = await storeA.isConnected();
75-
const finalConnectedB = await storeB.isConnected();
76-
if (!finalConnectedA || !finalConnectedB) {
77-
throw new Error(`Redis connection failed: wsServiceA=${finalConnectedA}, wsServiceB=${finalConnectedB}`);
78-
}
79-
}
50+
wsServiceA = new IPRateLimiterService(storeA, logger.child({ service: 'WS-A' }), registryA);
51+
wsServiceB = new IPRateLimiterService(storeB, logger.child({ service: 'WS-B' }), registryB);
8052
});
8153

8254
after(async function () {
83-
// Clean up WebSocket services and Redis connections
84-
const storeA = wsServiceA.rateLimitStore;
85-
const storeB = wsServiceB.rateLimitStore;
86-
87-
// Only disconnect if stores are Redis stores
88-
if (storeA instanceof RedisRateLimitStore) {
89-
await storeA.disconnect();
90-
}
91-
if (storeB instanceof RedisRateLimitStore) {
92-
await storeB.disconnect();
93-
}
94-
9555
await redisClient.quit();
9656

9757
// Clean up environment variables
@@ -108,16 +68,6 @@ describe('@web-socket-ratelimiter Shared Rate Limiting Acceptance Tests', functi
10868

10969
describe('Shared WebSocket Rate Limiting Between Services', function () {
11070
it('should share rate limit counters between two WebSocket service instances', async function () {
111-
// Verify both WebSocket services are using Redis
112-
const storeA = wsServiceA.rateLimitStore;
113-
const storeB = wsServiceB.rateLimitStore;
114-
if (storeA instanceof RedisRateLimitStore && storeB instanceof RedisRateLimitStore) {
115-
const redisA = await storeA.isConnected();
116-
const redisB = await storeB.isConnected();
117-
expect(redisA).to.be.true;
118-
expect(redisB).to.be.true;
119-
}
120-
12171
let rateLimited = false;
12272

12373
// Make exactly LIMIT requests through wsServiceA to hit the limit
@@ -280,18 +230,6 @@ describe('@web-socket-ratelimiter Shared Rate Limiting Acceptance Tests', functi
280230
});
281231

282232
describe('WebSocket Service Independence and Failover', function () {
283-
it('should maintain independent Redis connections for WebSocket services', async function () {
284-
// Both WebSocket services should report connected to Redis
285-
const storeA = wsServiceA.rateLimitStore;
286-
const storeB = wsServiceB.rateLimitStore;
287-
if (storeA instanceof RedisRateLimitStore && storeB instanceof RedisRateLimitStore) {
288-
const connectedA = await storeA.isConnected();
289-
const connectedB = await storeB.isConnected();
290-
expect(connectedA).to.be.true;
291-
expect(connectedB).to.be.true;
292-
}
293-
});
294-
295233
it('should handle concurrent WebSocket requests from both services', async function () {
296234
// Send concurrent requests from both WebSocket services
297235
const promises: Promise<boolean>[] = [];

packages/ws-server/tests/unit/connectionLimiter.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ describe('Connection Limiter', function () {
6767
configServiceStub.withArgs('TIER_2_RATE_LIMIT').returns(800);
6868
configServiceStub.withArgs('TIER_3_RATE_LIMIT').returns(1600);
6969

70-
const rateLimiter = new IPRateLimiterService(mockLogger, mockRegistry, 9000);
70+
const mockStore = {
71+
incrementAndCheck: sinon.stub().resolves(false),
72+
};
73+
const rateLimiter = new IPRateLimiterService(mockStore as any, mockLogger, mockRegistry);
7174

7275
rateLimiterStub = sinon.stub(IPRateLimiterService.prototype, 'shouldRateLimit');
7376
connectionLimiter = new ConnectionLimiter(mockLogger, mockRegistry, rateLimiter);

0 commit comments

Comments
 (0)