27
27
#import " GDLTestBackend.h"
28
28
#import " GDLTestPrioritizer.h"
29
29
30
+ #import " GDLAssertHelper.h"
30
31
#import " GDLLogStorage+Testing.h"
31
32
#import " GDLRegistrar+Testing.h"
32
33
@@ -45,13 +46,15 @@ @interface GDLLogStorageTest : GDLTestCase
45
46
@implementation GDLLogStorageTest
46
47
47
48
- (void )setUp {
49
+ [super setUp ];
48
50
self.testBackend = [[GDLTestBackend alloc ] init ];
49
51
self.testPrioritizer = [[GDLTestPrioritizer alloc ] init ];
50
52
[[GDLRegistrar sharedInstance ] registerBackend: _testBackend forLogTarget: logTarget];
51
53
[[GDLRegistrar sharedInstance ] registerLogPrioritizer: _testPrioritizer forLogTarget: logTarget];
52
54
}
53
55
54
56
- (void )tearDown {
57
+ [super tearDown ];
55
58
// Destroy these objects before the next test begins.
56
59
self.testBackend = nil ;
57
60
self.testPrioritizer = nil ;
@@ -109,9 +112,84 @@ - (void)testRemoveLog {
109
112
});
110
113
}
111
114
115
+ /* * Tests storing a few different logs. */
116
+ - (void )testStoreMultipleLogs {
117
+ NSUInteger log1Hash, log2Hash, log3Hash;
118
+
119
+ // logEvents are autoreleased, and the pool needs to drain.
120
+ @autoreleasepool {
121
+ GDLLogEvent *logEvent = [[GDLLogEvent alloc ] initWithLogMapID: @" 404" logTarget: logTarget];
122
+ logEvent.extensionBytes = [@" testString1" dataUsingEncoding: NSUTF8StringEncoding];
123
+ log1Hash = logEvent.hash ;
124
+ XCTAssertNoThrow ([[GDLLogStorage sharedInstance ] storeLog: logEvent]);
125
+
126
+ logEvent = [[GDLLogEvent alloc ] initWithLogMapID: @" 100" logTarget: logTarget];
127
+ logEvent.extensionBytes = [@" testString2" dataUsingEncoding: NSUTF8StringEncoding];
128
+ log2Hash = logEvent.hash ;
129
+ XCTAssertNoThrow ([[GDLLogStorage sharedInstance ] storeLog: logEvent]);
130
+
131
+ logEvent = [[GDLLogEvent alloc ] initWithLogMapID: @" 404" logTarget: logTarget];
132
+ logEvent.extensionBytes = [@" testString3" dataUsingEncoding: NSUTF8StringEncoding];
133
+ log3Hash = logEvent.hash ;
134
+ XCTAssertNoThrow ([[GDLLogStorage sharedInstance ] storeLog: logEvent]);
135
+ }
136
+ dispatch_sync ([GDLLogStorage sharedInstance ].storageQueue , ^{
137
+ XCTAssertEqual ([GDLLogStorage sharedInstance ].logHashToLogFile .count , 3 );
138
+ XCTAssertEqual ([GDLLogStorage sharedInstance ].logTargetToLogFileSet [@(logTarget)].count , 3 );
139
+
140
+ NSURL *log1File = [GDLLogStorage sharedInstance ].logHashToLogFile [@(log1Hash)];
141
+ XCTAssertNotNil (log1File);
142
+ XCTAssertTrue ([[NSFileManager defaultManager ] fileExistsAtPath: log1File.path]);
143
+ NSError *error;
144
+ XCTAssertTrue ([[NSFileManager defaultManager ] removeItemAtURL: log1File error: &error]);
145
+ XCTAssertNil (error, @" There was an error deleting the logFile: %@ " , error);
146
+
147
+ NSURL *log2File = [GDLLogStorage sharedInstance ].logHashToLogFile [@(log2Hash)];
148
+ XCTAssertNotNil (log2File);
149
+ XCTAssertTrue ([[NSFileManager defaultManager ] fileExistsAtPath: log2File.path]);
150
+ error = nil ;
151
+ XCTAssertTrue ([[NSFileManager defaultManager ] removeItemAtURL: log2File error: &error]);
152
+ XCTAssertNil (error, @" There was an error deleting the logFile: %@ " , error);
153
+
154
+ NSURL *log3File = [GDLLogStorage sharedInstance ].logHashToLogFile [@(log3Hash)];
155
+ XCTAssertNotNil (log3File);
156
+ XCTAssertTrue ([[NSFileManager defaultManager ] fileExistsAtPath: log3File.path]);
157
+ error = nil ;
158
+ XCTAssertTrue ([[NSFileManager defaultManager ] removeItemAtURL: log3File error: &error]);
159
+ XCTAssertNil (error, @" There was an error deleting the logFile: %@ " , error);
160
+ });
161
+ }
162
+
112
163
/* * Tests enforcing that a log prioritizer does not retain a log in memory. */
113
164
- (void )testLogEventDeallocationIsEnforced {
114
- // TODO
165
+ XCTestExpectation *errorExpectation = [self expectationWithDescription: @" log retain error" ];
166
+ [GDLAssertHelper setAssertionBlock: ^{
167
+ [errorExpectation fulfill ];
168
+ }];
169
+
170
+ // logEvent is referenced past -storeLog, ensuring it's retained, which should assert.
171
+ GDLLogEvent *logEvent = [[GDLLogEvent alloc ] initWithLogMapID: @" 404" logTarget: logTarget];
172
+ logEvent.extensionBytes = [@" testString" dataUsingEncoding: NSUTF8StringEncoding];
173
+
174
+ // Store the log and wait for the expectation.
175
+ [[GDLLogStorage sharedInstance ] storeLog: logEvent];
176
+ [self waitForExpectations: @[ errorExpectation ] timeout: 5.0 ];
177
+
178
+ NSURL *logFile;
179
+ logFile = [GDLLogStorage sharedInstance ].logHashToLogFile [@(logEvent.hash)];
180
+
181
+ // This isn't strictly necessary because of the -waitForExpectations above.
182
+ dispatch_sync ([GDLLogStorage sharedInstance ].storageQueue , ^{
183
+ XCTAssertTrue ([[NSFileManager defaultManager ] fileExistsAtPath: logFile.path]);
184
+ });
185
+
186
+ // Ensure log was removed.
187
+ [[GDLLogStorage sharedInstance ] removeLog: @(logEvent.hash) logTarget: @(logTarget)];
188
+ dispatch_sync ([GDLLogStorage sharedInstance ].storageQueue , ^{
189
+ XCTAssertFalse ([[NSFileManager defaultManager ] fileExistsAtPath: logFile.path]);
190
+ XCTAssertEqual ([GDLLogStorage sharedInstance ].logHashToLogFile .count , 0 );
191
+ XCTAssertEqual ([GDLLogStorage sharedInstance ].logTargetToLogFileSet [@(logTarget)].count , 0 );
192
+ });
115
193
}
116
194
117
195
/* * Tests encoding and decoding the storage singleton correctly. */
0 commit comments