@@ -129,6 +129,7 @@ - (void) testIncrementSessionAttribute {
129
129
error: nil ];
130
130
XCTAssertTrue ([decode[attribute] isEqual: @(2 )]);
131
131
}
132
+
132
133
- (void ) testIncrementSessionAttributeDiffTypes {
133
134
NSString * attribute = @" incrementableAttribute" ;
134
135
float initialValue = 1.2 ;
@@ -266,6 +267,74 @@ - (void) testClearLastSessionsAnalytics {
266
267
267
268
}
268
269
270
+ - (void )waitForAttributesToPersist : (NSArray <NSString *> *)expectedAttributes timeout : (NSTimeInterval )timeout {
271
+ NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow: timeout];
272
+ while ([timeoutDate timeIntervalSinceNow ] > 0 ) {
273
+ NSString *attributes = [NRMASAM getLastSessionsAttributes ];
274
+ NSDictionary *decode = [NSJSONSerialization JSONObjectWithData: [attributes dataUsingEncoding: NSUTF8StringEncoding] options: 0 error: nil ];
275
+
276
+ BOOL allAttributesPersisted = YES ;
277
+ for (NSString *attribute in expectedAttributes) {
278
+ if (![decode objectForKey: attribute]) {
279
+ allAttributesPersisted = NO ;
280
+ break ;
281
+ }
282
+ }
283
+
284
+ if (allAttributesPersisted) {
285
+ return ;
286
+ }
287
+
288
+ // Wait a short period before retrying
289
+ [NSThread sleepForTimeInterval: 0.1 ];
290
+ }
291
+ }
292
+
293
+ - (void )testPersistedSessionAnalytics {
294
+
295
+ NSFileManager *fileManager = [NSFileManager defaultManager ];
296
+ if ([fileManager fileExistsAtPath: [NSString stringWithFormat: @" %@ /%@ " ,[NewRelicInternalUtils getStorePath ],kNRMA_Attrib_file ]]) {
297
+ [fileManager removeItemAtPath: [NSString stringWithFormat: @" %@ /%@ " ,[NewRelicInternalUtils getStorePath ],kNRMA_Attrib_file ] error: nil ];
298
+ }
299
+ if ([fileManager fileExistsAtPath: [NSString stringWithFormat: @" %@ /%@ " ,[NewRelicInternalUtils getStorePath ],kNRMA_Attrib_file_private ]]) {
300
+ [fileManager removeItemAtPath: [NSString stringWithFormat: @" %@ /%@ " ,[NewRelicInternalUtils getStorePath ],kNRMA_Attrib_file_private ] error: nil ];
301
+ }
302
+
303
+ NSString *attribute = @" blarg" ;
304
+ NSString *attribute2 = @" blarg2" ;
305
+ NSString *attribute3 = @" privateBlarg3" ;
306
+ NSString *attribute4 = @" Blarg4" ;
307
+ NSString *attribute5 = @" privateBlarg5" ;
308
+
309
+ // Set attributes
310
+ XCTAssertTrue ([manager setSessionAttribute: attribute value: @" blurg" ], @" Failed to successfully set session attribute" );
311
+ XCTAssertTrue ([manager setSessionAttribute: attribute2 value: @" blurg2" ], @" Failed to successfully set session attribute" );
312
+ XCTAssertTrue ([manager setNRSessionAttribute: attribute3 value: @" blurg2" ], @" Failed to successfully set private session attribute" );
313
+
314
+ NSString *attributes = [manager sessionAttributeJSONString ];
315
+ NSDictionary *decode = [NSJSONSerialization JSONObjectWithData: [attributes dataUsingEncoding: NSUTF8StringEncoding] options: 0 error: nil ];
316
+
317
+ XCTAssertEqual (decode.count , 3 );
318
+
319
+ manager = nil ;
320
+ // Wait for persistence
321
+ [self waitForAttributesToPersist: @[attribute, attribute2, attribute3] timeout: 10 ];
322
+
323
+ manager = [self samTest ];
324
+
325
+ XCTAssertTrue ([manager setSessionAttribute: attribute4 value: @" blurg" ], @" Failed to successfully set session attribute" );
326
+ XCTAssertTrue ([manager setNRSessionAttribute: attribute5 value: @" blurg2" ], @" Failed to successfully set private session attribute" );
327
+
328
+ attributes = [manager sessionAttributeJSONString ];
329
+ decode = [NSJSONSerialization JSONObjectWithData: [attributes dataUsingEncoding: NSUTF8StringEncoding] options: 0 error: nil ];
330
+
331
+ XCTAssertTrue ([[decode allKeys ] containsObject: attribute], @" Should have persisted and new attribute 1." );
332
+ XCTAssertTrue ([[decode allKeys ] containsObject: attribute2], @" Should have persisted and new attribute 2." );
333
+ XCTAssertTrue ([[decode allKeys ] containsObject: attribute3], @" Should have persisted and new private attribute 3." );
334
+ XCTAssertTrue ([[decode allKeys ] containsObject: attribute4], @" Should have persisted and new attribute 4." );
335
+ XCTAssertTrue ([[decode allKeys ] containsObject: attribute5], @" Should have persisted and new private attribute 5." );
336
+ }
337
+
269
338
- (void ) testClearPersistedSessionAnalytics {
270
339
NRMASAM *manager = [self samTest ];
271
340
NSString *attribute = @" blarg" ;
0 commit comments