From 2ae0b8fd01c0a3efc32da50166223085eee13e49 Mon Sep 17 00:00:00 2001 From: Mike Bruin Date: Fri, 31 Jan 2025 10:09:40 -0500 Subject: [PATCH] added unit test for persisted attributes --- Agent/Analytics/NRMASAM.mm | 2 +- .../Analytics-Tests/NRMASAMTest.mm | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/Agent/Analytics/NRMASAM.mm b/Agent/Analytics/NRMASAM.mm index 5a31de2a..801c2337 100644 --- a/Agent/Analytics/NRMASAM.mm +++ b/Agent/Analytics/NRMASAM.mm @@ -222,7 +222,7 @@ - (NSString*) sessionAttributeJSONString { + (NSString*) getLastSessionsAttributes { NSError *error; NSString *lastSessionAttributesJsonString = nil; - NSDictionary *lastSessionAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[self attributeFilePath]]; + NSDictionary *lastSessionAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[NRMASAM attributeFilePath]]; NSDictionary *lastSessionPrivateAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[NRMASAM privateAttributeFilePath]]; NSMutableDictionary *mergedDictionary = [NSMutableDictionary dictionary]; diff --git a/Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm b/Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm index fd5707b2..50006b53 100644 --- a/Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm +++ b/Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm @@ -129,6 +129,7 @@ - (void) testIncrementSessionAttribute { error:nil]; XCTAssertTrue([decode[attribute] isEqual:@(2)]); } + - (void) testIncrementSessionAttributeDiffTypes { NSString* attribute = @"incrementableAttribute"; float initialValue = 1.2; @@ -266,6 +267,74 @@ - (void) testClearLastSessionsAnalytics { } +- (void)waitForAttributesToPersist:(NSArray *)expectedAttributes timeout:(NSTimeInterval)timeout { + NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout]; + while ([timeoutDate timeIntervalSinceNow] > 0) { + NSString *attributes = [NRMASAM getLastSessionsAttributes]; + NSDictionary *decode = [NSJSONSerialization JSONObjectWithData:[attributes dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; + + BOOL allAttributesPersisted = YES; + for (NSString *attribute in expectedAttributes) { + if (![decode objectForKey:attribute]) { + allAttributesPersisted = NO; + break; + } + } + + if (allAttributesPersisted) { + return; + } + + // Wait a short period before retrying + [NSThread sleepForTimeInterval:0.1]; + } +} + +- (void)testPersistedSessionAnalytics { + + NSFileManager *fileManager = [NSFileManager defaultManager]; + if([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Attrib_file]]) { + [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Attrib_file] error:nil]; + } + if([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Attrib_file_private]]) { + [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Attrib_file_private] error:nil]; + } + + NSString *attribute = @"blarg"; + NSString *attribute2 = @"blarg2"; + NSString *attribute3 = @"privateBlarg3"; + NSString *attribute4 = @"Blarg4"; + NSString *attribute5 = @"privateBlarg5"; + + // Set attributes + XCTAssertTrue([manager setSessionAttribute:attribute value:@"blurg"], @"Failed to successfully set session attribute"); + XCTAssertTrue([manager setSessionAttribute:attribute2 value:@"blurg2"], @"Failed to successfully set session attribute"); + XCTAssertTrue([manager setNRSessionAttribute:attribute3 value:@"blurg2"], @"Failed to successfully set private session attribute"); + + NSString *attributes = [manager sessionAttributeJSONString]; + NSDictionary *decode = [NSJSONSerialization JSONObjectWithData:[attributes dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; + + XCTAssertEqual(decode.count, 3); + + manager = nil; + // Wait for persistence + [self waitForAttributesToPersist:@[attribute, attribute2, attribute3] timeout:10]; + + manager = [self samTest]; + + XCTAssertTrue([manager setSessionAttribute:attribute4 value:@"blurg"], @"Failed to successfully set session attribute"); + XCTAssertTrue([manager setNRSessionAttribute:attribute5 value:@"blurg2"], @"Failed to successfully set private session attribute"); + + attributes = [manager sessionAttributeJSONString]; + decode = [NSJSONSerialization JSONObjectWithData:[attributes dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; + + XCTAssertTrue([[decode allKeys] containsObject:attribute], @"Should have persisted and new attribute 1."); + XCTAssertTrue([[decode allKeys] containsObject:attribute2], @"Should have persisted and new attribute 2."); + XCTAssertTrue([[decode allKeys] containsObject:attribute3], @"Should have persisted and new private attribute 3."); + XCTAssertTrue([[decode allKeys] containsObject:attribute4], @"Should have persisted and new attribute 4."); + XCTAssertTrue([[decode allKeys] containsObject:attribute5], @"Should have persisted and new private attribute 5."); +} + - (void) testClearPersistedSessionAnalytics { NRMASAM *manager = [self samTest]; NSString *attribute = @"blarg";