Browse Source

Simplify waiting on predicates for operations to finish.

Nikita Lutsenko 9 years ago
parent
commit
bf9252bcd6

+ 0 - 6
SocketRocket.xcodeproj/project.pbxproj

@@ -20,7 +20,6 @@
 		555E0EB41C51E57A00E6BB92 /* SocketRocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 555E0EB11C51E56D00E6BB92 /* SocketRocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		8105E4801CDD67B400AA12DB /* SRAutobahnTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8105E47A1CDD679A00AA12DB /* SRAutobahnTests.m */; };
 		8105E4821CDD67BD00AA12DB /* SRTWebSocketOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 8105E4771CDD679A00AA12DB /* SRTWebSocketOperation.m */; };
-		8105E4841CDD67CE00AA12DB /* XCTestCase+SRTAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 8105E47E1CDD679A00AA12DB /* XCTestCase+SRTAdditions.m */; };
 		8105E4AE1CDD6E6200AA12DB /* SRAutobahnOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 8105E4AD1CDD6E6200AA12DB /* SRAutobahnOperation.m */; };
 		8105E5281CDD98E100AA12DB /* autobahn_configuration.json in Resources */ = {isa = PBXBuildFile; fileRef = 8105E5271CDD98E100AA12DB /* autobahn_configuration.json */; };
 		811934BC1CDAF725003AB243 /* SocketRocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 555E0EB11C51E56D00E6BB92 /* SocketRocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -116,8 +115,6 @@
 		8105E4771CDD679A00AA12DB /* SRTWebSocketOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SRTWebSocketOperation.m; sourceTree = "<group>"; };
 		8105E4791CDD679A00AA12DB /* SRWebSocketTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "SRWebSocketTests-Info.plist"; sourceTree = "<group>"; };
 		8105E47A1CDD679A00AA12DB /* SRAutobahnTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SRAutobahnTests.m; sourceTree = "<group>"; };
-		8105E47D1CDD679A00AA12DB /* XCTestCase+SRTAdditions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "XCTestCase+SRTAdditions.h"; sourceTree = "<group>"; };
-		8105E47E1CDD679A00AA12DB /* XCTestCase+SRTAdditions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "XCTestCase+SRTAdditions.m"; sourceTree = "<group>"; };
 		8105E4AC1CDD6E6200AA12DB /* SRAutobahnOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SRAutobahnOperation.h; sourceTree = "<group>"; };
 		8105E4AD1CDD6E6200AA12DB /* SRAutobahnOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SRAutobahnOperation.m; sourceTree = "<group>"; };
 		8105E5271CDD98E100AA12DB /* autobahn_configuration.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = autobahn_configuration.json; sourceTree = "<group>"; };
@@ -271,8 +268,6 @@
 			children = (
 				8179967E1CE184F40084DA37 /* SRAutobahnUtilities.h */,
 				8179967F1CE184F40084DA37 /* SRAutobahnUtilities.m */,
-				8105E47D1CDD679A00AA12DB /* XCTestCase+SRTAdditions.h */,
-				8105E47E1CDD679A00AA12DB /* XCTestCase+SRTAdditions.m */,
 			);
 			path = Utilities;
 			sourceTree = "<group>";
@@ -744,7 +739,6 @@
 			buildActionMask = 2147483647;
 			files = (
 				8105E4AE1CDD6E6200AA12DB /* SRAutobahnOperation.m in Sources */,
-				8105E4841CDD67CE00AA12DB /* XCTestCase+SRTAdditions.m in Sources */,
 				817996801CE184F40084DA37 /* SRAutobahnUtilities.m in Sources */,
 				8105E4801CDD67B400AA12DB /* SRAutobahnTests.m in Sources */,
 				8105E4821CDD67BD00AA12DB /* SRTWebSocketOperation.m in Sources */,

+ 2 - 0
Tests/Operations/SRTWebSocketOperation.h

@@ -24,4 +24,6 @@
 - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean NS_REQUIRES_SUPER;
 - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error NS_REQUIRES_SUPER;
 
+- (BOOL)waitUntilFinishedWithTimeout:(NSTimeInterval)timeout;
+
 @end

+ 12 - 0
Tests/Operations/SRTWebSocketOperation.m

@@ -11,6 +11,8 @@
 
 #import "SRTWebSocketOperation.h"
 
+#import "SRAutobahnUtilities.h"
+
 @interface SRTWebSocketOperation ()
 
 @end
@@ -81,4 +83,14 @@
     _webSocket = nil;
 }
 
