mirror of
https://github.com/apple/swift-nio
synced 2026-11-26 04:57:49 +00:00
fix two tests threading issues (#513)
Motivation: testScheduleRepeatedTask and testScheduleRepeatedTaskCancelFromDifferentThread has threading issues that I unfortunately missed in review. Modifications: fix the threading issues by using a `DispatchGroup` Result: fewer theading issues in the tests
This commit is contained in:
1 parent
6bf4f19ffe
commit
ba077d3be8
1 file changed
+23
-9
@@ -103,16 +103,28 @@ public class EventLoopTest : XCTestCase {
|
||||
|
||||
let expect = expectation(description: "Is cancelling RepatedTask")
|
||||
let counter = Atomic<Int>(value: 0)
|
||||
var repeatedTask: RepeatedTask?
|
||||
repeatedTask = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { () -> Void in
|
||||
let lock = Lock()
|
||||
var repeatedTask: RepeatedTask? = nil // protected by `lock`
|
||||
let task = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { () -> Void in
|
||||
if counter.load() == 0 {
|
||||
XCTAssertTrue(DispatchTime.now().uptimeNanoseconds - nanos >= initialDelay.nanoseconds)
|
||||
} else if counter.load() == count {
|
||||
expect.fulfill()
|
||||
repeatedTask?.cancel()
|
||||
while true {
|
||||
let mTask = lock.withLock { repeatedTask }
|
||||
guard let task = mTask else {
|
||||
usleep(10_000)
|
||||
continue
|
||||
}
|
||||
task.cancel()
|
||||
break
|
||||
}
|
||||
}
|
||||
counter.store(counter.load() + 1)
|
||||
}
|
||||
lock.withLock {
|
||||
repeatedTask = task
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1) { _ in
|
||||
XCTAssertEqual(counter.load(), count + 1)
|
||||
@@ -130,12 +142,14 @@ public class EventLoopTest : XCTestCase {
|
||||
}
|
||||
|
||||
let expect = expectation(description: "Is cancelling RepatedTask")
|
||||
var repeatedTask: RepeatedTask?
|
||||
repeatedTask = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { () -> Void in
|
||||
DispatchQueue(label: "nio.repeatedtask.test").async {
|
||||
repeatedTask?.cancel()
|
||||
expect.fulfill()
|
||||
}
|
||||
let group = DispatchGroup()
|
||||
group.enter()
|
||||
let repeatedTask = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { () -> Void in
|
||||
group.leave()
|
||||
expect.fulfill()
|
||||
}
|
||||
group.notify(queue: DispatchQueue.global()) {
|
||||
repeatedTask.cancel()
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1) { _ in
|
||||
|
||||
Reference in new issue
Block a user