Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/MockingKit/Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import Foundation
///
/// Inherit this type instead of implementing the ``Mockable``
/// protocol, to save some code for every mock you create.
open class Mock: Mockable {
open class Mock: Mockable, @unchecked Sendable {

public init() {}

public var mock: Mock { self }

var registeredCalls: [UUID: [AnyCall]] = [:]
var registeredResults: [UUID: Function] = [:]
let registeredCallsLock = NSLock()
}
36 changes: 25 additions & 11 deletions Sources/MockingKit/Mockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Foundation
///
/// Implement this protocol instead of inheriting the ``Mock``
/// base class, to save some code for every mock you create.
public protocol Mockable {
public protocol Mockable: Sendable {

typealias Function = Any

Expand All @@ -38,41 +38,55 @@ extension Mockable {
_ call: MockCall<Arguments, Result>,
for ref: MockReference<Arguments, Result>
) {
let calls = mock.registeredCalls[ref.id] ?? []
mock.registeredCalls[ref.id] = calls + [call]
mock.registeredCallsLock.withLock {
let calls = mock.registeredCalls[ref.id] ?? []
mock.registeredCalls[ref.id] = calls + [call]
}
}

func registerCall<Arguments, Result>(
_ call: MockCall<Arguments, Result>,
for ref: AsyncMockReference<Arguments, Result>
) {
let calls = mock.registeredCalls[ref.id] ?? []
mock.registeredCalls[ref.id] = calls + [call]
mock.registeredCallsLock.withLock {
let calls = mock.registeredCalls[ref.id] ?? []
mock.registeredCalls[ref.id] = calls + [call]
}
}

func registeredCalls<Arguments, Result>(
for ref: MockReference<Arguments, Result>
) -> [MockCall<Arguments, Result>] {
let calls = mock.registeredCalls[ref.id]
return (calls as? [MockCall<Arguments, Result>]) ?? []
mock.registeredCallsLock.withLock {
let calls = mock.registeredCalls[ref.id]
return (calls as? [MockCall<Arguments, Result>]) ?? []
}
}

func registeredCalls<Arguments, Result>(
for ref: AsyncMockReference<Arguments, Result>
) -> [MockCall<Arguments, Result>] {
let calls = mock.registeredCalls[ref.id]
return (calls as? [MockCall<Arguments, Result>]) ?? []
mock.registeredCallsLock.withLock {
let calls = mock.registeredCalls[ref.id]
return (calls as? [MockCall<Arguments, Result>]) ?? []
}
}

func registeredResult<Arguments, Result>(
for ref: MockReference<Arguments, Result>
) -> ((Arguments) throws -> Result)? {
mock.registeredResults[ref.id] as? (Arguments) throws -> Result
mock.registeredCallsLock.withLock {
let result = mock.registeredResults[ref.id] as? (Arguments) throws -> Result
return result
}
}

func registeredResult<Arguments, Result>(
for ref: AsyncMockReference<Arguments, Result>
) -> ((Arguments) async throws -> Result)? {
mock.registeredResults[ref.id] as? (Arguments) async throws -> Result
mock.registeredCallsLock.withLock {
let result = mock.registeredResults[ref.id] as? (Arguments) async throws -> Result
return result
}
}
}
2 changes: 1 addition & 1 deletion Sources/MockingKit/Mocks/MockPasteboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import AppKit
This mock only mocks `setValue(_:forKey:)` for now, but you
can subclass this class and mock more functionality.
*/
public class MockPasteboard: NSPasteboard, Mockable {
public class MockPasteboard: NSPasteboard, Mockable, @unchecked Sendable {

public lazy var setValueForKeyRef = MockReference(setValueForKey)

Expand Down
2 changes: 1 addition & 1 deletion Sources/MockingKit/Mocks/MockUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// This class can be used to mock `UserDefaults`.
open class MockUserDefaults: UserDefaults, Mockable {
open class MockUserDefaults: UserDefaults, Mockable, @unchecked Sendable {

public lazy var boolRef = MockReference(bool)
public lazy var arrayRef = MockReference(array)
Expand Down
2 changes: 1 addition & 1 deletion Tests/MockingKitTests/GenericTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class GenericTests: XCTestCase {
}
}

private class GenericMock<T>: Mock {
private class GenericMock<T>: Mock, @unchecked Sendable {

lazy var doitRef = MockReference(doit)

Expand Down
26 changes: 24 additions & 2 deletions Tests/MockingKitTests/MockableAsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,33 @@ class MockableAsyncTests: XCTestCase {
XCTAssertFalse(mock.hasCalled(mock.functionWithIntResultRef))
XCTAssertTrue(mock.hasCalled(\.functionWithStringResultRef))
}

func testMultiThreadedAccess_doesNotCorruptState() async throws {
let expectation = XCTestExpectation()
expectation.expectedFulfillmentCount = 2
let mock = TestClass()

Task {
for index in 0..<100 {
await mock.functionWithVoidResult(arg1: "Test", arg2: index)
}
expectation.fulfill()
}

Task {
for _ in 0..<100 {
_ = mock.hasCalled(\.functionWithIntResultRef)
}
expectation.fulfill()
}

await fulfillment(of: [expectation])
}
}

private class TestClass: AsyncTestProtocol, Mockable {
private final class TestClass: AsyncTestProtocol, Mockable, @unchecked Sendable {

var mock = Mock()
let mock = Mock()

lazy var functionWithIntResultRef = AsyncMockReference(functionWithIntResult)
lazy var functionWithStringResultRef = AsyncMockReference(functionWithStringResult)
Expand Down
23 changes: 21 additions & 2 deletions Tests/MockingKitTests/MockableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,30 @@ class MockableTests: XCTestCase {
XCTAssertFalse(mock.hasCalled(mock.functionWithIntResultRef))
XCTAssertTrue(mock.hasCalled(\.functionWithStringResultRef))
}

func testMultiThreadedAccess_doesNotCorruptState() {
let queueA = DispatchQueue(label: "QueueA")
let queueB = DispatchQueue(label: "QueueB")

let mock = TestClass()

queueA.async {
for index in 0..<100 {
mock.functionWithVoidResult(arg1: "Something", arg2: index)
}
}

queueB.async {
for _ in 0..<100 {
_ = mock.hasCalled(\.functionWithIntResultRef)
}
}
}
}

private class TestClass: AsyncTestProtocol, Mockable {
private final class TestClass: AsyncTestProtocol, Mockable, @unchecked Sendable {

var mock = Mock()
let mock = Mock()

lazy var functionWithIntResultRef = MockReference(functionWithIntResult)
lazy var functionWithStringResultRef = MockReference(functionWithStringResult)
Expand Down