PageRenderTime 81ms CodeModel.GetById 11ms app.highlight 65ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/update-engine/Common/KSStatsCollectionTest.m

http://macfuse.googlecode.com/
Objective C | 240 lines | 156 code | 45 blank | 39 comment | 21 complexity | a6b9efede9e110b16a50853e3dd00b4c MD5 | raw file
  1// Copyright 2008 Google Inc.
  2//
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6//
  7//     http://www.apache.org/licenses/LICENSE-2.0
  8//
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15#import <SenTestingKit/SenTestingKit.h>
 16#import "KSStatsCollection.h"
 17#import "KSUUID.h"
 18
 19
 20@interface KSStatsCollectionTest : SenTestCase
 21@end
 22
 23
 24@implementation KSStatsCollectionTest
 25
 26- (void)testCreation {
 27  KSStatsCollection *stats = nil;
 28  
 29  stats = [[[KSStatsCollection alloc] init] autorelease];
 30  STAssertNil(stats, nil);
 31  
 32  stats = [KSStatsCollection statsCollectionWithPath:nil];
 33  STAssertNil(stats, nil);
 34
 35  NSString *tempPath = @"/tmp/qwedzsrzstzw";
 36  stats = [KSStatsCollection statsCollectionWithPath:tempPath];
 37  STAssertNotNil(stats, nil);
 38  STAssertEqualObjects(tempPath, [stats path], nil);
 39  STAssertTrue([stats autoSynchronize], nil);
 40  STAssertTrue([[stats description] length] > 1, nil);
 41  
 42  [stats setAutoSynchronize:NO];
 43  STAssertFalse([stats autoSynchronize], nil);
 44  
 45  [[NSFileManager defaultManager] removeFileAtPath:tempPath handler:nil];
 46}
 47
 48- (void)testIntegerStats {
 49  NSString *path = @"/dev/null";
 50  KSStatsCollection *stats = [KSStatsCollection statsCollectionWithPath:path
 51                                                        autoSynchronize:NO];
 52  STAssertNotNil(stats, nil);
 53  STAssertTrue([stats count] == 0, nil);
 54  
 55  //
 56  // Test -setNumber:forStat
 57  //
 58  STAssertNil([stats numberForStat:@"test1"], nil);
 59  [stats setNumber:[NSNumber numberWithInt:3] forStat:@"test1"];
 60  STAssertNotNil([stats numberForStat:@"test1"], nil);
 61  STAssertEqualObjects([stats numberForStat:@"test1"], [NSNumber numberWithInt:3], nil);
 62  STAssertTrue([stats count] == 1, nil);
 63  
 64  //
 65  // Test -incrementStat:
 66  //
 67  STAssertNil([stats numberForStat:@"test2"], nil);
 68  [stats incrementStat:@"test2"];
 69  STAssertNotNil([stats numberForStat:@"test2"], nil);
 70  STAssertEqualObjects([stats numberForStat:@"test2"], [NSNumber numberWithInt:1], nil);
 71  STAssertTrue([stats count] == 2, nil);
 72  [stats incrementStat:@"test2"];
 73  [stats incrementStat:@"test2"];
 74  STAssertEqualObjects([stats numberForStat:@"test2"], [NSNumber numberWithInt:3], nil);
 75  STAssertTrue([stats count] == 2, nil);
 76
 77  //
 78  // Test -decrementStat:
 79  //
 80  STAssertNil([stats numberForStat:@"test3"], nil);
 81  [stats decrementStat:@"test3"];
 82  STAssertNotNil([stats numberForStat:@"test3"], nil);
 83  STAssertEqualObjects([stats numberForStat:@"test3"], [NSNumber numberWithInt:-1], nil);
 84  STAssertTrue([stats count] == 3, nil);
 85  [stats decrementStat:@"test3"];
 86  [stats decrementStat:@"test3"];
 87  STAssertEqualObjects([stats numberForStat:@"test3"], [NSNumber numberWithInt:-3], nil);
 88  STAssertTrue([stats count] == 3, nil);
 89  
 90  //
 91  // Test -incrementStat:by:
 92  //
 93  STAssertNil([stats numberForStat:@"test4"], nil);
 94  [stats incrementStat:@"test4" by:1];
 95  STAssertNotNil([stats numberForStat:@"test4"], nil);
 96  STAssertEqualObjects([stats numberForStat:@"test4"], [NSNumber numberWithInt:1], nil);
 97  [stats incrementStat:@"test4" by:0];
 98  STAssertEqualObjects([stats numberForStat:@"test4"], [NSNumber numberWithInt:1], nil);
 99  [stats incrementStat:@"test4" by:2];
100  STAssertEqualObjects([stats numberForStat:@"test4"], [NSNumber numberWithInt:3], nil);
101  [stats incrementStat:@"test4" by:-2];
102  STAssertEqualObjects([stats numberForStat:@"test4"], [NSNumber numberWithInt:1], nil);
103  STAssertTrue([stats count] == 4, nil);
104  
105  //
106  // Test -decrementStat:by:
107  //
108  STAssertNil([stats numberForStat:@"test5"], nil);
109  [stats decrementStat:@"test5" by:1];
110  STAssertNotNil([stats numberForStat:@"test5"], nil);
111  STAssertEqualObjects([stats numberForStat:@"test5"], [NSNumber numberWithInt:-1], nil);
112  [stats decrementStat:@"test5" by:2];
113  STAssertEqualObjects([stats numberForStat:@"test5"], [NSNumber numberWithInt:-3], nil);
114  [stats decrementStat:@"test5" by:-3];
115  STAssertEqualObjects([stats numberForStat:@"test5"], [NSNumber numberWithInt:0], nil);
116  STAssertTrue([stats count] == 5, nil);
117  
118  NSDictionary *expect = [NSDictionary dictionaryWithObjectsAndKeys:
119                          [NSNumber numberWithInt:3], @"test1",
120                          [NSNumber numberWithInt:3], @"test2",
121                          [NSNumber numberWithInt:-3], @"test3",
122                          [NSNumber numberWithInt:1], @"test4",
123                          [NSNumber numberWithInt:0], @"test5",
124                          nil];
125  
126  NSDictionary *dict = [stats statsDictionary];
127  STAssertEqualObjects(dict, expect, nil);
128  
129  // Make sure our path is /dev/null and that synchronization fails (because we
130  // can't atomically write to /dev/null).
131  STAssertEqualObjects([stats path], @"/dev/null", nil);
132  STAssertFalse([stats synchronize], nil);
133  
134  STAssertTrue([stats count] == 5, nil);
135  [stats removeAllStats];
136  STAssertTrue([stats count] == 0, nil);
137}
138
139- (void)testFloatStats {
140  NSString *path = @"/dev/null";
141  KSStatsCollection *stats = [KSStatsCollection statsCollectionWithPath:path
142                                                        autoSynchronize:NO];
143  STAssertNotNil(stats, nil);
144  STAssertTrue([stats count] == 0, nil);
145  
146  STAssertNil([stats numberForStat:@"test1"], nil);
147  [stats setNumber:[NSNumber numberWithFloat:3.2] forStat:@"test1"];
148  STAssertNotNil([stats numberForStat:@"test1"], nil);
149  STAssertEqualObjects([stats numberForStat:@"test1"], [NSNumber numberWithFloat:3.2], nil);
150  STAssertTrue([stats count] == 1, nil);
151  
152  NSDictionary *expect = [NSDictionary dictionaryWithObjectsAndKeys:
153                          [NSNumber numberWithFloat:3.2], @"test1",
154                          nil];
155  
156  NSDictionary *dict = [stats statsDictionary];
157  STAssertEqualObjects(dict, expect, nil);
158  
159  // Make sure our path is /dev/null and that synchronization fails (because we
160  // can't atomically write to /dev/null).
161  STAssertEqualObjects([stats path], @"/dev/null", nil);
162  STAssertFalse([stats synchronize], nil);
163  
164  STAssertTrue([stats count] == 1, nil);
165  [stats removeAllStats];
166  STAssertTrue([stats count] == 0, nil);
167}
168
169- (void)testSynchronizingStats {
170  NSString *path = [NSString stringWithFormat:@"/tmp/%@.stats_unittest",
171                    [KSUUID uuidString]];
172  
173  BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
174  STAssertFalse(exists, nil);
175  
176  KSStatsCollection *stats = [KSStatsCollection statsCollectionWithPath:path
177                                                        autoSynchronize:NO];
178  STAssertNotNil(stats, nil);
179  STAssertTrue([stats count] == 0, nil);
180  
181  [stats incrementStat:@"foo"];
182  
183  // After setting one stat, the file still should not exist because we set
184  // auto syncing to NO.
185  exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
186  STAssertFalse(exists, nil);
187  
188  // Now we'll synchronize, and make sure the stats file gets created
189  STAssertTrue([stats synchronize], nil);
190  exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
191  STAssertTrue(exists, nil);
192  
193  // Read the stats in from disk, and make sure |stats2| is the same as |stats|.
194  KSStatsCollection *stats2 = [KSStatsCollection statsCollectionWithPath:path];
195  STAssertEqualObjects([stats2 statsDictionary], [stats statsDictionary], nil);
196  
197  STAssertTrue([stats2 count] == 1, nil);
198  [stats2 removeAllStats];
199  STAssertTrue([stats2 count] == 0, nil);
200  
201  // Cleanup
202  STAssertTrue([[NSFileManager defaultManager] removeFileAtPath:path handler:nil], nil);
203  exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
204  STAssertFalse(exists, nil);
205}
206
207- (void)testAutoSynchronizingStats {
208  NSString *path = [NSString stringWithFormat:@"/tmp/%@.stats_unittest",
209                    [KSUUID uuidString]];
210  
211  BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
212  STAssertFalse(exists, nil);
213  
214  KSStatsCollection *stats = [KSStatsCollection statsCollectionWithPath:path];
215  STAssertNotNil(stats, nil);
216  STAssertTrue([stats count] == 0, nil);
217  
218  [stats incrementStat:@"foo"];
219  [stats incrementStat:@"bar"];
220  [stats incrementStat:@"bar"];
221  
222  exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
223  STAssertTrue(exists, nil);
224  
225  // Read the stats in from disk, and make sure |stats2| is the same as |stats|.
226  KSStatsCollection *stats2 = [KSStatsCollection statsCollectionWithPath:path];
227  STAssertTrue([stats count] == [stats2 count], nil);
228  STAssertEqualObjects([stats2 statsDictionary], [stats statsDictionary], nil);
229  
230  STAssertTrue([stats2 count] == 2, nil);
231  [stats2 removeAllStats];
232  STAssertTrue([stats2 count] == 0, nil);
233  
234  // Cleanup
235  STAssertTrue([[NSFileManager defaultManager] removeFileAtPath:path handler:nil], nil);
236  exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
237  STAssertFalse(exists, nil);
238}
239
240@end