mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
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.
This commit is contained in:
1 parent
7f8e96cf02
commit
9f9976adae
5 files changed
+44
-25
No files matched your search
@@ -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<UInt64>(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..<noCounts {
|
||||
_ = ai.add(thread)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _ in 0..<noAsyncs {
|
||||
everybodyHere.wait()
|
||||
}
|
||||
for _ in 0..<noAsyncs {
|
||||
go.signal()
|
||||
}
|
||||
g.wait()
|
||||
XCTAssertEqual(sumOfIntegers(until: noAsyncs) * noCounts, ai.load())
|
||||
}
|
||||
@@ -230,19 +240,29 @@ class NIOConcurrencyHelpersTests: XCTestCase {
|
||||
}
|
||||
|
||||
func testLargeContendedNIOAtomicSum() {
|
||||
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.NIOAtomic<UInt64>.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..<noCounts {
|
||||
_ = ai.add(thread)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _ in 0..<noAsyncs {
|
||||
everybodyHere.wait()
|
||||
}
|
||||
for _ in 0..<noAsyncs {
|
||||
go.signal()
|
||||
}
|
||||
g.wait()
|
||||
XCTAssertEqual(sumOfIntegers(until: noAsyncs) * noCounts, ai.load())
|
||||
}
|
||||
@@ -835,7 +855,7 @@ class NIOConcurrencyHelpersTests: XCTestCase {
|
||||
@available(*, deprecated, message: "AtomicBox is deprecated, this is a test for the deprecated functionality")
|
||||
func testLoadAndExchangeHammering() {
|
||||
let allDeallocations = NIOAtomic<Int>.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<Int>.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<Int>.makeAtomic(value: 0)
|
||||
let iterations = 100_000
|
||||
let iterations = 10_000
|
||||
|
||||
@inline(never)
|
||||
func doIt() {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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..<count).map { _ in
|
||||
UInt8.random(in: .min ... .max)
|
||||
}
|
||||
_ = values.withUnsafeMutableBytes { ptr in
|
||||
read(fd, ptr.baseAddress!, ptr.count)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
class HeapTests: XCTestCase {
|
||||
|
||||
@@ -78,7 +78,7 @@ class PriorityQueueTest: XCTestCase {
|
||||
|
||||
func testBuildAndRemoveFromRandomPriorityQueues() {
|
||||
for ascending in [true, false] {
|
||||
for size in 0...50 {
|
||||
for size in 0...33 {
|
||||
var pq = PriorityQueue<UInt8>(ascending: ascending)
|
||||
let randoms = getRandomNumbers(count: size)
|
||||
randoms.forEach { pq.push($0) }
|
||||
|
||||
Reference in new issue
Block a user