From 81b4fcbe84ad644ddd2ff6c293610415859bec03 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Mon, 24 Aug 2015 10:56:44 -0700 Subject: Added test for responseHeaders KVO compliance --- src/objective-c/tests/GRPCClientTests.m | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'src/objective-c/tests/GRPCClientTests.m') diff --git a/src/objective-c/tests/GRPCClientTests.m b/src/objective-c/tests/GRPCClientTests.m index 06581e7599..fa604211e2 100644 --- a/src/objective-c/tests/GRPCClientTests.m +++ b/src/objective-c/tests/GRPCClientTests.m @@ -53,6 +53,37 @@ static ProtoMethod *kInexistentMethod; static ProtoMethod *kEmptyCallMethod; static ProtoMethod *kUnaryCallMethod; +// This is an observer class for testing that responseMetadata is KVO-compliant + +@interface PassthroughObserver : NSObject + +- (instancetype) initWithCallback:(void (^)(NSString*, id, NSDictionary*))callback; + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change + context:(void *)context; +@end + +@implementation PassthroughObserver { + void (^_callback)(NSString*, id, NSDictionary*); +} + +- (instancetype)initWithCallback:(void (^)(NSString *, id, NSDictionary *))callback { + self = [super init]; + if (self) { + _callback = callback; + } + return self; + +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + _callback(keyPath, object, change); + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; +} + +@end + @interface GRPCClientTests : XCTestCase @end @@ -183,4 +214,35 @@ static ProtoMethod *kUnaryCallMethod; [self waitForExpectationsWithTimeout:4 handler:nil]; } +- (void)testResponseMetadataKVO { + __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; + __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; + __weak XCTestExpectation *metadata = [self expectationWithDescription:@"Metadata changed."]; + + PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) { + if (keypath == @"responseHeaders") { + [expectation fulfill]; + } + }] + + GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress + path:kEmptyCallMethod.HTTPPath + requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; + + [call addObserver:observer forKeyPath:@"responseHeaders" options:0 context:NULL]; + + id responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { + XCTAssertNotNil(value, @"nil value received as response."); + XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value); + [response fulfill]; + } completionHandler:^(NSError *errorOrNil) { + XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil); + [completion fulfill]; + }]; + + [call startWithWriteable:responsesWriteable]; + + [self waitForExpectationsWithTimeout:8 handler:nil]; +} + @end -- cgit v1.2.3 From 2e49a355b64e2fec5f00995b8ba2dba1afef9700 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Mon, 24 Aug 2015 14:40:45 -0700 Subject: Fixed KVO test --- src/objective-c/tests/GRPCClientTests.m | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/objective-c/tests/GRPCClientTests.m') diff --git a/src/objective-c/tests/GRPCClientTests.m b/src/objective-c/tests/GRPCClientTests.m index fa604211e2..09a55e0704 100644 --- a/src/objective-c/tests/GRPCClientTests.m +++ b/src/objective-c/tests/GRPCClientTests.m @@ -79,7 +79,7 @@ static ProtoMethod *kUnaryCallMethod; - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { _callback(keyPath, object, change); - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + [object removeObserver:self forKeyPath:keyPath]; } @end @@ -219,16 +219,16 @@ static ProtoMethod *kUnaryCallMethod; __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; __weak XCTestExpectation *metadata = [self expectationWithDescription:@"Metadata changed."]; - PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) { - if (keypath == @"responseHeaders") { - [expectation fulfill]; - } - }] - GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress path:kEmptyCallMethod.HTTPPath requestsWriter:[GRXWriter writerWithValue:[NSData data]]]; + PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) { + if ([keypath isEqual: @"responseHeaders"]) { + [metadata fulfill]; + } + }]; + [call addObserver:observer forKeyPath:@"responseHeaders" options:0 context:NULL]; id responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { -- cgit v1.2.3