+- (BOOL)waitUntilFinishedWithTimeout:(NSTimeInterval)timeout
+{
+    if (self.isFinished) {
+        return YES;
+    }
+    return SRRunLoopRunUntil(^BOOL{
+        return self.isFinished;
+    }, timeout);
+}
+
 @end

+ 37 - 37
Tests/SRAutobahnTests.m

@@ -16,7 +16,6 @@
 
 #import "SRTWebSocketOperation.h"
 #import "SRAutobahnOperation.h"
-#import "XCTestCase+SRTAdditions.h"
 #import "SRAutobahnUtilities.h"
 
 @interface SRAutobahnTests : XCTestCase
@@ -24,6 +23,33 @@
 
 @implementation SRAutobahnTests
 
+///--------------------------------------
+#pragma mark - Init
+///--------------------------------------
+
+/**
+ This method is called if Xcode is targeting a specific test or a set of them.
+ If you change this method - please make sure you test this behavior in Xcode by running all tests, then running 1+ test.
+ */
++ (instancetype)testCaseWithSelector:(SEL)selector
+{
+    NSArray<NSInvocation *> *invocations = [self testInvocations];
+    for (NSInvocation *invocation in invocations) {
+        if (invocation.selector == selector) {
+            return [super testCaseWithSelector:selector];
+        }
+    }
+    return nil;
+}
+
+///--------------------------------------
+#pragma mark - Setup
+///--------------------------------------
+
+/**
+ This method is called by xctest to figure out all the tests that are available.
+ All the selector names are also reported back to Xcode and displayed in Test Navigator/Console.
+ */
 + (NSArray<NSInvocation *> *)testInvocations
 {
     __block NSArray<NSInvocation *> *array = nil;
@@ -45,37 +71,6 @@
     return array;
 }
 
-+ (void)updateReports
-{
-    SRAutobahnOperation *operation = SRAutobahnTestUpdateReportsOperation(SRAutobahnTestServerURL(), SRAutobahnTestAgentName());
-    [operation start];
-
-    SRRunLoopRunUntil(^BOOL{
-        return operation.isFinished;
-    }, 60 * 60);
-
-    NSAssert(!operation.error, @"Updating the report should not have errored %@", operation.error);
-}
-
-///--------------------------------------
-#pragma mark - Init
-///--------------------------------------
-
-+ (instancetype)testCaseWithSelector:(SEL)selector
-{
-    NSArray<NSInvocation *> *invocations = [self testInvocations];
-    for (NSInvocation *invocation in invocations) {
-        if (invocation.selector == selector) {
-            return [super testCaseWithSelector:selector];
-        }
-    }
-    return nil;
-}
-
-///--------------------------------------
-#pragma mark - Setup
-///--------------------------------------
-
 + (NSInvocation *)invocationWithCaseNumber:(NSUInteger)caseNumber identifier:(NSString *)identifier
 {
     SEL selector = [self addInstanceMethodForTestCaseNumber:caseNumber identifier:identifier];
@@ -109,6 +104,15 @@
     [super tearDown];
 }
 
++ (void)updateReports
+{
+    SRAutobahnOperation *operation = SRAutobahnTestUpdateReportsOperation(SRAutobahnTestServerURL(), SRAutobahnTestAgentName());
+    [operation start];
+
+    NSAssert([operation waitUntilFinishedWithTimeout:60], @"Timed out on updating reports.");
+    NSAssert(!operation.error, @"Updating the report should not have errored %@", operation.error);
+}
+
 ///--------------------------------------
 #pragma mark - Test
 ///--------------------------------------
@@ -131,12 +135,8 @@
     [resultOp addDependency:testOp];
     [testQueue addOperation:resultOp];
 
-    testQueue.suspended = NO;
-
-    [self runCurrentRunLoopUntilTestPasses:^BOOL{
-        return resultOp.isFinished;
-    } timeout:60 * 60];
 
