From def2f0efe9951f8c9b99e6587bfbae4b21118323 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Tue, 2 Jun 2020 14:48:09 +0100 Subject: [PATCH] remove XCTest expectations with timeouts (#1539) Motivation: The XCTest expectations caused problems when they time out. When they time out, the execution continues which in the tests in question then shut down the EventLoop. Shutting down the event loop meant that other things don't happen which caused the test to hang. The XCTest expectations however tell us about the failure at the end of the test (which we never reach). Modifications: Remove the XCTest expectations with timeouts. Result: Even slow CI should pass this test correctly. Co-authored-by: Cory Benfield --- Tests/NIOTests/EventLoopTest.swift | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Tests/NIOTests/EventLoopTest.swift b/Tests/NIOTests/EventLoopTest.swift index b89c44b4f..51dd04a2e 100644 --- a/Tests/NIOTests/EventLoopTest.swift +++ b/Tests/NIOTests/EventLoopTest.swift @@ -143,9 +143,10 @@ public final class EventLoopTest : XCTestCase { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } - let expect = expectation(description: "Is cancelling RepatedTask") let counter = NIOAtomic.makeAtomic(value: 0) let loop = eventLoopGroup.next() + let allDone = DispatchGroup() + allDone.enter() loop.scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { repeatedTask -> Void in XCTAssertTrue(loop.inEventLoop) let initialValue = counter.load() @@ -153,16 +154,15 @@ public final class EventLoopTest : XCTestCase { if initialValue == 0 { XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay) } else if initialValue == count { - expect.fulfill() repeatedTask.cancel() + allDone.leave() } } - waitForExpectations(timeout: 1) { error in - XCTAssertNil(error) - XCTAssertEqual(counter.load(), count + 1) - XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay + Int64(count) * delay) - } + allDone.wait() + + XCTAssertEqual(counter.load(), count + 1) + XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay + Int64(count) * delay) } public func testScheduledTaskThatIsImmediatelyCancelledNeverFires() throws { @@ -226,10 +226,11 @@ public final class EventLoopTest : XCTestCase { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } - let expect = expectation(description: "Is cancelling RepatedTask") - let group = DispatchGroup() + let hasFiredGroup = DispatchGroup() + let isCancelledGroup = DispatchGroup() let loop = eventLoopGroup.next() - group.enter() + hasFiredGroup.enter() + isCancelledGroup.enter() var isAllowedToFire = true // read/write only on `loop` var hasFired = false // read/write only on `loop` let repeatedTask = loop.scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { (_: RepeatedTask) -> Void in @@ -238,23 +239,22 @@ public final class EventLoopTest : XCTestCase { // we can only do this once as we can only leave the DispatchGroup once but we might lose a race and // the timer might fire more than once (until `shouldNoLongerFire` becomes true). hasFired = true - group.leave() - expect.fulfill() + hasFiredGroup.leave() } XCTAssertTrue(isAllowedToFire) } - group.notify(queue: DispatchQueue.global()) { + hasFiredGroup.notify(queue: DispatchQueue.global()) { repeatedTask.cancel() loop.execute { // only now do we know that the `cancel` must have gone through isAllowedToFire = false + isCancelledGroup.leave() } } - waitForExpectations(timeout: 1) { error in - XCTAssertNil(error) - XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay) - } + hasFiredGroup.wait() + XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay) + isCancelledGroup.wait() } public func testScheduleRepeatedTaskToNotRetainRepeatedTask() throws {