Skip to content

Simplify tests #896

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

Merged
merged 6 commits into from
Oct 13, 2023
Merged

Simplify tests #896

merged 6 commits into from
Oct 13, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 12, 2023

Apologies for the large diff but all the changes are fairly straightforward and intertwined so it doesn’t make too much sense to split them up.

The goal of this PR is to simplify the API of TestSourceKitServer

  • Send requests and notifications to SourceKitServer by directly calling into SourceKitServer.handle instead of going through a Connection. This makes the code a lot easier to understand statically
  • Make TestSourceKitServer conform to MessageHandler instead of going through TestClient
    • IMO this centralizes all the handling and makes it a lot easier to follow. TestClient didn’t do a whole bunch anyway.
  • Change sendSync function that sends a request and returns the result to be async
  • Allow async awaiting of next notifications instead of having to register a handleNextNotification handler before expecting the notification to be emitted.
    • This allows us to remove quite a few XCTExpectations in test cases
  • Remove the JSONRPC connection kind between TestSourceKitServer and SourceKitServer
    • It wasn’t actually used and the connection abstraction just made things more complicated than they needed to be
  • And finally rename TestSourceKitServer to TestSourceKitClient because really, it’s mocking an LSP client and isn’t modeling a server.

While doing this refactoring, I discovered a race condition where two code completion requests could be executed concurrently, causing conflicts because sourcekitd only has a single, global code completion session. I hot-fixed this for now by making code completion requests barriers in the message handling queue. The real fix is #899.

- Remove the JSONRPC connection kind between `TestSourceKitServer` and `SourceKitServer`
  - It wasn’t actually used and the connection abstraction just made things more complicated than they needed to be
- Send requests and notifications to `SourceKitServer` by directly calling into `SourceKitServer.handle` instead of going through a `Connection`. This makes the code a lot easier to understand statically
- Make `TestSourceKitServer` conform to `MessageHandler` instead of going through `TestClient`
  - IMO this centralizes all the handling and makes it a lot easier to follow. `TestClient` didn’t do a whole bunch anyway.
- Allow async awaiting of next notifications instead of having to register a `handleNextNotification` handler before expecting the notification to be emitted.
  - This allows us to remove quite a few `XCTExpectation`s in test cases
- Change `sendSync` function that sends a request and returns the result to be `async`
Really, what the class was mocking is a SourceKit-LSP client.
…eKitServer.Options`

Really, these options had nothing to do with `TestSourceKitLSPClient`, they are just the defaults that are used for all the tests.
It turns out that `XCTestCase.setUp` can indeed be `async` and we thus don’t actually need `awaitTask`
@ahoppen
Copy link
Member Author

ahoppen commented Oct 12, 2023

@swift-ci Please test

Comment on lines +538 to +541
// FIXME: (async) We need more granular request handling. Completion requests
// to the same file depend on each other because we only have one global
// code completion session in sourcekitd but they don't need to be full
// barriers to any other request.
Copy link
Contributor

Choose a reason for hiding this comment

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

If there's only the one session, doesn't that mean all completion requests (even across files) have to depend on each other? What happens today if multiple are sent?

We could also just make it an error to request completion when there's already a completion happening.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let’s discuss any of the code completion scheduling on #899.

@ahoppen ahoppen merged commit 78a4fab into swiftlang:main Oct 13, 2023
@ahoppen ahoppen deleted the ahoppen/simplify-tests branch October 13, 2023 20:43
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.

3 participants