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

Merged
merged 13 commits into from
Dec 3, 2024
Merged

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.

Copy link
Collaborator

@fummicc1 fummicc1 left a comment

Choose a reason for hiding this comment

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

Thank you so much! LGTM from my side, but I left some questions for posterity.

@@ -23,12 +23,12 @@ final class NominalModel: Model {
let accessLevel: String
let identifier: String
let declKindOfMockAnnotatedBaseType: NominalTypeDeclKind
let inheritedTypes: [String]
Copy link
Collaborator

Choose a reason for hiding this comment

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

[question]

I am sorry but could you elaborate why you delete inheritedTypes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because it is never used.
It might be back in the future if needed.

Comment on lines +3 to +5
fileprivate func warnIfNotSendable<each T>(function: String = #function, _: repeat each T) {
print("At \(function), the captured arguments are not Sendable, it is not concurrency-safe.")
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not confident this print statement is needed or not, as mocking library.
I think print is not time-efficient. It might be good to opt-in this warning in another PR. What do you think of it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This function is called only if there is an issue with the user's implementation, so it can be resolved if the user fixes the problem.

As an escape hatch, I want to make it possible to disable it with:

/// @mockable(history: fooFunc = false)

(Probably it cannot be done now.)

Comment on lines +62 to +64
var requiresSendable: Bool {
return inheritedTypes.contains(.sendable) || inheritedTypes.contains(.error)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

[question]

Actor protocol is not written here. Is it okay?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The mock of an Actor protocol automatically becomes an actor, so it is not necessary to make the mock implementation Sendable.

@sidepelican sidepelican merged commit 1fb44ed into uber:master Dec 3, 2024
3 checks passed
@sidepelican sidepelican deleted the concurrency_safe branch December 3, 2024 09:48
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.

2 participants