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

Add ability to pause/resume websocket without dumping subscriptions #1335

Merged
merged 7 commits into from
Aug 6, 2020

Conversation

designatednerd
Copy link
Contributor

Addresses #1333.

@fassko If you're around, I'd love your advice on whether I'm doing something terribly dumb with Starscream here 😇

Copy link
Contributor

@fassko fassko left a comment

Choose a reason for hiding this comment

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

Overall looks légit

About omitting self, I just like less code and use self` only where it is used.

/// Reconnects a paused web socket.
///
/// - Parameter autoReconnect: `true` if you want the websocket to automatically reconnect if the connection drops. Defaults to true.
public func resumeWebSocketConnection(autoReconnect: Bool = true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think just reconnect is fine? Aligning it with Starscream. But on other hand adding auto can be more explicit. :P

Tests/ApolloWebsocketTests/StarWarsSubscriptionTests.swift Outdated Show resolved Hide resolved
}

extension StarWarsSubscriptionTests: WebSocketTransportDelegate {

func webSocketTransportDidConnect(_ webSocketTransport: WebSocketTransport) {
self.connectionStartedExpectation?.fulfill()
}

func webSocketTransportDidReconnect(_ webSocketTransport: WebSocketTransport) {
self.reconnectedExpectation?.fulfill()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.reconnectedExpectation?.fulfill()
reconnectedExpectation?.fulfill()

I don't know if there is a style guide, but I usually omit self when it's not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is not currently.

Personally, I find self makes it much easier to disambiguate local vars from class vars when you're looking at things without syntax highlighting (ex, in Github). At some point I do want to make this more consistent throughout the codebase, but I'm gonna leave these for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK yep, being consistent is the key here, I agree!

}

func webSocketTransport(_ webSocketTransport: WebSocketTransport, didDisconnectWithError error: Error?) {
self.disconnectedExpectation?.fulfill()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.disconnectedExpectation?.fulfill()
disconnectedExpectation?.fulfill()

httpClient.perform(mutation: reviewMutation) { mutationResult in
switch mutationResult {
case .success:
break
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm how about already here it can fulfill the expectation? Or I'm missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The expectation should be fulfilled and the wait should be ended no matter which case comes in, so that's why it's below the switch statement

Copy link
Contributor

Choose a reason for hiding this comment

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

I think XCTFail runs before I guess. Hmm ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep that's by design - we want that failure to occur before the wait ends.

self.waitForSubscriptionsToStart()
sendReview()

self.disconnectedExpectation = self.expectation(description: "Web socket disconnected")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.disconnectedExpectation = self.expectation(description: "Web socket disconnected")
disconnectedExpectation = expectation(description: "Web socket disconnected")


self.disconnectedExpectation = self.expectation(description: "Web socket disconnected")
webSocketTransport.pauseWebSocketConnection()
self.wait(for: [self.disconnectedExpectation!], timeout: 10)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.wait(for: [self.disconnectedExpectation!], timeout: 10)
wait(for: [disconnectedExpectation!], timeout: 10)

Do we need to specify exact expectation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes since that's the only one we want to wait for

// This should not go through since the socket is paused
sendReview()

self.reconnectedExpectation = self.expectation(description: "Web socket reconnected")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.reconnectedExpectation = self.expectation(description: "Web socket reconnected")
reconnectedExpectation = expectation(description: "Web socket reconnected")

Comment on lines +469 to +470
self.wait(for: [self.reconnectedExpectation!], timeout: 10)
self.waitForSubscriptionsToStart()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.wait(for: [self.reconnectedExpectation!], timeout: 10)
self.waitForSubscriptionsToStart()
wait(for: [reconnectedExpectation!], timeout: 10)
waitForSubscriptionsToStart()

// Now that we've reconnected, this should go through to the same subscription.
sendReview()

self.wait(for: [subscriptionExpectation], timeout: 10)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.wait(for: [subscriptionExpectation], timeout: 10)
wait(for: [subscriptionExpectation], timeout: 10)

Copy link
Contributor

@fassko fassko left a comment

Choose a reason for hiding this comment

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

Overall looks légit

About omitting self, I just like less code and use self` only where it is used.

@designatednerd
Copy link
Contributor Author

At some point I'll have to make a real decision about self usage, but for now I'm gonna leave it in.

@designatednerd designatednerd merged commit 0de79e3 into main Aug 6, 2020
@designatednerd designatednerd added this to the Next Release milestone Aug 6, 2020
@designatednerd designatednerd deleted the add/websocket-pause branch August 6, 2020 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants