1/* 2 * Copyright (c) 2011 Apple Inc. All rights reserved. 3 * 4 * @APPLE_APACHE_LICENSE_HEADER_START@ 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * @APPLE_APACHE_LICENSE_HEADER_END@ 19 */ 20// 21// ResourceTracking.m 22// Copyright (c) 2009-2011 Apple Inc. All rights reserved. 23// 24 25#import "BlackBoxTest.h" 26 27static BOOL shouldCollect; 28 29@interface ResourceTracking : BlackBoxTest 30{ 31 BOOL _finalized; 32 BOOL _collected; 33 BOOL _resourceTrackerQueried; 34} 35- (void)verifyNoCollection; 36- (void)verifyCollection; 37@end 38 39@implementation ResourceTracking 40 41static char *tracker_description = "resource tracker unit test"; 42 43- (BOOL)shouldCollect 44{ 45 boolean_t collect = shouldCollect; 46 _resourceTrackerQueried = YES; 47 shouldCollect = NO; 48 return collect; 49} 50 51- (void)processOutputLine:(NSString *)line 52{ 53 NSString *expectedString = @"triggering collection due to external resource tracker: resource tracker unit test"; 54 NSRange r = [line rangeOfString:expectedString]; 55 if (r.location == NSNotFound) { 56 [super processOutputLine:line]; 57 } 58} 59 60- (void)allocate 61{ 62 // force test object out of thread local set 63 CFRelease(CFRetain([TestFinalizer new])); 64} 65 66- (void)performTest 67{ 68 auto_zone_register_resource_tracker([self auto_zone], tracker_description, ^{ 69 return (boolean_t)[self shouldCollect]; 70 }); 71 shouldCollect = NO; 72 _finalized = NO; 73 _resourceTrackerQueried = NO; 74 75 [self allocate]; 76 77 [NSThread sleepForTimeInterval:0.3]; // wait long enough that the collector will poll the resource tracker 78 auto_zone_collect_and_notify([self auto_zone], AUTO_ZONE_COLLECT_NO_OPTIONS, _testQueue, ^{ [self verifyNoCollection]; } ); // should not collect 79} 80 81- (void)verifyNoCollection 82{ 83 if (_finalized) { 84 [self fail:@"unexpected collection"]; 85 } 86 if (!_resourceTrackerQueried) { 87 [self fail:@"resource tracker was not queried (no collection)"]; 88 } 89 90 shouldCollect = YES; 91 _resourceTrackerQueried = NO; 92 [NSThread sleepForTimeInterval:0.3]; // wait long enough that the collector will poll the resource tracker 93 auto_zone_collect_and_notify([self auto_zone], AUTO_ZONE_COLLECT_NO_OPTIONS, _testQueue, ^{ [self verifyCollection]; } ); // should collect 94} 95 96- (void)verifyCollection 97{ 98 if (!_resourceTrackerQueried) { 99 [self fail:@"resource tracker was not queried (collection)"]; 100 } 101 if (_finalized) { 102 [self passed]; 103 } else { 104 [self fail:@"did not collect"]; 105 } 106 auto_zone_unregister_resource_tracker([self auto_zone], tracker_description); 107 [self testFinished]; 108} 109 110- (void)didFinalize:(TestFinalizer *)finalizer 111{ 112 _finalized = YES; 113} 114 115@end 116