-
Notifications
You must be signed in to change notification settings - Fork 1.6k
DispatchQueue delayed callback improvements + testing #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mikelehen
merged 3 commits into
master
from
mikelehen/dispatch-queue-delays-and-testing
Feb 16, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import "Firestore/Source/Util/FSTDispatchQueue.h" | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
#import "Firestore/Example/Tests/Util/XCTestCase+Await.h" | ||
|
||
// In these generic tests the specific TimerIDs don't matter. | ||
static const FSTTimerID timerID1 = FSTTimerIDListenStreamConnection; | ||
static const FSTTimerID timerID2 = FSTTimerIDListenStreamIdle; | ||
static const FSTTimerID timerID3 = FSTTimerIDWriteStreamConnection; | ||
|
||
@interface FSTDispatchQueueTests : XCTestCase | ||
@end | ||
|
||
@implementation FSTDispatchQueueTests { | ||
FSTDispatchQueue *_queue; | ||
NSMutableArray *_completedSteps; | ||
NSArray *_expectedSteps; | ||
XCTestExpectation *_expectation; | ||
} | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
dispatch_queue_t dispatch_queue = | ||
dispatch_queue_create("FSTDispatchQueueTests", DISPATCH_QUEUE_SERIAL); | ||
_queue = [[FSTDispatchQueue alloc] initWithQueue:dispatch_queue]; | ||
_completedSteps = [NSMutableArray array]; | ||
_expectedSteps = nil; | ||
} | ||
|
||
/** | ||
* Helper to return a block that adds @(n) to _completedSteps when run and fulfils _expectation if | ||
* the _completedSteps match the _expectedSteps. | ||
*/ | ||
- (void (^)())blockForStep:(int)n { | ||
return ^void() { | ||
[self->_completedSteps addObject:@(n)]; | ||
if (self->_expectedSteps && self->_completedSteps.count >= self->_expectedSteps.count) { | ||
XCTAssertEqualObjects(self->_completedSteps, self->_expectedSteps); | ||
[self->_expectation fulfill]; | ||
} | ||
}; | ||
} | ||
|
||
- (void)testCanScheduleCallbacksInTheFuture { | ||
_expectation = [self expectationWithDescription:@"Expected steps"]; | ||
_expectedSteps = @[ @1, @2, @3, @4 ]; | ||
[_queue dispatchAsync:[self blockForStep:1]]; | ||
[_queue dispatchAfterDelay:0.005 timerID:timerID1 block:[self blockForStep:4]]; | ||
[_queue dispatchAfterDelay:0.001 timerID:timerID2 block:[self blockForStep:3]]; | ||
[_queue dispatchAsync:[self blockForStep:2]]; | ||
|
||
[self awaitExpectations]; | ||
} | ||
|
||
- (void)testCanCancelDelayedCallbacks { | ||
_expectation = [self expectationWithDescription:@"Expected steps"]; | ||
_expectedSteps = @[ @1, @3 ]; | ||
// Queue everything from the queue to ensure nothing completes before we cancel. | ||
[_queue dispatchAsync:^{ | ||
[_queue dispatchAsyncAllowingSameQueue:[self blockForStep:1]]; | ||
FSTDelayedCallback *step2Timer = | ||
[_queue dispatchAfterDelay:.001 timerID:timerID1 block:[self blockForStep:2]]; | ||
[_queue dispatchAfterDelay:.005 timerID:timerID2 block:[self blockForStep:3]]; | ||
|
||
XCTAssertTrue([_queue containsDelayedCallbackWithTimerID:timerID1]); | ||
[step2Timer cancel]; | ||
XCTAssertFalse([_queue containsDelayedCallbackWithTimerID:timerID1]); | ||
}]; | ||
|
||
[self awaitExpectations]; | ||
} | ||
|
||
- (void)testCanManuallyDrainAllDelayedCallbacksForTesting { | ||
[_queue dispatchAsync:[self blockForStep:1]]; | ||
[_queue dispatchAfterDelay:20 timerID:timerID1 block:[self blockForStep:4]]; | ||
[_queue dispatchAfterDelay:10 timerID:timerID2 block:[self blockForStep:3]]; | ||
[_queue dispatchAsync:[self blockForStep:2]]; | ||
|
||
[_queue runDelayedCallbacksUntil:FSTTimerIDAll]; | ||
XCTAssertEqualObjects(_completedSteps, (@[ @1, @2, @3, @4 ])); | ||
} | ||
|
||
- (void)testCanManuallyDrainSpecificDelayedCallbacksForTesting { | ||
[_queue dispatchAsync:[self blockForStep:1]]; | ||
[_queue dispatchAfterDelay:20 timerID:timerID1 block:[self blockForStep:5]]; | ||
[_queue dispatchAfterDelay:10 timerID:timerID2 block:[self blockForStep:3]]; | ||
[_queue dispatchAfterDelay:15 timerID:timerID3 block:[self blockForStep:4]]; | ||
[_queue dispatchAsync:[self blockForStep:2]]; | ||
|
||
[_queue runDelayedCallbacksUntil:timerID3]; | ||
XCTAssertEqualObjects(_completedSteps, (@[ @1, @2, @3, @4 ])); | ||
} | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this function pays for itself. Why not just access this property directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because only this file has this at the top:
I'm open to exposing this property to tests differently if you have a more-preferred way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other cases we've added the testing category in a separate header and just included that in the places where it's required. However this whole thing is going to be rewritten in C++ where that pattern doesn't apply so carry on.