Skip to content

Commit

Permalink
Changed it to pass the AttributeValidator in as a parameter to the ha…
Browse files Browse the repository at this point in the history
…ndled exception, so it can be replaced for testing
  • Loading branch information
mbruin-NR committed Jan 27, 2025
1 parent ac96dc0 commit a47828e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
4 changes: 3 additions & 1 deletion Agent/General/NewRelicAgentInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#import "NRMAUDIDManager.h"
#import "NRMASupportMetricHelper.h"
#import "NRAutoLogCollector.h"
#import "NRMAAttributeValidator.h"


// Support for teardown and re-setup of the agent within a process lifetime for our test harness
Expand Down Expand Up @@ -568,7 +569,8 @@ - (void) onSessionStart {
sessionStartTime:self.appSessionStartDate
agentConfiguration:self.agentConfiguration
platform:[NewRelicInternalUtils osName]
sessionId:[self currentSessionId]];
sessionId:[self currentSessionId]
attributeValidator:[[NRMAAttributeValidator alloc] init]];

if (status != NotReachable) { // Because we support offline mode check if we're online before sending the handled exceptions
[self.handledExceptionsController processAndPublishPersistedReports];
Expand Down
3 changes: 2 additions & 1 deletion Agent/HandledException/NRMAHandledExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ extern const NSString* kHexBackupStoreFolder;
sessionStartTime:(NSDate*)sessionStartDate
agentConfiguration:(NRMAAgentConfiguration*)agentConfiguration
platform:(NSString*)platform
sessionId:(NSString*)sessionId;
sessionId:(NSString*)sessionId
attributeValidator:(id<AttributeValidatorProtocol>) attributeValidator;

- (void) recordHandledException:(NSException*) exception
attributes:(NSDictionary* _Nullable)attributes;
Expand Down
7 changes: 4 additions & 3 deletions Agent/HandledException/NRMAHandledExceptions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (void) dealloc {

self.sessionId = nil;
self.sessionStartDate = nil;
_attributeValidator = nil;
[_attributeValidator release];

[super dealloc];
}
Expand All @@ -60,7 +60,8 @@ - (instancetype) initWithAnalyticsController:(NRMAAnalytics*)analytics
sessionStartTime:(NSDate*)sessionStartDate
agentConfiguration:(NRMAAgentConfiguration*)agentConfiguration
platform:(NSString*)platform
sessionId:(NSString*)sessionId {
sessionId:(NSString*)sessionId
attributeValidator:(id<AttributeValidatorProtocol>) attributeValidator {
if (analytics == nil || sessionStartDate == nil || [agentConfiguration applicationToken] == nil || platform == nil || sessionId == nil) {
NSMutableArray* missingParams = [[NSMutableArray new] autorelease];
if ([agentConfiguration applicationToken] == nil) [missingParams addObject:@"appToken"];
Expand All @@ -75,7 +76,7 @@ - (instancetype) initWithAnalyticsController:(NRMAAnalytics*)analytics
if (self) {
analyticsParent = analytics;

_attributeValidator = [[NRMAAttributeValidator alloc] init];
_attributeValidator = [attributeValidator retain];

_analytics = std::shared_ptr<NewRelic::AnalyticsController>([analytics analyticsController]);
self.sessionStartDate = sessionStartDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ - (void)setUp {
sessionStartTime:[NSDate date]
agentConfiguration:_agentConfiguration
platform:@"iOS"
sessionId:@"id"];
sessionId:@"id"
attributeValidator:[[NRMAAttributeValidator alloc] init]];

NSUInteger procCount = [[NSProcessInfo processInfo] processorCount];
self.semaphore = dispatch_semaphore_create(procCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "NRMAAgentConfiguration.h"

#import "NewRelicInternalUtils.h"
#import "NRMAAttributeValidator.h"

#import <Hex/HexContext.hpp>
#import "NRAgentTestBase.h"
Expand Down Expand Up @@ -63,14 +64,16 @@ - (void) testBadParams {
sessionStartTime:0
agentConfiguration:nil
platform:nil
sessionId:nil]);
sessionId:nil
attributeValidator:nil]);

NRMAAnalytics* analytics = [[NRMAAnalytics alloc] initWithSessionStartTimeMS:0];
NRMAHandledExceptions* exceptions = [[NRMAHandledExceptions alloc] initWithAnalyticsController:nil
sessionStartTime:0
agentConfiguration:nil
platform:nil
sessionId:nil];
sessionId:nil
attributeValidator:nil];


XCTAssertTrue(exceptions == nil);
Expand All @@ -81,7 +84,8 @@ - (void) testBadParams {
sessionStartTime:0
agentConfiguration:nil
platform:nil
sessionId:nil];
sessionId:nil
attributeValidator:nil];

XCTAssertTrue(exceptions == nil);

Expand All @@ -94,7 +98,8 @@ - (void) testBadParams {
sessionStartTime:0
agentConfiguration:agentConfig
platform:nil
sessionId:nil];
sessionId:nil
attributeValidator:nil];

XCTAssertTrue(exceptions == nil);

Expand All @@ -104,7 +109,8 @@ - (void) testBadParams {
sessionStartTime:0
agentConfiguration:agentConfig
platform:@"iOS"
sessionId:nil];
sessionId:nil
attributeValidator:nil];

XCTAssertTrue(exceptions == nil);

