aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar makdharma <makdharma@users.noreply.github.com>2016-05-01 15:01:59 -0700
committerGravatar makdharma <makdharma@users.noreply.github.com>2016-05-01 15:01:59 -0700
commit6b1afe1ad9e6eda6864627f8ad1f42ee088ae4ed (patch)
tree18dc97919a8edea77b00ea7e4583fccbac8e0965 /src
parent2f262342039b9f21897dcedd9b62a75a129b1bcb (diff)
parent0803bb0b330e0a08c08697bcb4332eb836d04f63 (diff)
Merge pull request #6386 from jcanizales/fix-call-state
Fix call state
Diffstat (limited to 'src')
-rw-r--r--src/objective-c/GRPCClient/GRPCCall.m54
-rw-r--r--src/objective-c/tests/InteropTests.m6
2 files changed, 38 insertions, 22 deletions
diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m
index 1847d6016f..0eb10656dd 100644
--- a/src/objective-c/GRPCClient/GRPCCall.m
+++ b/src/objective-c/GRPCClient/GRPCCall.m
@@ -136,6 +136,10 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
#pragma mark Finish
- (void)finishWithError:(NSError *)errorOrNil {
+ @synchronized(self) {
+ _state = GRXWriterStateFinished;
+ }
+
// If the call isn't retained anywhere else, it can be deallocated now.
_retainSelf = nil;
@@ -342,6 +346,10 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
#pragma mark GRXWriter implementation
- (void)startWithWriteable:(id<GRXWriteable>)writeable {
+ @synchronized(self) {
+ _state = GRXWriterStateStarted;
+ }
+
// Create a retain cycle so that this instance lives until the RPC finishes (or is cancelled).
// This makes RPCs in which the call isn't externally retained possible (as long as it is started
// before being autoreleased).
@@ -375,30 +383,32 @@ NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
}
- (void)setState:(GRXWriterState)newState {
- // Manual transitions are only allowed from the started or paused states.
- if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) {
- return;
- }
-
- switch (newState) {
- case GRXWriterStateFinished:
- _state = newState;
- // Per GRXWriter's contract, setting the state to Finished manually
- // means one doesn't wish the writeable to be messaged anymore.
- [_responseWriteable cancelSilently];
- _responseWriteable = nil;
- return;
- case GRXWriterStatePaused:
- _state = newState;
+ @synchronized(self) {
+ // Manual transitions are only allowed from the started or paused states.
+ if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) {
return;
- case GRXWriterStateStarted:
- if (_state == GRXWriterStatePaused) {
+ }
+
+ switch (newState) {
+ case GRXWriterStateFinished:
_state = newState;
- [self startNextRead];
- }
- return;
- case GRXWriterStateNotStarted:
- return;
+ // Per GRXWriter's contract, setting the state to Finished manually
+ // means one doesn't wish the writeable to be messaged anymore.
+ [_responseWriteable cancelSilently];
+ _responseWriteable = nil;
+ return;
+ case GRXWriterStatePaused:
+ _state = newState;
+ return;
+ case GRXWriterStateStarted:
+ if (_state == GRXWriterStatePaused) {
+ _state = newState;
+ [self startNextRead];
+ }
+ return;
+ case GRXWriterStateNotStarted:
+ return;
+ }
}
}
diff --git a/src/objective-c/tests/InteropTests.m b/src/objective-c/tests/InteropTests.m
index 26877b1ae8..4f096b9efa 100644
--- a/src/objective-c/tests/InteropTests.m
+++ b/src/objective-c/tests/InteropTests.m
@@ -272,8 +272,14 @@
XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED);
[expectation fulfill];
}];
+ XCTAssertEqual(call.state, GRXWriterStateNotStarted);
+
[call start];
+ XCTAssertEqual(call.state, GRXWriterStateStarted);
+
[call cancel];
+ XCTAssertEqual(call.state, GRXWriterStateFinished);
+
[self waitForExpectationsWithTimeout:1 handler:nil];
}