From 2ef87d45e679179a4d0e78f6f07c4e56039349c9 Mon Sep 17 00:00:00 2001 From: hamzahrmalik Date: Thu, 25 Jul 2024 15:02:43 +0100 Subject: [PATCH] Make testInstantTCPConnectionResetThrowsError more reliable (#2810) --- Tests/NIOPosixTests/SocketChannelTest.swift | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Tests/NIOPosixTests/SocketChannelTest.swift b/Tests/NIOPosixTests/SocketChannelTest.swift index 80133abbdd..0164bb2c0d 100644 --- a/Tests/NIOPosixTests/SocketChannelTest.swift +++ b/Tests/NIOPosixTests/SocketChannelTest.swift @@ -611,7 +611,8 @@ public final class SocketChannelTest: XCTestCase { #if !os(Linux) && !os(Android) // This test checks that we correctly fail with an error rather than // asserting or silently ignoring if a client aborts the connection - // early with a RST during or immediately after accept(). + // early with a RST before accept(). The behaviour is the same as closing the socket + // during the accept. But it is easier to test closing the socket before the accept, than during it. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) defer { XCTAssertNoThrow(try group.syncShutdownGracefully()) } @@ -660,8 +661,13 @@ public final class SocketChannelTest: XCTestCase { XCTAssertNoThrow(try clientSocket.connect(to: serverChannel.localAddress!)) XCTAssertNoThrow(try clientSocket.close()) - // Trigger accept() in the server - serverChannel.read() + // We wait here to allow slow machines to close the socket + // We want to ensure the socket is closed before we trigger accept + // That will trigger the error that we want to test for + group.any().scheduleTask(in: .seconds(1)) { + // Trigger accept() in the server + serverChannel.read() + } // Wait for the server to have something XCTAssertThrowsError(try serverPromise.futureResult.wait()) { error in