Skip to content

Commit 0a08873

Browse files
committed
chore: improve tcp packets sent test by using tun interfaces
1 parent 5141839 commit 0a08873

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

packages/tcpip/src/stack.test.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -844,16 +844,28 @@ describe('tcp', () => {
844844
});
845845

846846
test('tcp packets are sent immediately without delay', async () => {
847-
const stack = await createStack();
847+
const stack1 = await createStack();
848+
const stack2 = await createStack();
848849

849-
const listener = await stack.listenTcp({
850-
host: '127.0.0.1',
850+
const tun1 = await stack1.createTunInterface({
851+
ip: '192.168.1.1/24',
852+
});
853+
854+
const tun2 = await stack2.createTunInterface({
855+
ip: '192.168.1.2/24',
856+
});
857+
858+
// Connect the two interfaces
859+
tun1.readable.pipeTo(tun2.writable);
860+
tun2.readable.pipeTo(tun1.writable);
861+
862+
const listener = await stack2.listenTcp({
851863
port: 8080,
852864
});
853865

854866
const [outbound, inbound] = await Promise.all([
855-
stack.connectTcp({
856-
host: '127.0.0.1',
867+
stack1.connectTcp({
868+
host: '192.168.1.2',
857869
port: 8080,
858870
}),
859871
nextValue(listener),
@@ -866,20 +878,20 @@ describe('tcp', () => {
866878

867879
// Record start time
868880
const startTime = performance.now();
869-
881+
870882
// Write and read immediately
871883
await outboundWriter.write(data);
872884
const received = await inboundReader.read();
873885

874886
// Record end time
875887
const endTime = performance.now();
876-
888+
877889
expect(received.value).toStrictEqual(data);
878-
890+
879891
// Check timing - with tcp_output enabled, this should be very fast
880892
// Without tcp_output, there would be a significant delay (~300ms)
881893
const elapsed = endTime - startTime;
882-
expect(elapsed).toBeLessThan(200);
894+
expect(elapsed).toBeCloseTo(0, -1);
883895
});
884896
});
885897

0 commit comments

Comments
 (0)