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

Concurrency safe handler #276

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

sidepelican
Copy link
Collaborator

Issue: #249

When the mock protocol requires Sendable, correctly implement the mock functions as Sendable. To provide a concurrency-safe implementation, we also add some helper functions and types.

This PR does not address stored properties or computed properties (Do it later).

As shown below, various function Handlers are required to be Sendable.

/// @mockable
protocol APIClient: Sendable {
    func fetchData(_ id: AnyObject) async -> Int
}
final class APIClientMock: APIClient {
    init() { }

    private let fetchDataState = MockoloMutex(MockoloHandlerState<AnyObject, @Sendable (AnyObject) async -> Int>())
    var fetchDataCallCount: Int {
        return fetchDataState.withLock(\.callCount)
    }
    var fetchDataArgValues: [AnyObject] {
        return fetchDataState.withLock(\.argValues).map(\.value)
    }
    var fetchDataHandler: (@Sendable (AnyObject) async -> (Int))? {
        get { fetchDataState.withLock(\.handler) }
        set { fetchDataState.withLock { $0.handler = newValue } }
    }
    func fetchData(_ id: AnyObject) async -> Int {
        warnIfNotSendable(id)
        let handler = fetchDataState.withLock { state in
            state.callCount += 1
            state.argValues.append(.init(id))
            return state.handler
        }
        if let handler {
            return await handler(id)
        }
        return 0
    }
}

mockReturn = ".void"
} else if let val = defaultVal {
mockReturn = ".val(\(val))"
struct Renderer {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed the implementation because the processing became more complex.

let fixtureContents = formattedDstContent.components(separatedBy: .whitespacesAndNewlines).filter{!$0.isEmpty}
XCTAssert(fixtureContents == outputContents, "output:\n" + output)
let fixtureContents = dstContent.components(separatedBy: .whitespacesAndNewlines).filter{!$0.isEmpty}
XCTAssert(outputContents.contains(subArray: fixtureContents), "output:\n" + output)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Instead of examining all lines, we check whether the output lines contain the fixture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant