@@ -23,9 +23,33 @@ @protocol MTRDelegateManagerTestsDelegate <NSObject>
2323@end
2424
2525@interface MTRDelegateManagerTestsDelegateImpl : NSObject <MTRDelegateManagerTestsDelegate>
26+ - (instancetype )initWithDeallocExpectation : (XCTestExpectation *)deallocExpectation ;
27+ @property (nonatomic , readonly , nullable ) XCTestExpectation * deallocExpectation;
2628@end
2729
2830@implementation MTRDelegateManagerTestsDelegateImpl
31+ - (instancetype )init
32+ {
33+ self = [super init ];
34+ NSLog (@" Initializing delegate %p " , self);
35+ return self;
36+ }
37+
38+ - (instancetype )initWithDeallocExpectation : (XCTestExpectation *)deallocExpectation
39+ {
40+ self = [super init ];
41+ NSLog (@" Initializing delegate %p " , self);
42+ _deallocExpectation = deallocExpectation;
43+ return self;
44+ }
45+
46+ - (void )dealloc
47+ {
48+ NSLog (@" Deallocating delegate %p " , self);
49+ if (_deallocExpectation) {
50+ [_deallocExpectation fulfill ];
51+ }
52+ }
2953@end
3054
3155@interface MTRDelegateManagerTestsDelegateInfo : MTRDelegateInfo <id <MTRDelegateManagerTestsDelegate>>
@@ -151,8 +175,9 @@ - (void)testCall
151175 __auto_type * info3 = [[MTRDelegateManagerTestsDelegateInfo alloc ] initWithDelegate: delegate3 queue: queue3];
152176
153177 MTRDelegateManagerTestsDelegateInfo * info1;
178+ __auto_type * delegate1DeallocExpectation = [self expectationWithDescription: @" Delegate 1 deallocated" ];
154179 @autoreleasepool {
155- __auto_type * delegate1 = [[MTRDelegateManagerTestsDelegateImpl alloc ] init ];
180+ __auto_type * delegate1 = [[MTRDelegateManagerTestsDelegateImpl alloc ] initWithDeallocExpectation: delegate1DeallocExpectation ];
156181 info1 = [[MTRDelegateManagerTestsDelegateInfo alloc ] initWithDelegate: delegate1 queue: dispatch_get_main_queue ()];
157182
158183 [manager addDelegateInfo: info1];
@@ -173,19 +198,18 @@ - (void)testCall
173198 dispatch_assert_queue (queue3);
174199 [expectation3 fulfill ];
175200 } else {
176- XCTFail (" Unexpected delegate %@: " , delegate);
201+ XCTFail (" Unexpected delegate: %@" , delegate);
177202 }
178203 }];
179204
180- [self waitForExpectations: @[ expectation1, expectation3, expectation2 ]]; // NOTE: No particular ordering required
205+ [self waitForExpectations: @[ expectation1, expectation3, expectation2 ] timeout: 3 ]; // NOTE: No particular ordering required
181206 }
182207
183208 // Now delegate1 should go away.
209+ [self waitForExpectations: @[ delegate1DeallocExpectation ] timeout: 3 ];
184210
185- __auto_type * expectation1 = [self expectationWithDescription: @" Delegate 1 again" ];
186211 __auto_type * expectation2 = [self expectationWithDescription: @" Delegate 2 again" ];
187212 __auto_type * expectation3 = [self expectationWithDescription: @" Delegate 3 again" ];
188- expectation1.inverted = YES ;
189213 [manager callDelegatesWithBlock: ^(MTRDelegateManagerTestsDelegateImpl * delegate) {
190214 if (delegate == delegate2) {
191215 dispatch_assert_queue (dispatch_get_main_queue ());
@@ -194,12 +218,11 @@ - (void)testCall
194218 dispatch_assert_queue (queue3);
195219 [expectation3 fulfill ];
196220 } else {
197- dispatch_assert_queue (dispatch_get_main_queue ());
198- [expectation1 fulfill ];
221+ XCTFail (" Unexpected delegate: %@" , delegate);
199222 }
200223 }];
201224
202- [self waitForExpectations: @[ expectation3, expectation2, expectation1 ] timeout: 3 ]; // NOTE: No particular ordering required
225+ [self waitForExpectations: @[ expectation3, expectation2 ] timeout: 3 ]; // NOTE: No particular ordering required
203226}
204227
205228@end
0 commit comments