Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
- name: Run Backend Tests
run: |
ls -la src/generated/prisma
npx vitest run --coverage --reporter=basic
npx vitest run --coverage --reporter=basic --no-file-parallelism
working-directory: backend
env:
DATABASE_URL: postgresql://postgres:password@127.0.0.1:5432/flowfi_test
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-test-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
DATABASE_URL: postgresql://postgres:password@127.0.0.1:5432/flowfi_test

- name: Run backend tests
run: npm test
run: npm test -- --no-file-parallelism
working-directory: backend
env:
DATABASE_URL: postgresql://postgres:password@127.0.0.1:5432/flowfi_test
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/integration/stream-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function createStreamCreatedEvent(
];

const overrideEntries: [string, xdr.ScVal][] = Object.entries(overrides).map(
([k, v]) => [k, nativeToScVal(v)],
([k, v]) => [k, typeof v === 'bigint' ? scvI128(v) : nativeToScVal(v)],
);

return {
Expand Down
2 changes: 2 additions & 0 deletions backend/tests/stream.validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ describe('Stream Validator', () => {
};
const result = createStreamSchema.safeParse(data);
expect(result.success).toBe(false);
expect(result.error).toBeDefined();
expect(result.error!.issues.length).toBeGreaterThan(0);
expect(result.error!.issues[0]!.message).toBe('Rate exceeds maximum allowed value');
});

Expand Down
52 changes: 52 additions & 0 deletions frontend/src/__tests__/useStreamEvents.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class MockEventSource {
static instance: MockEventSource | null = null;
static instanceCount = 0;

url: string;
onopen: (() => void) | null = null;
Expand All @@ -21,6 +22,7 @@
constructor(url: string) {
this.url = url;
MockEventSource.instance = this;
MockEventSource.instanceCount += 1;
}

addEventListener(type: string, handler: EventHandler) {
Expand Down Expand Up @@ -66,6 +68,7 @@
describe('useStreamEvents', () => {
beforeEach(() => {
MockEventSource.instance = null;
MockEventSource.instanceCount = 0;
vi.useFakeTimers();
});

Expand Down Expand Up @@ -280,4 +283,53 @@

expect(result.current.events).toHaveLength(types.length);
});

it('creates only one EventSource across multiple re-renders and incoming events', () => {
const { result, rerender } = renderHook(
(opts: { streamIds: string[] } = { streamIds: ['1'] }) =>
useStreamEvents({ ...opts, autoReconnect: false }),
);

const firstInstance = MockEventSource.instance;

act(() => { firstInstance?.open(); });

// Simulate multiple re-renders with the same subscription (inline array)
rerender({ streamIds: ['1'] });
rerender({ streamIds: ['1'] });
rerender({ streamIds: ['1'] });

// Simulate incoming events causing re-renders of the consumer
act(() => {
MockEventSource.instance?.emit('stream.created', { i: 1 });
MockEventSource.instance?.emit('stream.created', { i: 2 });
MockEventSource.instance?.emit('stream.created', { i: 3 });
});

expect(result.current.events).toHaveLength(3);

// Re-render again after events
rerender({ streamIds: ['1'] });
rerender({ streamIds: ['1'] });

expect(MockEventSource.instanceCount).toBe(1);
expect(MockEventSource.instance).toBe(firstInstance);
});

it('stops reconnecting after reaching the cap', () => {
renderHook(() =>
useStreamEvents({ streamIds: ['1'], autoReconnect: true, maxRetryDelay: 1000 }),
);

// Trigger errors repeatedly to consume reconnect attempts.
// The reconnect delay stays at 1000ms (capped by maxRetryDelay).
for (let i = 0; i < 25; i++) {
act(() => { MockEventSource.instance?.triggerError(); });
act(() => { vi.advanceTimersByTime(2000); });
}

// 1 initial + 20 reconnect attempts = 21 instances max.
// After the 20th reconnect attempt, no more timers should fire.
expect(MockEventSource.instanceCount).toBeLessThanOrEqual(21);

Check failure on line 333 in frontend/src/__tests__/useStreamEvents.test.tsx

View workflow job for this annotation

GitHub Actions / Frontend CI

src/__tests__/useStreamEvents.test.tsx > useStreamEvents > stops reconnecting after reaching the cap

AssertionError: expected 26 to be less than or equal to 21 ❯ src/__tests__/useStreamEvents.test.tsx:333:43
});
});
35 changes: 24 additions & 11 deletions frontend/src/hooks/useStreamEvents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState, useCallback, useRef } from 'react';
import { useEffect, useState, useCallback, useRef, useMemo } from 'react';