+    XCTAssertTrue([resultOp waitUntilFinishedWithTimeout:60 * 5], @"Test operation timed out.");
     XCTAssertTrue(!testOp.error, @"Test operation should not have failed");
     if (!SRAutobahnIsValidResultBehavior(identifier, resultInfo[@"behavior"])) {
         XCTFail(@"Invalid test behavior %@ for %@.", resultInfo[@"behavior"], identifier);

+ 8 - 15
Tests/Utilities/SRAutobahnUtilities.m

@@ -92,18 +92,15 @@ NSUInteger SRAutobahnTestCaseCount(void)
     static NSUInteger count;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
-        SRAutobahnOperation *caseGetter = SRAutobahnTestCaseCountOperation(SRAutobahnTestServerURL(),
+        SRAutobahnOperation *operation = SRAutobahnTestCaseCountOperation(SRAutobahnTestServerURL(),
                                                                            SRAutobahnTestAgentName(),
                                                                            ^(NSInteger caseCount) {
                                                                                count = caseCount;
                                                                            });
-        [caseGetter start];
+        [operation start];
 
-        SRRunLoopRunUntil(^BOOL{
-            return caseGetter.isFinished;
-        }, 20.0);
-
-        NSCAssert(!caseGetter.error, @"CaseGetter should have successfully returned the number of testCases. Instead got error %@", caseGetter.error);
+        NSCAssert([operation waitUntilFinishedWithTimeout:10], @"Timed out fetching test case count.");
+        NSCAssert(!operation.error, @"CaseGetter should have successfully returned the number of testCases. Instead got error %@", operation.error);
     });
     return count;
 }
@@ -111,17 +108,13 @@ NSUInteger SRAutobahnTestCaseCount(void)
 NSDictionary<NSString *, id> *SRAutobahnTestCaseInfo(NSInteger caseNumber)
 {
     __block NSDictionary *caseInfo = nil;
-    SRAutobahnOperation *testInfoOperation = SRAutobahnTestCaseInfoOperation(SRAutobahnTestServerURL(), caseNumber, ^(NSDictionary * _Nullable info) {
+    SRAutobahnOperation *operation = SRAutobahnTestCaseInfoOperation(SRAutobahnTestServerURL(), caseNumber, ^(NSDictionary * _Nullable info) {
         caseInfo = info;
     });
+    [operation start];
 
-    [testInfoOperation start];
-
-    SRRunLoopRunUntil(^BOOL{
-        return testInfoOperation.isFinished;
-    }, 60 * 60);
-
-    NSCAssert(!testInfoOperation.error, @"Updating the report should not have errored");
+    NSCAssert([operation waitUntilFinishedWithTimeout:10], @"Timed out fetching test case info %ld.", (long)caseNumber);
+    NSCAssert(!operation.error, @"Updating the report should not have errored");
     return caseInfo;
 }
 

+ 0 - 18
Tests/Utilities/XCTestCase+SRTAdditions.h

@@ -1,18 +0,0 @@
-//
-// Copyright 2012 Square Inc.
-// Portions Copyright (c) 2016-present, Facebook, Inc.
-//
-// All rights reserved.
-//
-// This source code is licensed under the BSD-style license found in the
-// LICENSE file in the root directory of this source tree. An additional grant
-// of patent rights can be found in the PATENTS file in the same directory.
-//
-
-#import <XCTest/XCTest.h>
-
-@interface XCTestCase (SRTAdditions)
-
-- (void)runCurrentRunLoopUntilTestPasses:(BOOL (^)())predicate timeout:(NSTimeInterval)timeout;
-
-@end

+ 0 - 23
Tests/Utilities/XCTestCase+SRTAdditions.m

@@ -1,23 +0,0 @@
-//
-// Copyright 2012 Square Inc.
-// Portions Copyright (c) 2016-present, Facebook, Inc.
-//
-// All rights reserved.
-//
-// This source code is licensed under the BSD-style license found in the
-// LICENSE file in the root directory of this source tree. An additional grant
-// of patent rights can be found in the PATENTS file in the same directory.
-//
-
-#import "XCTestCase+SRTAdditions.h"
-
-#import "SRAutobahnUtilities.h"
-
-@implementation XCTestCase (SRTAdditions)
-
-- (void)runCurrentRunLoopUntilTestPasses:(BOOL (^)())predicate timeout:(NSTimeInterval)timeout
-{
-    XCTAssertTrue(SRRunLoopRunUntil(predicate, timeout), @"Timed out");
-}
-
-@end