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

Adding a test to test overall persistence of userId and anonymousId. Change setCachedSettings: to not statically dispatch_once #623

Merged
merged 1 commit into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions Analytics/Classes/Integrations/SEGIntegrationsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -334,30 +334,29 @@ - (void)setCachedSettings:(NSDictionary *)settings
}
[self.storage setDictionary:_cachedSettings forKey:@"analytics.settings.v2.plist"];

static dispatch_once_t once;
dispatch_once(&once, ^{
[self updateIntegrationsWithSettings:settings[@"integrations"]];
});
[self updateIntegrationsWithSettings:settings[@"integrations"]];
}

- (void)updateIntegrationsWithSettings:(NSDictionary *)projectSettings
{
for (id<SEGIntegrationFactory> factory in self.factories) {
NSString *key = [factory key];
NSDictionary *integrationSettings = [projectSettings objectForKey:key];
if (integrationSettings) {
id<SEGIntegration> integration = [factory createWithSettings:integrationSettings forAnalytics:self.analytics];
if (integration != nil) {
self.integrations[key] = integration;
self.registeredIntegrations[key] = @NO;
seg_dispatch_specific_sync(_serialQueue, ^{
if (self.initialized) {
return;
}
for (id<SEGIntegrationFactory> factory in self.factories) {
NSString *key = [factory key];
NSDictionary *integrationSettings = [projectSettings objectForKey:key];
if (integrationSettings) {
id<SEGIntegration> integration = [factory createWithSettings:integrationSettings forAnalytics:self.analytics];
if (integration != nil) {
self.integrations[key] = integration;
self.registeredIntegrations[key] = @NO;
}
[[NSNotificationCenter defaultCenter] postNotificationName:SEGAnalyticsIntegrationDidStart object:key userInfo:nil];
} else {
SEGLog(@"No settings for %@. Skipping.", key);
}
[[NSNotificationCenter defaultCenter] postNotificationName:SEGAnalyticsIntegrationDidStart object:key userInfo:nil];
} else {
SEGLog(@"No settings for %@. Skipping.", key);
}
}

seg_dispatch_specific_async(_serialQueue, ^{
[self flushMessageQueue];
self.initialized = true;
});
Expand Down
14 changes: 11 additions & 3 deletions Analytics/Classes/Internal/SEGSegmentIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,20 @@ - (id)initWithAnalytics:(SEGAnalytics *)analytics httpClient:(SEGHTTPClient *)ht
[self trackAttributionData:self.configuration.trackAttributionData];
}];

dispatch_sync(dispatch_get_main_queue(), ^{
self.flushTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(flush) userInfo:nil repeats:YES];
});
if ([NSThread isMainThread]) {
[self setupFlushTimer];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
[self setupFlushTimer];
});
}
}
return self;
}

- (void)setupFlushTimer {
self.flushTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(flush) userInfo:nil repeats:YES];
}

/*
* There is an iOS bug that causes instances of the CTTelephonyNetworkInfo class to
Expand Down
57 changes: 53 additions & 4 deletions Example/Tests/AnalyticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,48 @@ import Quick
import Nimble
import Analytics

extension SEGAnalytics {
func test_integrationsManager() -> SEGIntegrationsManager? {
return self.value(forKey: "integrationsManager") as? SEGIntegrationsManager
}
}

extension SEGIntegrationsManager {
func test_integrations() -> [String: SEGIntegration]? {
return self.value(forKey: "integrations") as? [String: SEGIntegration]
}
func test_segmentIntegration() -> SEGSegmentIntegration? {
return self.test_integrations()?["Segment.io"] as? SEGSegmentIntegration
}
func test_setCachedSettings(settings: NSDictionary) {
self.perform("setCachedSettings:", with: settings)
}
}

extension SEGSegmentIntegration {
func test_userId() -> String? {
return self.value(forKey: "userId") as? String
}
}

class AnalyticsTests: QuickSpec {
override func spec() {

let config = SEGAnalyticsConfiguration(writeKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE")
let cachedSettings = [
"integrations": [
"Segment.io": ["apiKey": "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE"]
],
"plan": ["track": []],
] as NSDictionary
var analytics: SEGAnalytics!

beforeEach {
let config = SEGAnalyticsConfiguration(writeKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE")
SEGAnalytics.setup(with: config)
analytics = SEGAnalytics.shared()
analytics = SEGAnalytics(configuration: config)
analytics.test_integrationsManager()?.test_setCachedSettings(settings: cachedSettings)
}

afterEach {
analytics.reset()
}

it("initialized correctly") {
Expand All @@ -28,6 +61,22 @@ class AnalyticsTests: QuickSpec {
expect(analytics.configuration.shouldUseLocationServices) == false
expect(analytics.configuration.enableAdvertisingTracking) == true
expect(analytics.configuration.shouldUseBluetooth) == false
expect(analytics.getAnonymousId()).toNot(beNil())
}

it("persists anonymousId") {
let analytics2 = SEGAnalytics(configuration: config)
expect(analytics.getAnonymousId()) == analytics2?.getAnonymousId()
}

it("persists userId") {
analytics.identify("testUserId1")

let analytics2 = SEGAnalytics(configuration: config)
analytics2?.test_integrationsManager()?.test_setCachedSettings(settings: cachedSettings)

expect(analytics.test_integrationsManager()?.test_segmentIntegration()?.test_userId()) == "testUserId1"
expect(analytics2?.test_integrationsManager()?.test_segmentIntegration()?.test_userId()) == "testUserId1"
}
}

Expand Down
2 changes: 2 additions & 0 deletions Example/Tests/Analytics_Tests-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import <Analytics/SEGFileStorage.h>
#import <Analytics/SEGUserDefaultsStorage.h>
#import <Analytics/SEGContext.h>
#import <Analytics/SEGIntegrationsManager.h>
#import <Analytics/SEGSegmentIntegration.h>

#import "NSData+SEGGUNZIPP.h"
// Temp hack. We should fix the LSNocilla podspec to make this header publicly available
Expand Down