forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASRunLoopQueueTests.mm
201 lines (175 loc) · 6.93 KB
/
ASRunLoopQueueTests.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// ASRunLoopQueueTests.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ASTestCase.h"
#import <AsyncDisplayKit/ASRunLoopQueue.h>
#import "ASDisplayNodeTestsHelper.h"
static NSTimeInterval const kRunLoopRunTime = 0.01; // Allow the RunLoop to run for 1/100 second each time.
@interface QueueObject : NSObject <ASCATransactionQueueObserving>
@property (nonatomic) BOOL queueObjectProcessed;
@end
@implementation QueueObject
- (void)prepareForCATransactionCommit
{
self.queueObjectProcessed = YES;
}
@end
@interface ASRunLoopQueueTests : ASTestCase
@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);
}
- (void)testASCATransactionQueueDisable
{
// Disable coalescing.
ASConfiguration *config = [[ASConfiguration alloc] init];
config.experimentalFeatures = kNilOptions;
[ASConfigurationManager test_resetWithConfiguration:config];
ASCATransactionQueue *queue = [[ASCATransactionQueue alloc] init];
QueueObject *object = [[QueueObject alloc] init];
XCTAssertFalse(object.queueObjectProcessed);
[queue enqueue:object];
XCTAssertTrue(object.queueObjectProcessed);
XCTAssertTrue([queue isEmpty]);
XCTAssertFalse(queue.enabled);
}
- (void)testASCATransactionQueueProcess
{
ASConfiguration *config = [[ASConfiguration alloc] initWithDictionary:nil];
config.experimentalFeatures = ASExperimentalInterfaceStateCoalescing;
[ASConfigurationManager test_resetWithConfiguration:config];
ASCATransactionQueue *queue = [[ASCATransactionQueue alloc] init];
QueueObject *object = [[QueueObject alloc] init];
[queue enqueue:object];
XCTAssertFalse(object.queueObjectProcessed);
ASCATransactionQueueWait(queue);
XCTAssertTrue(object.queueObjectProcessed);
XCTAssertTrue(queue.enabled);
}
@end