Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NR-186415 rewrote the persistence in the new event system #190

Closed
wants to merge 11 commits into from
36 changes: 19 additions & 17 deletions Agent/Analytics/PersistentEventStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#import "NRLogger.h"

@interface PersistentEventStore ()
@property (strong) NSOperationQueue *workQueue;
@end

@implementation PersistentEventStore {
Expand All @@ -19,6 +18,8 @@ @implementation PersistentEventStore {
NSTimeInterval _minimumDelay;
NSDate *_lastSave;
BOOL _dirty;

dispatch_queue_t workQueue;
}

- (nonnull instancetype)initWithFilename:(NSString *)filename
Expand All @@ -30,8 +31,7 @@ - (nonnull instancetype)initWithFilename:(NSString *)filename
_minimumDelay = secondsDelay;
_lastSave = [NSDate new];
_dirty = NO;
_workQueue = [[NSOperationQueue alloc] init];
[_workQueue setMaxConcurrentOperationCount:1];
workQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
}
return self;
}
Expand All @@ -56,10 +56,10 @@ - (void)setObject:(nonnull id)object forKey:(nonnull id)key {
}

__block PersistentEventStore *blockSafeSelf = self;
[_workQueue addOperationWithBlock:^{
dispatch_sync(workQueue, ^{
NRLOG_VERBOSE(@"Entered Save Block");
[blockSafeSelf saveFile];
}];
});
}

- (void)removeObjectForKey:(id)key {
Expand All @@ -70,11 +70,10 @@ - (void)removeObjectForKey:(id)key {
}

__block PersistentEventStore *blockSafeSelf = self;
[_workQueue addOperationWithBlock:^{
dispatch_sync(workQueue, ^{
NRLOG_VERBOSE(@"Entered Remove Block");
[blockSafeSelf saveFile];
}];

});
}

- (nullable id)objectForKey:(nonnull id)key {
Expand All @@ -87,10 +86,10 @@ - (void)clearAll {
NRLOG_VERBOSE(@"Marked dirty for clearing");
}
__block PersistentEventStore *blockSafeSelf = self;
[_workQueue addOperationWithBlock:^{
dispatch_sync(workQueue, ^{
NRLOG_VERBOSE(@"Entered Remove Block");
[blockSafeSelf removeFile];
}];
});
}

- (void)removeFile {
Expand Down Expand Up @@ -162,13 +161,16 @@ - (void)saveToFile {
}

- (NSDictionary *)getLastSessionEvents {
NSData *storedData;
NSError * __autoreleasing *error = nil;
@synchronized (self) {
storedData = [NSData dataWithContentsOfFile:_filename
options:0
error:error];
}
__block NSData *storedData;
__block NSError * __autoreleasing *error = nil;
__block PersistentEventStore *blockSafeSelf = self;
dispatch_sync(workQueue, ^{
@synchronized (self) {
storedData = [NSData dataWithContentsOfFile:blockSafeSelf->_filename
options:0
error:error];
}
});
if(storedData == nil) {
if(error != NULL && *error != nil) {
NRLOG_ERROR(@"Error getting last sessions saved events: %@", *error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1065,31 +1065,38 @@ - (void) testJSONEscapeCharacters {

- (void) testDuplicateStore {
//todo: reenable test (disabled for beta 1, no persistent store)
// NRMAAnalytics* analytics = [[NRMAAnalytics alloc] initWithSessionStartTimeMS:0];
// [analytics setSessionAttribute:@"12345" value:@5];
// [analytics addEventNamed:@"blah" withAttributes:@{@"pew pew":@"asdf"}];
// [analytics setSessionAttribute:@"test" value:@"hello"];
//
// NSDictionary* dict = [NRMAAnalytics getLastSessionsAttributes];
// NSArray* array = [NRMAAnalytics getLastSessionsEvents];
//
// XCTAssertTrue([dict[@"12345"] isEqual:@5], @"failed to correctly fetch from dup attribute store.");
// XCTAssertTrue([dict[@"test"] isEqual:@"hello"],@"failed to correctly fetch from dup attribute store.");
// XCTAssertTrue([array[0][@"name"] isEqualToString: @"blah"], @"failed to correctly fetch dup event store.");
//
// dict = [NRMAAnalytics getLastSessionsAttributes];
// array = [NRMAAnalytics getLastSessionsEvents];
//
// XCTAssertTrue(dict.count == 0, @"dup stores should be empty.");
// XCTAssertTrue(array.count == 0, @"dup stores should be empty.");
//
// analytics = [[NRMAAnalytics alloc] initWithSessionStartTimeMS:0];
// dict = [NRMAAnalytics getLastSessionsAttributes];
// array = [NRMAAnalytics getLastSessionsEvents];
// XCTAssertTrue([dict[@"test"] isEqualToString:@"hello"],@"persistent attribute was not added to the dup store");
// XCTAssertTrue(array.count == 0, @"dup events not empty after Analytics persistent data restored.");
NRMAAnalytics* analytics = [[NRMAAnalytics alloc] initWithSessionStartTimeMS:0];

[analytics setSessionAttribute:@"12345" value:@5];
[analytics addEventNamed:@"blah" withAttributes:@{@"pew pew":@"asdf"}];
[analytics setSessionAttribute:@"test" value:@"hello"];

NSString* attributes = [NRMAAnalytics getLastSessionsAttributes];
NSData *data = [attributes dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *aDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSString* events = [NRMAAnalytics getLastSessionsEvents];
data = [events dataUsingEncoding:NSUTF8StringEncoding];
NSArray *eArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

XCTAssertTrue([aDict[@"12345"] isEqual:@5], @"failed to correctly fetch from dup attribute store.");
XCTAssertTrue([aDict[@"test"] isEqual:@"hello"],@"failed to correctly fetch from dup attribute store.");
XCTAssertTrue([eArray[0][@"eventType"] isEqualToString: @"blah"], @"failed to correctly fetch dup event store.");

analytics = [[NRMAAnalytics alloc] initWithSessionStartTimeMS:0];
attributes = [NRMAAnalytics getLastSessionsAttributes];
data = [attributes dataUsingEncoding:NSUTF8StringEncoding];
aDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

events = [NRMAAnalytics getLastSessionsEvents];
data = [events dataUsingEncoding:NSUTF8StringEncoding];
eArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

XCTAssertTrue([aDict[@"test"] isEqualToString:@"hello"],@"persistent attribute was not added to the dup store");
XCTAssertTrue(eArray.count != 0, @"dup events not empty after Analytics persistent data restored.");
}


- (void) testMidSessionHarvest {
//todo: reenable test (disabled for beta 1, no persistent store)
//
Expand Down