Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes an issue where asking a mock for values in a concurrent way causes a crash in the VerifyContainer #69

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 9 additions & 6 deletions Sources/SwiftMock/Verify/VerifyContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ public class VerifyContainer: CallContainer {
var calls: [Any] = []
var functions: [String] = []
var isVerified: [Bool] = []

private let queue = DispatchQueue(label: "VerifyContainerQueue")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix indentation, please


public init() { }

public func append<T>(mock: AnyObject, call: MethodCall<T>, function: String) {
calls.append(call)
functions.append(function)
isVerified.append(false)

InOrderContainer.append(mock: mock, call: call, function: function)
queue.sync { [weak self] in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda wierd. We call closure synchronous on the queue, but capture self in a weak manner.
Just use unowned reference

self?.calls.append(call)
self?.functions.append(function)
self?.isVerified.append(false)

InOrderContainer.append(mock: mock, call: call, function: function)
}
}

public func verify<T>(
Expand Down
22 changes: 11 additions & 11 deletions Tests/SwiftMockTests/Macro/MockMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ private let testMacros: [String: Macro.Type] = [
#endif

final class MockMacroTests: XCTestCase {
func testEmptyProtocol() {
func testEmptyProtocol() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -46,7 +46,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testInternalProtocol() {
func testInternalProtocol() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -79,7 +79,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testFunctionWithoutArgumentsAndReturnType() {
func testFunctionWithoutArgumentsAndReturnType() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -136,7 +136,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testGetProperty() {
func testGetProperty() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -192,7 +192,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testGetSetProperty() {
func testGetSetProperty() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -432,7 +432,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testFunctionWithoutArguments() {
func testFunctionWithoutArguments() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -489,7 +489,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testFunctionWitOneArgument() {
func testFunctionWitOneArgument() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -546,7 +546,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testFunctionWithTwoArgument() {
func testFunctionWithTwoArgument() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -605,7 +605,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testFunctionThatThrows() {
func testFunctionThatThrows() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -664,7 +664,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testAsyncFunction() {
func testAsyncFunction() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -723,7 +723,7 @@ final class MockMacroTests: XCTestCase {
#endif
}

func testAsyncThrowsFunction() {
func testAsyncThrowsFunction() throws {
#if canImport(SwiftMockMacros)
assertMacroExpansion(
"""
Expand Down
24 changes: 23 additions & 1 deletion Tests/SwiftMockTests/MethodStubbingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,29 @@ final class MethodStubbingTests: XCTestCase {
}
XCTAssertNil(catchedError)
}


func testConcurrentMockAccess() throws {
let mock = SimpleProtocolMock()
let expectation = -1
when(mock.$call()).thenReturn(expectation)

let xcTestExpectation = XCTestExpectation()
xcTestExpectation.expectedFulfillmentCount = 400
DispatchQueue.concurrentPerform(iterations: 200) { number in
let actual = mock.call()
XCTAssertEqual(expectation, actual)
xcTestExpectation.fulfill()
}

DispatchQueue.concurrentPerform(iterations: 200) { number in
let actual = mock.call()
XCTAssertEqual(expectation, actual)
xcTestExpectation.fulfill()
}

wait(for: [xcTestExpectation])
}

func testGenericWithOneParameter() {
let mock = GenericMethodProtocolMock()

Expand Down