-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Mark ASRunLoopQueue as drained if it contains only NULLs #558
Merged
garrettmoon
merged 7 commits into
TextureGroup:master
from
cesteban:CETRunLoopQueueInfiniteLoop
Sep 11, 2017
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
94bb783
Mark ASRunLoopQueue as drained if it contains only NULLs
cestebanez 747e973
Update CHANGELOG.md
cestebanez 4862fd6
Cover ASRunLoopQueue with tests
cestebanez 9a3910f
Include PR link in CHANGELOG.md
cestebanez 0e7d781
Replace license header of ASRunLoopQueueTests.m with correct one
cestebanez 7b4b361
Insert a nil in _internalQueue to ensure compaction, instead of maint…
cestebanez 5796eba
Merge branch 'master' into CETRunLoopQueueInfiniteLoop
nguyenhuy 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 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 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 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 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 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,165 @@ | ||
// | ||
// ASRunLoopQueueTests.m | ||
// Texture | ||
// | ||
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the /ASDK-Licenses directory of this source tree. An additional | ||
// grant of patent rights can be found in the PATENTS file in the same directory. | ||
// | ||
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present, | ||
// Pinterest, Inc. 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 | ||
// | ||
|
||
#import <XCTest/XCTest.h> | ||
#import <AsyncDisplayKit/ASRunLoopQueue.h> | ||
|
||
static NSTimeInterval const kRunLoopRunTime = 0.001; // Allow the RunLoop to run for one millisecond each time. | ||
|
||
@interface ASRunLoopQueueTests : XCTestCase | ||
|
||
@end | ||
|
||
@implementation ASRunLoopQueueTests | ||
|
||
#pragma mark enqueue tests | ||
|
||
- (void)testEnqueueNilObjectsToQueue | ||
{ | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:YES handler:nil]; | ||
id object = nil; | ||
[queue enqueue:object]; | ||
XCTAssertTrue(queue.isEmpty); | ||
} | ||
|
||
- (void)testEnqueueSameObjectTwiceToDefaultQueue | ||
{ | ||
id object = [[NSObject alloc] init]; | ||
__unsafe_unretained id weakObject = object; | ||
__block NSUInteger dequeuedCount = 0; | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:YES handler:^(id _Nonnull dequeuedItem, BOOL isQueueDrained) { | ||
if (dequeuedItem == weakObject) { | ||
dequeuedCount++; | ||
} | ||
}]; | ||
[queue enqueue:object]; | ||
[queue enqueue:object]; | ||
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:kRunLoopRunTime]]; | ||
XCTAssert(dequeuedCount == 1); | ||
} | ||
|
||
- (void)testEnqueueSameObjectTwiceToNonExclusiveMembershipQueue | ||
{ | ||
id object = [[NSObject alloc] init]; | ||
__unsafe_unretained id weakObject = object; | ||
__block NSUInteger dequeuedCount = 0; | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:YES handler:^(id _Nonnull dequeuedItem, BOOL isQueueDrained) { | ||
if (dequeuedItem == weakObject) { | ||
dequeuedCount++; | ||
} | ||
}]; | ||
queue.ensureExclusiveMembership = NO; | ||
[queue enqueue:object]; | ||
[queue enqueue:object]; | ||
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:kRunLoopRunTime]]; | ||
XCTAssert(dequeuedCount == 2); | ||
} | ||
|
||
#pragma mark processQueue tests | ||
|
||
- (void)testDefaultQueueProcessObjectsOneAtATime | ||
{ | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:YES handler:^(id _Nonnull dequeuedItem, BOOL isQueueDrained) { | ||
[NSThread sleepForTimeInterval:kRunLoopRunTime * 2]; // So each element takes more time than the available | ||
}]; | ||
[queue enqueue:[[NSObject alloc] init]]; | ||
[queue enqueue:[[NSObject alloc] init]]; | ||
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:kRunLoopRunTime]]; | ||
XCTAssertFalse(queue.isEmpty); | ||
} | ||
|
||
- (void)testQueueProcessObjectsInBatchesOfSpecifiedSize | ||
{ | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:YES handler:^(id _Nonnull dequeuedItem, BOOL isQueueDrained) { | ||
[NSThread sleepForTimeInterval:kRunLoopRunTime * 2]; // So each element takes more time than the available | ||
}]; | ||
queue.batchSize = 2; | ||
[queue enqueue:[[NSObject alloc] init]]; | ||
[queue enqueue:[[NSObject alloc] init]]; | ||
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:kRunLoopRunTime]]; | ||
XCTAssertTrue(queue.isEmpty); | ||
} | ||
|
||
- (void)testQueueOnlySendsIsDrainedForLastObjectInBatch | ||
{ | ||
id objectA = [[NSObject alloc] init]; | ||
id objectB = [[NSObject alloc] init]; | ||
__unsafe_unretained id weakObjectA = objectA; | ||
__unsafe_unretained id weakObjectB = objectB; | ||
__block BOOL isQueueDrainedWhenProcessingA = NO; | ||
__block BOOL isQueueDrainedWhenProcessingB = NO; | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:YES handler:^(id _Nonnull dequeuedItem, BOOL isQueueDrained) { | ||
if (dequeuedItem == weakObjectA) { | ||
isQueueDrainedWhenProcessingA = isQueueDrained; | ||
} else if (dequeuedItem == weakObjectB) { | ||
isQueueDrainedWhenProcessingB = isQueueDrained; | ||
} | ||
}]; | ||
queue.batchSize = 2; | ||
[queue enqueue:objectA]; | ||
[queue enqueue:objectB]; | ||
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:kRunLoopRunTime]]; | ||
XCTAssertFalse(isQueueDrainedWhenProcessingA); | ||
XCTAssertTrue(isQueueDrainedWhenProcessingB); | ||
} | ||
|
||
#pragma mark strong/weak tests | ||
|
||
- (void)testStrongQueueRetainsObjects | ||
{ | ||
id object = [[NSObject alloc] init]; | ||
__unsafe_unretained id weakObject = object; | ||
__block BOOL didProcessObject = NO; | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:YES handler:^(id _Nonnull dequeuedItem, BOOL isQueueDrained) { | ||
if (dequeuedItem == weakObject) { | ||
didProcessObject = YES; | ||
} | ||
}]; | ||
[queue enqueue:object]; | ||
object = nil; | ||
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:kRunLoopRunTime]]; | ||
XCTAssertTrue(didProcessObject); | ||
} | ||
|
||
- (void)testWeakQueueDoesNotRetainsObjects | ||
{ | ||
id object = [[NSObject alloc] init]; | ||
__unsafe_unretained id weakObject = object; | ||
__block BOOL didProcessObject = NO; | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:NO handler:^(id _Nonnull dequeuedItem, BOOL isQueueDrained) { | ||
if (dequeuedItem == weakObject) { | ||
didProcessObject = YES; | ||
} | ||
}]; | ||
[queue enqueue:object]; | ||
object = nil; | ||
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:kRunLoopRunTime]]; | ||
XCTAssertFalse(didProcessObject); | ||
} | ||
|
||
- (void)testWeakQueueWithAllDeallocatedObjectsIsDrained | ||
{ | ||
ASRunLoopQueue *queue = [[ASRunLoopQueue alloc] initWithRunLoop:CFRunLoopGetMain() retainObjects:NO handler:nil]; | ||
id object = [[NSObject alloc] init]; | ||
[queue enqueue:object]; | ||
object = nil; | ||
XCTAssertFalse(queue.isEmpty); | ||
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:kRunLoopRunTime]]; | ||
XCTAssertTrue(queue.isEmpty); | ||
} | ||
|
||
@end |
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.
Nit: Please add the PR's URL.
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.
Yup, sorry!