Skip to content

Commit

Permalink
added unit test for persisted attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruin-NR committed Jan 31, 2025
1 parent 840c8ad commit 2ae0b8f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Agent/Analytics/NRMASAM.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
69 changes: 69 additions & 0 deletions Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ - (void) testIncrementSessionAttribute {
error:nil];
XCTAssertTrue([decode[attribute] isEqual:@(2)]);
}

- (void) testIncrementSessionAttributeDiffTypes {
NSString* attribute = @"incrementableAttribute";
float initialValue = 1.2;
Expand Down Expand Up @@ -266,6 +267,74 @@ - (void) testClearLastSessionsAnalytics {

}

- (void)waitForAttributesToPersist:(NSArray<NSString *> *)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");

Check failure on line 325 in Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm

View workflow job for this annotation

GitHub Actions / TestWatchOS

testPersistedSessionAnalytics, (([manager setSessionAttribute:attribute4 value:@"blurg"]) is true) failed - 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.");

Check failure on line 331 in Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm

View workflow job for this annotation

GitHub Actions / TestWatchOS

testPersistedSessionAnalytics, (([[decode allKeys] containsObject:attribute]) is true) failed - Should have persisted and new attribute 1.
XCTAssertTrue([[decode allKeys] containsObject:attribute2], @"Should have persisted and new attribute 2.");

Check failure on line 332 in Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm

View workflow job for this annotation

GitHub Actions / TestWatchOS

testPersistedSessionAnalytics, (([[decode allKeys] containsObject:attribute2]) is true) failed - Should have persisted and new attribute 2.
XCTAssertTrue([[decode allKeys] containsObject:attribute3], @"Should have persisted and new private attribute 3.");

Check failure on line 333 in Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm

View workflow job for this annotation

GitHub Actions / TestWatchOS

testPersistedSessionAnalytics, (([[decode allKeys] containsObject:attribute3]) is true) failed - Should have persisted and new private attribute 3.
XCTAssertTrue([[decode allKeys] containsObject:attribute4], @"Should have persisted and new attribute 4.");

Check failure on line 334 in Tests/Unit-Tests/NewRelicAgentTests/Analytics-Tests/NRMASAMTest.mm

View workflow job for this annotation

GitHub Actions / TestWatchOS

testPersistedSessionAnalytics, (([[decode allKeys] containsObject:attribute4]) is true) failed - 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";
Expand Down

0 comments on commit 2ae0b8f

Please sign in to comment.