Expand All @@ -119,14 +125,16 @@ - (void) testBadParamsNewEventSystem {
sessionStartTime:0
agentConfiguration:nil
platform:nil
sessionId:nil]);
sessionId:nil
attributeValidator:nil]);

NRMAAnalytics* analytics = [[NRMAAnalytics alloc] initWithSessionStartTimeMS:0];
NRMAHandledExceptions* exceptions = [[NRMAHandledExceptions alloc] initWithAnalyticsController:nil
sessionStartTime:0
agentConfiguration:nil
platform:nil
sessionId:nil];
sessionId:nil
attributeValidator:nil];


XCTAssertTrue(exceptions == nil);
Expand All @@ -137,7 +145,8 @@ - (void) testBadParamsNewEventSystem {
sessionStartTime:0
agentConfiguration:nil
platform:nil
sessionId:nil];
sessionId:nil
attributeValidator:nil];

XCTAssertTrue(exceptions == nil);

Expand All @@ -150,7 +159,8 @@ - (void) testBadParamsNewEventSystem {
sessionStartTime:0
agentConfiguration:agentConfig
platform:nil
sessionId:nil];
sessionId:nil
attributeValidator:nil];

XCTAssertTrue(exceptions == nil);

Expand All @@ -160,7 +170,8 @@ - (void) testBadParamsNewEventSystem {
sessionStartTime:0
agentConfiguration:agentConfig
platform:@"iOS"
sessionId:nil];
sessionId:nil
attributeValidator:nil];

XCTAssertTrue(exceptions == nil);

Expand All @@ -180,7 +191,8 @@ - (void) testHandleException {
sessionStartTime:[NSDate new]
agentConfiguration:agentConfig
platform:[NewRelicInternalUtils osName]
sessionId:@"sessionId"];
sessionId:@"sessionId"
attributeValidator:[[NRMAAttributeValidator alloc] init]];

XCTAssertNoThrow([hexController recordHandledException:[NSException exceptionWithName:@"Hot Tea Exception"
reason:@"the Tea is too hot"
Expand Down Expand Up @@ -211,7 +223,8 @@ - (void) testHandleExceptionWithStackTrace {
sessionStartTime:[NSDate new]
agentConfiguration:agentConfig
platform:[NewRelicInternalUtils osName]
sessionId:@"sessionId"];
sessionId:@"sessionId"
attributeValidator:[[NRMAAttributeValidator alloc] init]];

id dict = @{@"name": @"Exception name not found",
@"reason": @"Reason not found",
Expand All @@ -238,7 +251,8 @@ - (void) testHandleExceptionWithStackTraceNewEventSystem {
sessionStartTime:[NSDate new]
agentConfiguration:agentConfig
platform:[NewRelicInternalUtils osName]
sessionId:@"sessionId"];
sessionId:@"sessionId"
attributeValidator:[[NRMAAttributeValidator alloc] init]];

id dict = @{@"name": @"Exception name not found",
@"reason": @"Reason not found",
Expand Down Expand Up @@ -266,7 +280,8 @@ - (void) testPlatform {
sessionStartTime:[NSDate new]
agentConfiguration:agentConfig
platform:[NewRelicInternalUtils osName]
sessionId:@"sessionId"];
sessionId:@"sessionId"
attributeValidator:[[NRMAAttributeValidator alloc] init]];
XCTAssertTrue([hexController fbsPlatformFromString:@"iOS"] == com::newrelic::mobile::fbs::Platform_iOS, @"Method returned %d, but should be %d", [hexController fbsPlatformFromString:@"iOS"],com::newrelic::mobile::fbs::Platform_iOS );
XCTAssertTrue([hexController fbsPlatformFromString:@"tvOS"] == com::newrelic::mobile::fbs::Platform_tvOS,@"Method returned %d, but should be %d", [hexController fbsPlatformFromString:@"tvOS"],com::newrelic::mobile::fbs::Platform_tvOS);

Expand All @@ -284,7 +299,8 @@ - (void) testDontRecordUnThrownExceptions {
sessionStartTime:[NSDate new]
agentConfiguration:agentConfig
platform:[NewRelicInternalUtils osName]
sessionId:@"sessionId"];
sessionId:@"sessionId"
attributeValidator:[[NRMAAttributeValidator alloc] init]];

id mockLogger = [OCMockObject mockForClass:[NRLogger class]];

Expand Down Expand Up @@ -319,7 +335,8 @@ - (void) testRecordError {
sessionStartTime:[NSDate new]
agentConfiguration:agentConfig
platform:[NewRelicInternalUtils osName]
sessionId:@"sessionId"];
sessionId:@"sessionId"
attributeValidator:[[NRMAAttributeValidator alloc] init]];


NSError* error = [NSError errorWithDomain:@"" code:NSURLErrorUnknown userInfo:@{}];
Expand Down Expand Up @@ -351,7 +368,8 @@ - (void) testRecordErrorNewEventSystem {
sessionStartTime:[NSDate new]
agentConfiguration:agentConfig
platform:[NewRelicInternalUtils osName]
sessionId:@"sessionId"];
sessionId:@"sessionId"
attributeValidator:[[NRMAAttributeValidator alloc] init]];


NSError* error = [NSError errorWithDomain:@"" code:NSURLErrorUnknown userInfo:@{}];
Expand Down

0 comments on commit a47828e

Please sign in to comment.