From 9656eca5102c6ee99f7cff227226a8c927eda722 Mon Sep 17 00:00:00 2001
From: Test User <noemail@noemail.com>
Date: Thu, 18 Feb 2016 14:47:22 -0800
Subject: [PATCH] Added tests for code coverage

---
 src/objective-c/tests/GRPCClientTests.m       | 45 ++++++++++++++++---
 .../tests/InteropTestsLocalCleartext.m        |  2 +-
 src/objective-c/tests/InteropTestsLocalSSL.m  | 11 +++++
 src/objective-c/tests/RxLibraryUnitTests.m    | 23 ++++++++++
 4 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/src/objective-c/tests/GRPCClientTests.m b/src/objective-c/tests/GRPCClientTests.m
index 00c4b8830d..1a82993955 100644
--- a/src/objective-c/tests/GRPCClientTests.m
+++ b/src/objective-c/tests/GRPCClientTests.m
@@ -41,11 +41,11 @@
 #import <RemoteTest/Messages.pbobjc.h>
 #import <RxLibrary/GRXWriteable.h>
 #import <RxLibrary/GRXWriter+Immediate.h>
-
+#import <GRPCClient/GRPCCall+ChannelArg.h>
 static NSString * const kHostAddress = @"localhost:5050";
 static NSString * const kPackage = @"grpc.testing";
 static NSString * const kService = @"TestService";
-
+static NSString * const kRemoteSSLHost = @"grpc-test.sandbox.googleapis.com";
 static ProtoMethod *kInexistentMethod;
 static ProtoMethod *kEmptyCallMethod;
 static ProtoMethod *kUnaryCallMethod;
@@ -127,8 +127,8 @@ static ProtoMethod *kUnaryCallMethod;
     XCTFail(@"Received unexpected response: %@", value);
   } completionHandler:^(NSError *errorOrNil) {
     XCTAssertNotNil(errorOrNil, @"Finished without error!");
-    // TODO(jcanizales): The server should return code 12 UNIMPLEMENTED, not 5 NOT FOUND.
-    XCTAssertEqual(errorOrNil.code, 5, @"Finished with unexpected error: %@", errorOrNil);
+    //
+    XCTAssertEqual(errorOrNil.code, 12, @"Finished with unexpected error: %@", errorOrNil);
     [expectation fulfill];
   }];
 
@@ -200,7 +200,7 @@ static ProtoMethod *kUnaryCallMethod;
   request.fillOauthScope = YES;
   GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]];
 
-  GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
+  GRPCCall *call = [[GRPCCall alloc] initWithHost:kRemoteSSLHost
                                              path:kUnaryCallMethod.HTTPPath
                                    requestsWriter:requestsWriter];
 
@@ -257,4 +257,39 @@ static ProtoMethod *kUnaryCallMethod;
   [self waitForExpectationsWithTimeout:8 handler:nil];
 }
 
+- (void)testExceptions {
+  // Try to set userAgentPrefix for host that is nil. This should cause
+  // an exception.
+  @try {
+    [GRPCCall setUserAgentPrefix:@"Foo" forHost:nil];
+    XCTFail(@"Did not receive an exception when host is nil");
+  } @catch(NSException *theException) {
+    NSLog(@"Received exception as expected: %@", theException.name);
+  }
+
+  // Try to set parameters to nil for GRPCCall. This should cause an exception
+  @try {
+    GRPCCall *call = [[GRPCCall alloc] initWithHost:nil
+                                               path:nil
+                                     requestsWriter:nil];
+    XCTFail(@"Did not receive an exception when parameters are nil");
+  } @catch(NSException *theException) {
+    NSLog(@"Received exception as expected: %@", theException.name);
+  }
+
+
+  // Set state to Finished by force
+  GRXWriter *requestsWriter = [GRXWriter emptyWriter];
+  [requestsWriter finishWithError:nil];
+  @try {
+    GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
+                                               path:kUnaryCallMethod.HTTPPath
+                                     requestsWriter:requestsWriter];
+    XCTFail(@"Did not receive an exception when GRXWriter has incorrect state.");
+  } @catch(NSException *theException) {
+    NSLog(@"Received exception as expected: %@", theException.name);
+  }
+
+}
+
 @end
diff --git a/src/objective-c/tests/InteropTestsLocalCleartext.m b/src/objective-c/tests/InteropTestsLocalCleartext.m
index 56927a8af6..f454285110 100644
--- a/src/objective-c/tests/InteropTestsLocalCleartext.m
+++ b/src/objective-c/tests/InteropTestsLocalCleartext.m
@@ -49,7 +49,7 @@ static NSString * const kLocalCleartextHost = @"localhost:5050";
 
 - (void)setUp {
   // Register test server as non-SSL.
-  [GRPCCall useInsecureConnectionsForHost:kLocalCleartextHost];
+  [GRPCCall useInsecureConnectionsForHost:nil];
 
   [super setUp];
 }
diff --git a/src/objective-c/tests/InteropTestsLocalSSL.m b/src/objective-c/tests/InteropTestsLocalSSL.m
index 9d7afefbfe..f0f4b1d71f 100644
--- a/src/objective-c/tests/InteropTestsLocalSSL.m
+++ b/src/objective-c/tests/InteropTestsLocalSSL.m
@@ -57,4 +57,15 @@ static NSString * const kLocalSSLHost = @"localhost:5051";
   [super setUp];
 }
 
+- (void)testExceptions {
+  // Try to set userAgentPrefix for host that is nil. This should cause
+  // an exception.
+  @try {
+    [GRPCCall useTestCertsPath:nil testName:nil forHost:nil];
+    XCTFail(@"Did not receive an exception when parameters are nil");
+  } @catch(NSException *theException) {
+    NSLog(@"Received exception as expected: %@", theException.name);
+  }
+}
+
 @end
diff --git a/src/objective-c/tests/RxLibraryUnitTests.m b/src/objective-c/tests/RxLibraryUnitTests.m
index a67a4c6cd9..ba79191dc4 100644
--- a/src/objective-c/tests/RxLibraryUnitTests.m
+++ b/src/objective-c/tests/RxLibraryUnitTests.m
@@ -137,4 +137,27 @@
   XCTAssertEqualObjects(handler.errorOrNil, anyError);
 }
 
+- (void)testBufferedPipeFinishWriteWhilePaused {
+  // Given:
+  CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
+  id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
+  id anyValue = @7;
+
+  // If:
+  GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
+  // Write something, then finish
+  [pipe writeValue:anyValue];
+  [pipe writesFinishedWithError:nil];
+  // then start the writeable
+  [pipe startWithWriteable:writeable];
+
+  // Then:
+  XCTAssertEqual(handler.timesCalled, 1);
+  XCTAssertEqualObjects(handler.value, anyValue);
+  XCTAssertEqualObjects(handler.errorOrNil, nil);
+}
+
+- (void)testBufferedPipeSetState {
+}
+
 @end
-- 
GitLab