From f7ef82703f35018c9531806cb9cd3059ada0cb74 Mon Sep 17 00:00:00 2001 From: Fabrizio Brancati Date: Tue, 7 May 2024 09:38:07 +0200 Subject: [PATCH] Use CIHelper to run tests on Linux but not on CI --- .../ConcurrentOperationTests.swift | 54 +++---- Tests/QueuerTests/QueuerTests.swift | 148 +++++++++--------- Tests/QueuerTests/SchedulerTests.swift | 90 ++++++----- Tests/QueuerTests/SemaphoreTests.swift | 62 ++++---- 4 files changed, 181 insertions(+), 173 deletions(-) diff --git a/Tests/QueuerTests/ConcurrentOperationTests.swift b/Tests/QueuerTests/ConcurrentOperationTests.swift index c9fee5a..4ce9864 100644 --- a/Tests/QueuerTests/ConcurrentOperationTests.swift +++ b/Tests/QueuerTests/ConcurrentOperationTests.swift @@ -108,39 +108,39 @@ final class ConcurrentOperationTests: XCTestCase { } } - #if !os(Linux) func testAsyncChainedRetry() async { - let queue = Queuer(name: "ConcurrentOperationTestChainedRetry") - let testExpectation = expectation(description: "Chained Retry") - let order = Order() - - let concurrentOperation1 = ConcurrentOperation { operation in - Task { - try? await Task.sleep(for: .seconds(1)) - await order.append(0) - operation.finish(success: false) + if CIHelper.isRunningOnCI() { + let queue = Queuer(name: "ConcurrentOperationTestChainedRetry") + let testExpectation = expectation(description: "Chained Retry") + let order = OrderHelper() + + let concurrentOperation1 = ConcurrentOperation { operation in + Task { + try? await Task.sleep(for: .seconds(1)) + await order.append(0) + operation.finish(success: false) + } } - } - concurrentOperation1.manualFinish = true - let concurrentOperation2 = ConcurrentOperation { operation in - Task { - await order.append(1) - operation.finish(success: false) + concurrentOperation1.manualFinish = true + let concurrentOperation2 = ConcurrentOperation { operation in + Task { + await order.append(1) + operation.finish(success: false) + } } - } - concurrentOperation2.manualFinish = true - queue.addChainedOperations([concurrentOperation1, concurrentOperation2]) { - Task { - await order.append(2) - testExpectation.fulfill() + concurrentOperation2.manualFinish = true + queue.addChainedOperations([concurrentOperation1, concurrentOperation2]) { + Task { + await order.append(2) + testExpectation.fulfill() + } } - } - await fulfillment(of: [testExpectation], timeout: 10) - let finalOrder = await order.order - XCTAssertEqual(finalOrder, [0, 0, 0, 1, 1, 1, 2]) + await fulfillment(of: [testExpectation], timeout: 10) + let finalOrder = await order.order + XCTAssertEqual(finalOrder, [0, 0, 0, 1, 1, 1, 2]) + } } - #endif func testCanceledChainedRetry() { let queue = Queuer(name: "ConcurrentOperationTestCanceledChainedRetry") diff --git a/Tests/QueuerTests/QueuerTests.swift b/Tests/QueuerTests/QueuerTests.swift index b1ea675..5203344 100644 --- a/Tests/QueuerTests/QueuerTests.swift +++ b/Tests/QueuerTests/QueuerTests.swift @@ -29,43 +29,45 @@ import Queuer import XCTest final class QueuerTests: XCTestCase { - #if !os(Linux) func testOperationCount() { - let queue = Queuer(name: "QueuerTestOperationCount") - let testExpectation = expectation(description: "Operation Count") + if CIHelper.isRunningOnCI() { + let queue = Queuer(name: "QueuerTestOperationCount") + let testExpectation = expectation(description: "Operation Count") - XCTAssertEqual(queue.operationCount, 0) + XCTAssertEqual(queue.operationCount, 0) - let concurrentOperation = ConcurrentOperation { _ in - Thread.sleep(forTimeInterval: 2) - testExpectation.fulfill() - } - concurrentOperation.addToQueue(queue) - XCTAssertEqual(queue.operationCount, 1) + let concurrentOperation = ConcurrentOperation { _ in + Thread.sleep(forTimeInterval: 2) + testExpectation.fulfill() + } + concurrentOperation.addToQueue(queue) + XCTAssertEqual(queue.operationCount, 1) - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) - XCTAssertEqual(queue.operationCount, 0) + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + XCTAssertEqual(queue.operationCount, 0) + } } } func testOperations() { - let queue = Queuer(name: "QueuerTestOperations") - let testExpectation = expectation(description: "Operations") - - let concurrentOperation = ConcurrentOperation { _ in - Thread.sleep(forTimeInterval: 2) - testExpectation.fulfill() - } - queue.addOperation(concurrentOperation) - XCTAssertTrue(queue.operations.contains(concurrentOperation)) - - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) - XCTAssertFalse(queue.operations.contains(concurrentOperation)) + if CIHelper.isRunningOnCI() { + let queue = Queuer(name: "QueuerTestOperations") + let testExpectation = expectation(description: "Operations") + + let concurrentOperation = ConcurrentOperation { _ in + Thread.sleep(forTimeInterval: 2) + testExpectation.fulfill() + } + queue.addOperation(concurrentOperation) + XCTAssertTrue(queue.operations.contains(concurrentOperation)) + + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + XCTAssertFalse(queue.operations.contains(concurrentOperation)) + } } } - #endif func testMaxConcurrentOperationCount() { let queue = Queuer(name: "QueuerTestMaxConcurrentOperationCount") @@ -75,31 +77,31 @@ final class QueuerTests: XCTestCase { XCTAssertEqual(queue.maxConcurrentOperationCount, 10) } - #if !os(Linux) func testMaxConcurrentOperationCountSetToOne() { - let testExpectation = expectation(description: "Max Concurrent Operation Count Set To One") - var testString = "" + if CIHelper.isRunningOnCI() { + let testExpectation = expectation(description: "Max Concurrent Operation Count Set To One") + var testString = "" - let concurrentOperation1 = ConcurrentOperation { _ in - Thread.sleep(forTimeInterval: 3.5) - testString = "Tested1" + let concurrentOperation1 = ConcurrentOperation { _ in + Thread.sleep(forTimeInterval: 3.5) + testString = "Tested1" - testExpectation.fulfill() - } - let concurrentOperation2 = ConcurrentOperation { _ in - testString = "Tested2" - } - Queuer.shared.maxConcurrentOperationCount = 1 - Queuer.shared.addOperation(concurrentOperation1) - Queuer.shared.addOperation(concurrentOperation2) + testExpectation.fulfill() + } + let concurrentOperation2 = ConcurrentOperation { _ in + testString = "Tested2" + } + Queuer.shared.maxConcurrentOperationCount = 1 + Queuer.shared.addOperation(concurrentOperation1) + Queuer.shared.addOperation(concurrentOperation2) - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) - XCTAssertEqual(testString, "Tested2") + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + XCTAssertEqual(testString, "Tested2") + } } } - #endif func testMaxConcurrentOperationCountSetToTwo() { let testExpectation = expectation(description: "Max Concurrent Operation Count Set To Two") @@ -351,38 +353,38 @@ final class QueuerTests: XCTestCase { } } - #if !os(Linux) func testWaitUnitlAllOperationsAreFinished() { - let queue = Queuer(name: "QueuerTestWaitUnitlAllOperationsAreFinished") - let testExpectation = expectation(description: "Wait Unitl All Operations Are Finished") - var order: [Int] = [] - - let concurrentOperation1 = ConcurrentOperation { _ in - Thread.sleep(forTimeInterval: 2) - order.append(0) - } - let concurrentOperation2 = ConcurrentOperation { _ in - order.append(1) - } - queue.addChainedOperations([concurrentOperation1, concurrentOperation2]) { - order.append(2) - } - - queue.waitUntilAllOperationsAreFinished() + if CIHelper.isRunningOnCI() { + let queue = Queuer(name: "QueuerTestWaitUnitlAllOperationsAreFinished") + let testExpectation = expectation(description: "Wait Unitl All Operations Are Finished") + var order: [Int] = [] + + let concurrentOperation1 = ConcurrentOperation { _ in + Thread.sleep(forTimeInterval: 2) + order.append(0) + } + let concurrentOperation2 = ConcurrentOperation { _ in + order.append(1) + } + queue.addChainedOperations([concurrentOperation1, concurrentOperation2]) { + order.append(2) + } + + queue.waitUntilAllOperationsAreFinished() - XCTAssertEqual(order, [0, 1, 2]) - XCTAssertLessThanOrEqual(queue.operationCount, 3) - testExpectation.fulfill() - - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) - XCTAssertTrue(queue.isExecuting) - XCTAssertLessThanOrEqual(queue.operationCount, 3) XCTAssertEqual(order, [0, 1, 2]) + XCTAssertLessThanOrEqual(queue.operationCount, 3) + testExpectation.fulfill() - queue.resume() - XCTAssertTrue(queue.isExecuting) + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + XCTAssertTrue(queue.isExecuting) + XCTAssertLessThanOrEqual(queue.operationCount, 3) + XCTAssertEqual(order, [0, 1, 2]) + + queue.resume() + XCTAssertTrue(queue.isExecuting) + } } } - #endif } diff --git a/Tests/QueuerTests/SchedulerTests.swift b/Tests/QueuerTests/SchedulerTests.swift index 83d7774..ee4bf0b 100644 --- a/Tests/QueuerTests/SchedulerTests.swift +++ b/Tests/QueuerTests/SchedulerTests.swift @@ -29,64 +29,68 @@ import Queuer import XCTest final class SchedulerTests: XCTestCase { - #if !os(Linux) func testInitWithoutHandler() { - let testExpectation = expectation(description: "Init Without Handler") - var order: [Int] = [] + if CIHelper.isRunningOnCI() { + let testExpectation = expectation(description: "Init Without Handler") + var order: [Int] = [] - var schedule = Scheduler(deadline: .now(), repeating: .seconds(1)) - schedule.setHandler { - order.append(0) - } + var schedule = Scheduler(deadline: .now(), repeating: .seconds(1)) + schedule.setHandler { + order.append(0) + } - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(3500)) { - testExpectation.fulfill() - } + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(3500)) { + testExpectation.fulfill() + } - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) - XCTAssertEqual(order, [0, 0, 0, 0]) - schedule.timer.cancel() + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + XCTAssertEqual(order, [0, 0, 0, 0]) + schedule.timer.cancel() + } } } func testInitWithHandler() { - let testExpectation = expectation(description: "Init With Handler") - var order: [Int] = [] - - let schedule = Scheduler(deadline: .now(), repeating: .never) { - order.append(0) - } - - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(3500)) { - testExpectation.fulfill() - } - - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) - XCTAssertEqual(order, [0]) - schedule.timer.cancel() + if CIHelper.isRunningOnCI() { + let testExpectation = expectation(description: "Init With Handler") + var order: [Int] = [] + + let schedule = Scheduler(deadline: .now(), repeating: .never) { + order.append(0) + } + + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(3500)) { + testExpectation.fulfill() + } + + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + XCTAssertEqual(order, [0]) + schedule.timer.cancel() + } } } func testCancel() { - let testExpectation = expectation(description: "Init Without Handler") - var order: [Int] = [] + if CIHelper.isRunningOnCI() { + let testExpectation = expectation(description: "Init Without Handler") + var order: [Int] = [] - var schedule = Scheduler(deadline: .now(), repeating: .seconds(1)) - schedule.setHandler { - order.append(0) - schedule.timer.cancel() - } + var schedule = Scheduler(deadline: .now(), repeating: .seconds(1)) + schedule.setHandler { + order.append(0) + schedule.timer.cancel() + } - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(3500)) { - testExpectation.fulfill() - } + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(3500)) { + testExpectation.fulfill() + } - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) - XCTAssertEqual(order, [0]) + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + XCTAssertEqual(order, [0]) + } } } - #endif } diff --git a/Tests/QueuerTests/SemaphoreTests.swift b/Tests/QueuerTests/SemaphoreTests.swift index 1ecb373..e6db2da 100644 --- a/Tests/QueuerTests/SemaphoreTests.swift +++ b/Tests/QueuerTests/SemaphoreTests.swift @@ -28,47 +28,49 @@ import Queuer import XCTest final class SemaphoreTests: XCTestCase { - #if !os(Linux) func testWithSemaphore() { - let semaphore = Semaphore() - let queue = Queuer(name: "SemaphoreTestWithSemaphore") - let testExpectation = expectation(description: "With Semaphore") - var testString = "" + if CIHelper.isRunningOnCI() { + let semaphore = Semaphore() + let queue = Queuer(name: "SemaphoreTestWithSemaphore") + let testExpectation = expectation(description: "With Semaphore") + var testString = "" - let concurrentOperation = ConcurrentOperation { _ in - Thread.sleep(forTimeInterval: 2) - testString = "Tested" - semaphore.continue() - } - concurrentOperation.addToQueue(queue) + let concurrentOperation = ConcurrentOperation { _ in + Thread.sleep(forTimeInterval: 2) + testString = "Tested" + semaphore.continue() + } + concurrentOperation.addToQueue(queue) - semaphore.wait() - XCTAssertEqual(testString, "Tested") - testExpectation.fulfill() + semaphore.wait() + XCTAssertEqual(testString, "Tested") + testExpectation.fulfill() - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + } } } func testWithoutSemaphore() { - let queue = Queuer(name: "SemaphoreTestWithoutSemaphore") - let testExpectation = expectation(description: "Without Semaphore") - var testString = "" + if CIHelper.isRunningOnCI() { + let queue = Queuer(name: "SemaphoreTestWithoutSemaphore") + let testExpectation = expectation(description: "Without Semaphore") + var testString = "" - let concurrentOperation = ConcurrentOperation { _ in - Thread.sleep(forTimeInterval: 2) - testString = "Tested" - testExpectation.fulfill() - } - concurrentOperation.addToQueue(queue) + let concurrentOperation = ConcurrentOperation { _ in + Thread.sleep(forTimeInterval: 2) + testString = "Tested" + testExpectation.fulfill() + } + concurrentOperation.addToQueue(queue) - XCTAssertEqual(testString, "") + XCTAssertEqual(testString, "") - waitForExpectations(timeout: 5) { error in - XCTAssertNil(error) - XCTAssertEqual(testString, "Tested") + waitForExpectations(timeout: 5) { error in + XCTAssertNil(error) + XCTAssertEqual(testString, "Tested") + } } } - #endif }