From 9f9976adaef4f76e2a741663156d731d75da6f3b Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Wed, 29 Jan 2020 15:26:57 +0000 Subject: [PATCH] make some unnecessarily slow tests faster (#1365) Motivation: As can be seen in #1358, we have a bunch of very slow tests. Some of them are unnecessarily slow. Modifications: Make easy to make fast tests faster. Result: Faster test suite. --- .../NIOConcurrencyHelpersTests.swift | 34 +++++++++++++++---- Tests/NIOTests/AcceptBackoffHandlerTest.swift | 16 ++++++--- Tests/NIOTests/CircularBufferTests.swift | 6 ++-- Tests/NIOTests/HeapTests.swift | 11 ++---- Tests/NIOTests/PriorityQueueTests.swift | 2 +- 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift b/Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift index b2b81e4a8..072f77424 100644 --- a/Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift +++ b/Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift @@ -28,19 +28,29 @@ class NIOConcurrencyHelpersTests: XCTestCase { @available(*, deprecated, message: "deprecated because it tests deprecated functionality") func testLargeContendedAtomicSum() { - let noAsyncs: UInt64 = 64 - let noCounts: UInt64 = 200_000 + let noAsyncs: UInt64 = 50 + let noCounts: UInt64 = 2_000 let q = DispatchQueue(label: "q", attributes: .concurrent) let g = DispatchGroup() let ai = NIOConcurrencyHelpers.Atomic(value: 0) + let everybodyHere = DispatchSemaphore(value: 0) + let go = DispatchSemaphore(value: 0) for thread in 1...noAsyncs { q.async(group: g) { + everybodyHere.signal() + go.wait() for _ in 0...makeAtomic(value: 0) + let everybodyHere = DispatchSemaphore(value: 0) + let go = DispatchSemaphore(value: 0) for thread in 1...noAsyncs { q.async(group: g) { + everybodyHere.signal() + go.wait() for _ in 0...makeAtomic(value: 0) - let iterations = 100_000 + let iterations = 10_000 @inline(never) func doIt() { @@ -868,7 +888,7 @@ class NIOConcurrencyHelpersTests: XCTestCase { @available(*, deprecated, message: "AtomicBox is deprecated, this is a test for the deprecated functionality") func testLoadAndStoreHammering() { let allDeallocations = NIOAtomic.makeAtomic(value: 0) - let iterations = 100_000 + let iterations = 10_000 @inline(never) func doIt() { @@ -938,7 +958,7 @@ class NIOConcurrencyHelpersTests: XCTestCase { func testMultipleLoadsRacingWhilstStoresAreGoingOn() { // regression test for https://github.com/apple/swift-nio/pull/1287#discussion_r353932225 let allDeallocations = NIOAtomic.makeAtomic(value: 0) - let iterations = 100_000 + let iterations = 10_000 @inline(never) func doIt() { diff --git a/Tests/NIOTests/AcceptBackoffHandlerTest.swift b/Tests/NIOTests/AcceptBackoffHandlerTest.swift index 5b7531ff3..064ea8efc 100644 --- a/Tests/NIOTests/AcceptBackoffHandlerTest.swift +++ b/Tests/NIOTests/AcceptBackoffHandlerTest.swift @@ -48,7 +48,10 @@ public final class AcceptBackoffHandlerTest: XCTestCase { } let readCountHandler = ReadCountHandler() - let serverChannel = try setupChannel(group: group, readCountHandler: readCountHandler, errors: [error]) + let serverChannel = try setupChannel(group: group, + readCountHandler: readCountHandler, + backoffProvider: { _ in return .milliseconds(100) }, + errors: [error]) XCTAssertEqual(0, try serverChannel.eventLoop.submit { serverChannel.readable() serverChannel.read() @@ -56,7 +59,7 @@ public final class AcceptBackoffHandlerTest: XCTestCase { }.wait()) // Inspect the read count after our scheduled backoff elapsed. - XCTAssertEqual(1, try serverChannel.eventLoop.scheduleTask(in: .seconds(1)) { + XCTAssertEqual(1, try serverChannel.eventLoop.scheduleTask(in: .milliseconds(100)) { return readCountHandler.readCount }.futureResult.wait()) @@ -119,7 +122,7 @@ public final class AcceptBackoffHandlerTest: XCTestCase { let readCountHandler = ReadCountHandler() let serverChannel = try setupChannel(group: group, readCountHandler: readCountHandler, backoffProvider: { err in - return .milliseconds(100) + return .milliseconds(10) }, errors: [ENFILE]) XCTAssertEqual(0, try serverChannel.eventLoop.submit { serverChannel.readable() @@ -170,7 +173,7 @@ public final class AcceptBackoffHandlerTest: XCTestCase { let readCountHandler = ReadCountHandler() let serverChannel = try setupChannel(group: group, readCountHandler: readCountHandler, backoffProvider: { err in - return .milliseconds(100) + return .milliseconds(10) }, errors: [ENFILE]) let inactiveVerificationHandler = InactiveVerificationHandler(promise: serverChannel.eventLoop.makePromise()) @@ -245,7 +248,10 @@ public final class AcceptBackoffHandlerTest: XCTestCase { } } - private func setupChannel(group: EventLoopGroup, readCountHandler: ReadCountHandler, backoffProvider: @escaping (IOError) -> TimeAmount? = AcceptBackoffHandler.defaultBackoffProvider, errors: [Int32]) throws -> ServerSocketChannel { + private func setupChannel(group: EventLoopGroup, + readCountHandler: ReadCountHandler, + backoffProvider: @escaping (IOError) -> TimeAmount? = AcceptBackoffHandler.defaultBackoffProvider, + errors: [Int32]) throws -> ServerSocketChannel { let eventLoop = group.next() as! SelectableEventLoop let socket = try NonAcceptingServerSocket(errors: errors) let serverChannel = try assertNoThrowWithValue(ServerSocketChannel(serverSocket: socket, diff --git a/Tests/NIOTests/CircularBufferTests.swift b/Tests/NIOTests/CircularBufferTests.swift index cfbb12e25..006caa986 100644 --- a/Tests/NIOTests/CircularBufferTests.swift +++ b/Tests/NIOTests/CircularBufferTests.swift @@ -42,14 +42,14 @@ class CircularBufferTests: XCTestCase { XCTAssertTrue(ring.isEmpty) XCTAssertEqual(0, ring.count) - for f in 1..<1000 { + for f in 1..<100 { ring.append(f) XCTAssertEqual(f, ring.count) XCTAssertTrue(ring.testOnly_verifyInvariantsForNonSlices()) } - for f in 1..<1000 { + for f in 1..<100 { XCTAssertEqual(f, ring.removeFirst()) - XCTAssertEqual(999 - f, ring.count) + XCTAssertEqual(99 - f, ring.count) XCTAssertTrue(ring.testOnly_verifyInvariantsForNonSlices()) } XCTAssertTrue(ring.isEmpty) diff --git a/Tests/NIOTests/HeapTests.swift b/Tests/NIOTests/HeapTests.swift index 7e77904e2..2c161d4c2 100644 --- a/Tests/NIOTests/HeapTests.swift +++ b/Tests/NIOTests/HeapTests.swift @@ -16,16 +16,9 @@ import XCTest @testable import NIO public func getRandomNumbers(count: Int) -> [UInt8] { - var values: [UInt8] = .init(repeating: 0, count: count) - let fd = open("/dev/urandom", O_RDONLY) - precondition(fd >= 0) - defer { - close(fd) + return (0..(ascending: ascending) let randoms = getRandomNumbers(count: size) randoms.forEach { pq.push($0) }