interface StreamEvent {
type: 'created' | 'topped_up' | 'withdrawn' | 'cancelled' | 'completed' | 'paused' | 'resumed';
Expand All @@ -23,12 +23,13 @@
clearEvents: () => void;
}

const MAX_RECONNECT_ATTEMPTS = 20;

Check warning on line 26 in frontend/src/hooks/useStreamEvents.ts

View workflow job for this annotation

GitHub Actions / Frontend CI

'MAX_RECONNECT_ATTEMPTS' is assigned a value but never used

export function useStreamEvents(
options: UseStreamEventsOptions = {}
): UseStreamEventsReturn {
const {
streamIds = [],
// userPublicKeys = [],
streamIds: rawStreamIds = [],
subscribeToAll = false,
autoReconnect = true,
maxRetryDelay = 30000,
Expand All @@ -43,32 +44,43 @@
const eventSourceRef = useRef<EventSource | null>(null);
const retryDelayRef = useRef(1000);
const reconnectTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const reconnectAttemptsRef = useRef(0);
const connectRef = useRef<() => void>(() => undefined);

const subscriptionKey = useMemo(() => {
const streams = [...rawStreamIds].sort().join(',');
return `${subscribeToAll ? 'all' : streams}|${jwtToken || ''}`;
}, [rawStreamIds, subscribeToAll, jwtToken]);

const buildUrl = useCallback(() => {
const params = new URLSearchParams();

if (subscribeToAll) {
params.append('all', 'true');
} else {
streamIds.forEach(id => params.append('streams', id));
rawStreamIds.forEach(id => params.append('streams', id));
}

// Add JWT token to query string for authentication
// (EventSource doesn't support custom headers in browser)
if (jwtToken) {
params.append('token', jwtToken);
}

const baseUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001';
return `${baseUrl}/v1/events/subscribe?${params}`;
}, [streamIds, subscribeToAll, jwtToken]);
// subscriptionKey captures all subscription parameters as a stable string
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [subscriptionKey]);

const clearEvents = useCallback(() => {
setEvents([]);
}, []);

const connect = useCallback(() => {
if (reconnectTimeoutRef.current !== null) {
clearTimeout(reconnectTimeoutRef.current);
reconnectTimeoutRef.current = null;
}

const url = buildUrl();
const eventSource = new EventSource(url);
eventSourceRef.current = eventSource;
Expand All @@ -77,16 +89,16 @@
setConnected(true);
setReconnecting(false);
setError(null);
retryDelayRef.current = 1000; // Reset retry delay
retryDelayRef.current = 1000;
reconnectAttemptsRef.current = 0;
};


const handleEvent = (type: StreamEvent['type']) => (e: MessageEvent) => {
try {
const data = JSON.parse(e.data);
setEvents((prev: StreamEvent[]) => [
{ type, data, timestamp: Date.now() },
...prev.slice(0, 99), // Keep last 100 events
...prev.slice(0, 99),
]);
} catch {
// Silently ignore malformed event messages
Expand Down Expand Up @@ -132,8 +144,9 @@
eventSourceRef.current.close();
eventSourceRef.current = null;
}
if (reconnectTimeoutRef.current) {
if (reconnectTimeoutRef.current !== null) {
clearTimeout(reconnectTimeoutRef.current);
reconnectTimeoutRef.current = null;
}
};
}, [connect]);
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ const isDev = process.env.NODE_ENV !== "production";

export const logger = {
debug: (...args: unknown[]) => {
if (isDev) console.debug(...args); // eslint-disable-line no-console
if (isDev) console.debug(...args);
},
info: (...args: unknown[]) => {
if (isDev) console.info(...args); // eslint-disable-line no-console
if (isDev) console.info(...args);
},
warn: (...args: unknown[]) => {
if (isDev) console.warn(...args); // eslint-disable-line no-console
if (isDev) console.warn(...args);
},
// errors always surface, even in production
error: (...args: unknown[]) => {
console.error(...args); // eslint-disable-line no-console
console.error(...args);
},
};
Loading