Skip to content

Commit 2ae0b8f

Browse files
committed
added unit test for persisted attributes
1 parent 840c8ad commit 2ae0b8f

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

Agent/Analytics/NRMASAM.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ - (NSString*) sessionAttributeJSONString {
222222
+ (NSString*) getLastSessionsAttributes {
223223
NSError *error;
224224
NSString *lastSessionAttributesJsonString = nil;
225-
NSDictionary *lastSessionAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[self attributeFilePath]];
225+
NSDictionary *lastSessionAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[NRMASAM attributeFilePath]];
226226
NSDictionary *lastSessionPrivateAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[NRMASAM privateAttributeFilePath]];
227227

228228
NSMutableDictionary *mergedDictionary = [NSMutableDictionary dictionary];

Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ - (void) testIncrementSessionAttribute {
129129
error:nil];
130130
XCTAssertTrue([decode[attribute] isEqual:@(2)]);
131131
}
132+
132133
- (void) testIncrementSessionAttributeDiffTypes {
133134
NSString* attribute = @"incrementableAttribute";
134135
float initialValue = 1.2;
@@ -266,6 +267,74 @@ - (void) testClearLastSessionsAnalytics {
266267

267268
}
268269

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+
269338
- (void) testClearPersistedSessionAnalytics {
270339
NRMASAM *manager = [self samTest];
271340
NSString *attribute = @"blarg";

0 commit comments

Comments
 (0)