From d371bb5a10d8ea063c30cedecfeb647015478f53 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Thu, 18 Jun 2020 18:01:30 +0100 Subject: [PATCH] fix SAL non-determinism (#1562) Motivation: The SAL tests suffered from a rare non-determinism: When we go onto the EventLoop to get a value and then leave it, we previously just assumed that the EventLoop would park itself before we did any futher operations on it. That was mostly true because the tests are doing one thing after the other. There was however a narrow race: If the tests enqueued new work onto the EventLoop before the EventLoop parked itself, then the new work would be run _before_ the EL parks itself. That would make the _next_ operation fail because the operations usually start by checking that we're parked because that's a synchronisation point between tests and EL. Modifications: Actually wait until the EL's parked. That proves we found our synchronisation point. Result: - fixes #1514 - fixes #1545 - fixes #1548 - fixes #1550 --- Tests/NIOTests/SyscallAbstractionLayer.swift | 25 ++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Tests/NIOTests/SyscallAbstractionLayer.swift b/Tests/NIOTests/SyscallAbstractionLayer.swift index e4bf1bd9f..fb2c3326a 100644 --- a/Tests/NIOTests/SyscallAbstractionLayer.swift +++ b/Tests/NIOTests/SyscallAbstractionLayer.swift @@ -114,6 +114,18 @@ final class LockedBox { } } +extension LockedBox where T == UserToKernel { + func assertParkedRightNow(file: StaticString = #file, line: UInt = #line) throws { + SAL.printIfDebug("\(#function)") + let syscall = try self.waitForValue() + if case .whenReady = syscall { + return + } else { + XCTFail("unexpected syscall \(syscall)", file: (file), line: line) + } + } +} + enum UserToKernel { case localAddress case remoteAddress @@ -392,6 +404,9 @@ extension HookedSelector { try self.wakeups.takeValue() } + func assertParkedRightNow(file: StaticString = #file, line: UInt = #line) throws { + try self.userToKernel.assertParkedRightNow(file: file, line: line) + } } extension EventLoop { @@ -410,6 +425,7 @@ extension EventLoop { } try hookedSelector.assertWakeup(file: (file), line: line) try syscallAssertions() + try hookedSelector.userToKernel.assertParkedRightNow() // We need the EventLoop to finish running its tick. return try box.takeValue().get() } } @@ -434,6 +450,7 @@ extension EventLoopFuture { } } try hookedSelector.assertWakeup() + try hookedSelector.userToKernel.assertParkedRightNow() // We need the EventLoop to finish running its tick. return try box.takeValue().get() } } @@ -585,13 +602,7 @@ extension SALTest { } func assertParkedRightNow(file: StaticString = #file, line: UInt = #line) throws { - SAL.printIfDebug("\(#function)") - let syscall = try self.userToKernelBox.waitForValue() - if case .whenReady = syscall { - return - } else { - XCTFail("unexpected syscall \(syscall)", file: (file), line: line) - } + try self.userToKernelBox.assertParkedRightNow(file: file, line: line) } func assertWaitingForNotification(result: SelectorEvent?,