Skip to content

Commit

Permalink
Fix the current set of concurrency warnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
younata committed Oct 14, 2024
1 parent b4b2dad commit 7ccad44
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 228 deletions.
80 changes: 40 additions & 40 deletions Tests/NimbleTests/AsyncAwaitTest+Require.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if !os(WASI)

import XCTest
import Nimble
@testable import Nimble
#if SWIFT_PACKAGE
import NimbleSharedTestHelpers
#endif
Expand All @@ -24,12 +24,12 @@ final class AsyncAwaitRequireTest: XCTestCase { // swiftlint:disable:this type_b
}

func testToEventuallyPositiveMatches() async throws {
var value = 0
deferToMainQueue { value = 1 }
try await require { value }.toEventually(equal(1))
let value = LockedContainer(0)
deferToMainQueue { value.set(1) }
try await require { value.value }.toEventually(equal(1))

deferToMainQueue { value = 0 }
try await require { value }.toEventuallyNot(equal(1))
deferToMainQueue { value.set(0) }
try await require { value.value }.toEventuallyNot(equal(1))
}

func testToEventuallyNegativeMatches() async {
Expand Down Expand Up @@ -194,52 +194,52 @@ final class AsyncAwaitRequireTest: XCTestCase { // swiftlint:disable:this type_b
}

final class ClassUnderTest {
var deinitCalled: (() -> Void)?
var count = 0
deinit { deinitCalled?() }
let deinitCalled = LockedContainer<(() -> Void)?>(nil)
let count = LockedContainer(0)
deinit { deinitCalled.value?() }
}

func testSubjectUnderTestIsReleasedFromMemory() async throws {
var subject: ClassUnderTest? = ClassUnderTest()
let subject = LockedContainer<ClassUnderTest?>(ClassUnderTest())

if let sub = subject {
try await require(sub.count).toEventually(equal(0), timeout: .milliseconds(100))
try await require(sub.count).toEventuallyNot(equal(1), timeout: .milliseconds(100))
if let sub = subject.value {
try await require(sub.count.value).toEventually(equal(0), timeout: .milliseconds(100))
try await require(sub.count.value).toEventuallyNot(equal(1), timeout: .milliseconds(100))
}

await waitUntil(timeout: .milliseconds(500)) { done in
subject?.deinitCalled = {
subject.value?.deinitCalled.set({
done()
}
})

deferToMainQueue { subject = nil }
deferToMainQueue { subject.set(nil) }
}
}

func testToNeverPositiveMatches() async throws {
var value = 0
deferToMainQueue { value = 1 }
try await require { value }.toNever(beGreaterThan(1))
let value = LockedContainer(0)
deferToMainQueue { value.set(1) }
try await require { value.value }.toNever(beGreaterThan(1))

deferToMainQueue { value = 0 }
try await require { value }.neverTo(beGreaterThan(1))
deferToMainQueue { value.set(0) }
try await require { value.value }.neverTo(beGreaterThan(1))
}

func testToNeverNegativeMatches() async {
var value = 0
let value = LockedContainer(0)
await failsWithErrorMessage("expected to never equal <0>, got <0>") {
try await require { value }.toNever(equal(0))
try await require { value.value }.toNever(equal(0))
}
await failsWithErrorMessage("expected to never equal <0>, got <0>") {
try await require { value }.neverTo(equal(0))
try await require { value.value }.neverTo(equal(0))
}
await failsWithErrorMessage("expected to never equal <1>, got <1>") {
deferToMainQueue { value = 1 }
try await require { value }.toNever(equal(1))
deferToMainQueue { value.set(1) }
try await require { value.value }.toNever(equal(1))
}
await failsWithErrorMessage("expected to never equal <1>, got <1>") {
deferToMainQueue { value = 1 }
try await require { value }.neverTo(equal(1))
deferToMainQueue { value.set(1) }
try await require { value.value }.neverTo(equal(1))
}
await failsWithErrorMessage("unexpected error thrown: <\(Self.errorToThrow)>") {
try await require { try Self.doThrowError() }.toNever(equal(0))
Expand All @@ -253,29 +253,29 @@ final class AsyncAwaitRequireTest: XCTestCase { // swiftlint:disable:this type_b
}

func testToAlwaysPositiveMatches() async throws {
var value = 1
deferToMainQueue { value = 2 }
try await require { value }.toAlways(beGreaterThan(0))
let value = LockedContainer(1)
deferToMainQueue { value.set(2) }
try await require { value.value }.toAlways(beGreaterThan(0))

deferToMainQueue { value = 2 }
try await require { value }.alwaysTo(beGreaterThan(1))
deferToMainQueue { value.set(2) }
try await require { value.value }.alwaysTo(beGreaterThan(1))
}

func testToAlwaysNegativeMatches() async {
var value = 1
let value = LockedContainer(1)
await failsWithErrorMessage("expected to always equal <0>, got <1>") {
try await require { value }.toAlways(equal(0))
try await require { value.value }.toAlways(equal(0))
}
await failsWithErrorMessage("expected to always equal <0>, got <1>") {
try await require { value }.alwaysTo(equal(0))
try await require { value.value }.alwaysTo(equal(0))
}
await failsWithErrorMessage("expected to always equal <1>, got <0>") {
deferToMainQueue { value = 0 }
try await require { value }.toAlways(equal(1))
deferToMainQueue { value.set(0) }
try await require { value.value }.toAlways(equal(1))
}
await failsWithErrorMessage("expected to always equal <1>, got <0>") {
deferToMainQueue { value = 0 }
try await require { value }.alwaysTo(equal(1))
deferToMainQueue { value.set(0) }
try await require { value.value }.alwaysTo(equal(1))
}
await failsWithErrorMessage("unexpected error thrown: <\(Self.errorToThrow)>") {
try await require { try Self.doThrowError() }.toAlways(equal(0))
Expand Down
88 changes: 44 additions & 44 deletions Tests/NimbleTests/AsyncAwaitTest.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if !os(WASI)

import XCTest
import Nimble
@testable import Nimble
#if SWIFT_PACKAGE
import NimbleSharedTestHelpers
#endif
Expand All @@ -24,12 +24,12 @@ final class AsyncAwaitTest: XCTestCase { // swiftlint:disable:this type_body_len
}

func testToEventuallyPositiveMatches() async {
var value = 0
deferToMainQueue { value = 1 }
await expect { value }.toEventually(equal(1))
let value = LockedContainer(0)
deferToMainQueue { value.set(1) }
await expect { value.value }.toEventually(equal(1))

deferToMainQueue { value = 0 }
await expect { value }.toEventuallyNot(equal(1))
deferToMainQueue { value.set(0) }
await expect { value.value }.toEventuallyNot(equal(1))
}

func testToEventuallyNegativeMatches() async {
Expand Down Expand Up @@ -145,23 +145,23 @@ final class AsyncAwaitTest: XCTestCase { // swiftlint:disable:this type_body_len
PollingDefaults.timeout = .seconds(1)
}

var value = 0
let value = LockedContainer(0)

let sleepThenSetValueTo: (Int) -> Void = { newValue in
Thread.sleep(forTimeInterval: 1.1)
value = newValue
value.set(newValue)
}

let task = Task {
sleepThenSetValueTo(1)
}
await expect { value }.toEventually(equal(1))
await expect { value.value }.toEventually(equal(1))

let secondTask = Task {
sleepThenSetValueTo(0)
}

await expect { value }.toEventuallyNot(equal(1))
await expect { value.value }.toEventuallyNot(equal(1))

_ = await task.value
_ = await secondTask.result
Expand Down Expand Up @@ -286,52 +286,52 @@ final class AsyncAwaitTest: XCTestCase { // swiftlint:disable:this type_body_len
}

final class ClassUnderTest {
var deinitCalled: (() -> Void)?
var count = 0
deinit { deinitCalled?() }
let deinitCalled = LockedContainer<(() -> Void)?>(nil)
let count = LockedContainer(0)
deinit { deinitCalled.value?() }
}

func testSubjectUnderTestIsReleasedFromMemory() async {
var subject: ClassUnderTest? = ClassUnderTest()
let subject = LockedContainer<ClassUnderTest?>(ClassUnderTest())

if let sub = subject {
await expect(sub.count).toEventually(equal(0), timeout: .milliseconds(100))
await expect(sub.count).toEventuallyNot(equal(1), timeout: .milliseconds(100))
if let sub = subject.value {
await expect(sub.count.value).toEventually(equal(0), timeout: .milliseconds(100))
await expect(sub.count.value).toEventuallyNot(equal(1), timeout: .milliseconds(100))
}

await waitUntil(timeout: .milliseconds(500)) { done in
subject?.deinitCalled = {
subject.value?.deinitCalled.set({
done()
}
})

deferToMainQueue { subject = nil }
deferToMainQueue { subject.set(nil) }
}
}

func testToNeverPositiveMatches() async {
var value = 0
deferToMainQueue { value = 1 }
await expect { value }.toNever(beGreaterThan(1))
let value = LockedContainer(0)
deferToMainQueue { value.set(1) }
await expect { value.value }.toNever(beGreaterThan(1))

deferToMainQueue { value = 0 }
await expect { value }.neverTo(beGreaterThan(1))
deferToMainQueue { value.set(0) }
await expect { value.value }.neverTo(beGreaterThan(1))
}

func testToNeverNegativeMatches() async {
var value = 0
let value = LockedContainer(0)
await failsWithErrorMessage("expected to never equal <0>, got <0>") {
await expect { value }.toNever(equal(0))
await expect { value.value }.toNever(equal(0))
}
await failsWithErrorMessage("expected to never equal <0>, got <0>") {
await expect { value }.neverTo(equal(0))
await expect { value.value }.neverTo(equal(0))
}
await failsWithErrorMessage("expected to never equal <1>, got <1>") {
deferToMainQueue { value = 1 }
await expect { value }.toNever(equal(1))
deferToMainQueue { value.set(1) }
await expect { value.value }.toNever(equal(1))
}
await failsWithErrorMessage("expected to never equal <1>, got <1>") {
deferToMainQueue { value = 1 }
await expect { value }.neverTo(equal(1))
deferToMainQueue { value.set(1) }
await expect { value.value }.neverTo(equal(1))
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
await expect { try self.doThrowError() }.toNever(equal(0))
Expand All @@ -342,29 +342,29 @@ final class AsyncAwaitTest: XCTestCase { // swiftlint:disable:this type_body_len
}

func testToAlwaysPositiveMatches() async {
var value = 1
deferToMainQueue { value = 2 }
await expect { value }.toAlways(beGreaterThan(0))
let value = LockedContainer(1)
deferToMainQueue { value.set(2) }
await expect { value.value }.toAlways(beGreaterThan(0))

deferToMainQueue { value = 2 }
await expect { value }.alwaysTo(beGreaterThan(1))
deferToMainQueue { value.set(2) }
await expect { value.value }.alwaysTo(beGreaterThan(1))
}

func testToAlwaysNegativeMatches() async {
var value = 1
let value = LockedContainer(1)
await failsWithErrorMessage("expected to always equal <0>, got <1>") {
await expect { value }.toAlways(equal(0))
await expect { value.value }.toAlways(equal(0))
}
await failsWithErrorMessage("expected to always equal <0>, got <1>") {
await expect { value }.alwaysTo(equal(0))
await expect { value.value }.alwaysTo(equal(0))
}
await failsWithErrorMessage("expected to always equal <1>, got <0>") {
deferToMainQueue { value = 0 }
await expect { value }.toAlways(equal(1))
deferToMainQueue { value.set(0) }
await expect { value.value }.toAlways(equal(1))
}
await failsWithErrorMessage("expected to always equal <1>, got <0>") {
deferToMainQueue { value = 0 }
await expect { value }.alwaysTo(equal(1))
deferToMainQueue { value.set(0) }
await expect { value.value }.alwaysTo(equal(1))
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
await expect { try self.doThrowError() }.toAlways(equal(0))
Expand Down
12 changes: 8 additions & 4 deletions Tests/NimbleTests/Matchers/ContainElementSatisfyingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import NimbleSharedTestHelpers
final class ContainElementSatisfyingTest: XCTestCase {
// MARK: - Matcher variant
func testContainElementSatisfying() {
var orderIndifferentArray = [1, 2, 3]
let orderIndifferentArray = [1, 2, 3]
expect(orderIndifferentArray).to(containElementSatisfying({ number in
return number == 1
}))
Expand All @@ -18,8 +18,10 @@ final class ContainElementSatisfyingTest: XCTestCase {
expect(orderIndifferentArray).to(containElementSatisfying({ number in
return number == 3
}))
}

orderIndifferentArray = [3, 1, 2]
func testContainElementSatisfying2() {
let orderIndifferentArray = [3, 1, 2]
expect(orderIndifferentArray).to(containElementSatisfying({ number in
return number == 1
}))
Expand Down Expand Up @@ -76,7 +78,7 @@ final class ContainElementSatisfyingTest: XCTestCase {

// MARK: - AsyncMatcher variant
func testAsyncContainElementSatisfying() async {
var orderIndifferentArray = [1, 2, 3]
let orderIndifferentArray = [1, 2, 3]
await expect(orderIndifferentArray).to(containElementSatisfying({ number in
await asyncEqualityCheck(number, 1)
}))
Expand All @@ -86,8 +88,10 @@ final class ContainElementSatisfyingTest: XCTestCase {
await expect(orderIndifferentArray).to(containElementSatisfying({ number in
await asyncEqualityCheck(number, 3)
}))
}

orderIndifferentArray = [3, 1, 2]
func testAsyncContainElementSatisfying2() async {
let orderIndifferentArray = [3, 1, 2]
await expect(orderIndifferentArray).to(containElementSatisfying({ number in
await asyncEqualityCheck(number, 1)
}))
Expand Down
9 changes: 4 additions & 5 deletions Tests/NimbleTests/Matchers/ContainTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ final class ContainTest: XCTestCase {
}

func testContainNSStringSubstring() {
let str = "foo" as NSString
expect(str).to(contain("o" as NSString))
expect(str).to(contain("oo" as NSString))
expect(str).toNot(contain("z" as NSString))
expect(str).toNot(contain("zz" as NSString))
expect("foo" as NSString).to(contain("o" as NSString))
expect("foo" as NSString).to(contain("oo" as NSString))
expect("foo" as NSString).toNot(contain("z" as NSString))
expect("foo" as NSString).toNot(contain("zz" as NSString))
}

func testVariadicArguments() {
Expand Down
6 changes: 3 additions & 3 deletions Tests/NimbleTests/Matchers/MapTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class MapTest: XCTestCase {
}

func testMapWithAsyncFunction() async {
func someOperation(_ value: Int) async -> String {
@Sendable func someOperation(_ value: Int) async -> String {
"\(value)"
}
await expect(1).to(map(someOperation, equal("1")))
Expand All @@ -76,8 +76,8 @@ final class MapTest: XCTestCase {
let box = Box(int: 3, string: "world")

expect(box).to(satisfyAllOf(
map(\.int, equal(3)),
map(\.string, equal("world"))
map( { $0.int }, equal(3)),
map( { $0.string }, equal("world"))
))
}
}
Loading

0 comments on commit 7ccad44

Please sign in to comment.