From 1176dc36c56ac32dd7aea4b5f8734188faffca24 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Thu, 21 Jun 2018 11:52:59 -0700 Subject: [PATCH] Add tests --- src/objective-c/tests/GRPCClientTests.m | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/objective-c/tests/GRPCClientTests.m b/src/objective-c/tests/GRPCClientTests.m index d9186561c35..2a169800a09 100644 --- a/src/objective-c/tests/GRPCClientTests.m +++ b/src/objective-c/tests/GRPCClientTests.m @@ -548,4 +548,47 @@ static GRPCProtoMethod *kFullDuplexCallMethod; [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; } +- (void)testTimeoutBackoffWithTimeout:(double)timeout Backoff:(double)backoff { + const double maxConnectTime = timeout > backoff ? timeout : backoff; + const double kMargin = 0.1; + + __weak XCTestExpectation *completion = [self expectationWithDescription:@"Timeout in a second."]; + NSString *const kDummyAddress = [NSString stringWithFormat:@"8.8.8.8:1"]; + GRPCCall *call = [[GRPCCall alloc] initWithHost:kDummyAddress + path:@"" + requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; + [GRPCCall setMinConnectTimeout:timeout * 1000 + initialBackoff:backoff * 1000 + maxBackoff:0 + forHost:kDummyAddress]; + NSDate *startTime = [NSDate date]; + id responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(id value) { + XCTAssert(NO, @"Received message. Should not reach here"); + } + completionHandler:^(NSError *errorOrNil) { + XCTAssertNotNil(errorOrNil, @"Finished with no error"); + // The call must fail before maxConnectTime. However there is no lower bound on the time + // taken for connection. A shorter time happens when connection is actively refused + // by 8.8.8.8:1 before maxConnectTime elapsed. + XCTAssertLessThan([[NSDate date] timeIntervalSinceDate:startTime], + maxConnectTime + kMargin); + [completion fulfill]; + }]; + + [call startWithWriteable:responsesWriteable]; + + [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; +} + +// The numbers of the following three tests are selected to be smaller than the default values of +// initial backoff (1s) and min_connect_timeout (20s), so that if they fail we know the default +// values fail to be overridden by the channel args. +- (void)testTimeoutBackoff2 { + [self testTimeoutBackoffWithTimeout:0.7 Backoff:0.3]; +} + +- (void)testTimeoutBackoff3 { + [self testTimeoutBackoffWithTimeout:0.3 Backoff:0.7]; +} + @end