Skip to content

Commit cb2d955

Browse files
Merge pull request #980 from BranchMetrics/INTENG-8009
INTENG-8009
2 parents 16154d5 + 56b2f2b commit cb2d955

File tree

7 files changed

+145
-5
lines changed

7 files changed

+145
-5
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// BNCPreInitBlockTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Benas Klastaitis on 12/9/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
// #import "Branch.h"
11+
12+
@interface DispatchToIsolationQueueTests : XCTestCase
13+
// @property (nonatomic, strong, readwrite) Branch *branch;
14+
// @property (nonatomic, strong, readwrite) BNCPreferenceHelper *prefHelper;
15+
@end
16+
17+
// this is an integration test, needs to be moved to a new target
18+
@implementation DispatchToIsolationQueueTests
19+
20+
- (void)setUp {
21+
// self.branch = [Branch getInstance];
22+
// self.prefHelper = [[BNCPreferenceHelper alloc] init];
23+
}
24+
25+
- (void)tearDown {
26+
// [self.prefHelper setRequestMetadataKey:@"$marketing_cloud_visitor_id" value:@"dummy"];
27+
}
28+
29+
- (void)testPreInitBlock {
30+
// __block XCTestExpectation *expectation = [self expectationWithDescription:@""];
31+
32+
// [self.branch dispatchToIsolationQueue:^{
33+
// dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
34+
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
35+
// [self.branch setRequestMetadataKey:@"$marketing_cloud_visitor_id" value:@"adobeID123"];
36+
// dispatch_semaphore_signal(semaphore);
37+
// });
38+
// dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
39+
// }];
40+
41+
// [self.branch initSessionWithLaunchOptions:nil andRegisterDeepLinkHandlerUsingBranchUniversalObject:
42+
// ^ (BranchUniversalObject * _Nullable universalObject,
43+
// BranchLinkProperties * _Nullable linkProperties,
44+
// NSError * _Nullable error) {
45+
// [expectation fulfill];
46+
// }];
47+
48+
// // test that session initialization blocking works
49+
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
50+
// id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
51+
// XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 0);// uninitialized
52+
// XCTAssertNil([[self.prefHelper requestMetadataDictionary] objectForKey:@"$marketing_cloud_visitor_id"]);
53+
// });
54+
55+
// // test that initialization does happen afterwards and that pre init block was executed
56+
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
57+
// id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
58+
// XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 2);// initialized
59+
60+
// XCTAssertTrue([[[self.prefHelper requestMetadataDictionary] objectForKey:@"$marketing_cloud_visitor_id"] isEqualToString:@"adobeID123"]);
61+
// });
62+
63+
64+
// [self waitForExpectationsWithTimeout:6 handler:^(NSError * _Nullable error) {
65+
// NSLog(@"%@", error);
66+
// }];
67+
}
68+
69+
// -(int)enumIntValueFromId:(id)enumValueId {
70+
// if (![enumValueId respondsToSelector:@selector(intValue)])
71+
// return -1;
72+
73+
// return [enumValueId intValue];
74+
// }
75+
76+
@end

Branch-SDK/Branch-SDK/Branch.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,18 @@ typedef NS_ENUM(NSUInteger, BranchCreditHistoryOrder) {
514514
/// @name Push Notification Support
515515
///--------------------------------
516516

517+
#pragma mark - Pre-initialization support
518+
519+
/**
520+
DO NOT USE unless you are familiar with the SDK's threading model.
521+
522+
When certain actions are required to complete prior to session initialization, this method can be used to pass in a blocking dispatch_block_t.
523+
The passed in dispatch_block_t will block Branch initialization thread, not the main thread.
524+
525+
@param initBlock dispatch_block_t object to be executed prior to session initialization
526+
*/
527+
- (void)dispatchToIsolationQueue:(dispatch_block_t)initBlock;
528+
517529
#pragma mark - Push Notification support
518530

519531
/**

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,12 @@ - (void)checkAppleSearchAdsAttribution {
917917
});
918918
}
919919

920+
#pragma mark - Pre-initialization support
921+
922+
- (void) dispatchToIsolationQueue:(dispatch_block_t) initBlock {
923+
dispatch_async(self.isolationQueue, initBlock);
924+
}
925+
920926
#pragma mark - Facebook App Link Check
921927

922928
- (void)registerFacebookDeepLinkingClass:(id)FBSDKAppLinkUtility {
@@ -1775,11 +1781,10 @@ - (void)registerViewWithParams:(NSDictionary *)params andCallback:(callbackWithP
17751781
#pragma mark - Application State Change methods
17761782

17771783
- (void)applicationDidBecomeActive {
1778-
if (!Branch.trackingDisabled) {
1779-
if ((self.initializationStatus != BNCInitStatusInitialized) &&
1780-
![self.requestQueue containsInstallOrOpen]) {
1781-
[self initUserSessionAndCallCallback:YES];
1782-
}
1784+
if (!Branch.trackingDisabled &&
1785+
self.initializationStatus != BNCInitStatusInitialized &&
1786+
[self.requestQueue containsInstallOrOpen]) {
1787+
[self initUserSessionAndCallCallback:YES];
17831788
}
17841789
}
17851790

Branch-TestBed/Branch-TestBed.xcodeproj/project.pbxproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
4683F0751B20A73F00A432E7 /* CreditHistoryViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EAA790119E89F67008D4A83 /* CreditHistoryViewController.m */; };
2525
4683F0761B20A73F00A432E7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 670016731940F51400A9E103 /* AppDelegate.m */; };
2626
46DC406E1B2A328900D2D203 /* AdSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 67BBCF271A69E49A009C7DAE /* AdSupport.framework */; };
27+
4AB16368239E3A2700D42931 /* DispatchToIsolationQueueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB16367239E3A2700D42931 /* DispatchToIsolationQueueTests.m */; };
2728
4D130E731EE0C7B100A69A0A /* BNCNetworkService.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E4D1EE0C7B000A69A0A /* BNCNetworkService.m */; };
2829
4D130E761EE0C7B100A69A0A /* BNCServerInterface.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E501EE0C7B000A69A0A /* BNCServerInterface.m */; };
2930
4D130E781EE0C7B100A69A0A /* BNCServerRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E521EE0C7B000A69A0A /* BNCServerRequest.m */; };
@@ -317,6 +318,7 @@
317318
466D5A0F1B5991E3009DB845 /* BNCContentDiscoveryManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCContentDiscoveryManager.h; sourceTree = "<group>"; };
318319
466D5A101B5991E3009DB845 /* BNCContentDiscoveryManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BNCContentDiscoveryManager.m; sourceTree = "<group>"; };
319320
46DBB42F1B335A9B00642FC8 /* BranchDeepLinkingController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BranchDeepLinkingController.h; sourceTree = "<group>"; };
321+
4AB16367239E3A2700D42931 /* DispatchToIsolationQueueTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DispatchToIsolationQueueTests.m; sourceTree = "<group>"; };
320322
4D130E4C1EE0C7B000A69A0A /* BNCNetworkService.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCNetworkService.h; sourceTree = "<group>"; };
321323
4D130E4D1EE0C7B000A69A0A /* BNCNetworkService.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BNCNetworkService.m; sourceTree = "<group>"; };
322324
4D130E4E1EE0C7B000A69A0A /* BNCNetworkServiceProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCNetworkServiceProtocol.h; sourceTree = "<group>"; };
@@ -758,8 +760,12 @@
758760
5F3D6719233062FD00454FF1 /* BNCJsonLoader.h */,
759761
5F3D671A233062FD00454FF1 /* BNCJsonLoader.m */,
760762
4D16839F2098C901008819E3 /* Info.plist */,
763+
<<<<<<< HEAD
764+
4AB16367239E3A2700D42931 /* DispatchToIsolationQueueTests.m */,
765+
=======
761766
5F437E39237DE3480052064B /* BNCTelephonyTests.m */,
762767
5F437E3F237E1A560052064B /* BNCDeviceSystemTests.m */,
768+
>>>>>>> 16154d53d1bb758765658b3bc2368881e658cc89
763769
);
764770
name = "Branch-SDK-Tests";
765771
path = "../Branch-SDK-Tests";
@@ -1460,6 +1466,7 @@
14601466
4D1683B92098C902008819E3 /* BNCSystemObserver.Test.m in Sources */,
14611467
4D1683BE2098C902008819E3 /* BranchShortUrlSyncRequestTests.m in Sources */,
14621468
4D1683CA2098C902008819E3 /* BNCPreferenceHelperTests.m in Sources */,
1469+
4AB16368239E3A2700D42931 /* DispatchToIsolationQueueTests.m in Sources */,
14631470
4D1683B32098C902008819E3 /* BranchGetCreditHistoryRequestTests.m in Sources */,
14641471
5F892ECE23624E0A0023AEC1 /* BNCFacebookAppLinksTests.m in Sources */,
14651472
4D1683BB2098C902008819E3 /* BranchShortUrlRequestTests.m in Sources */,

Branch-TestBed/Branch-TestBed.xcodeproj/xcshareddata/xcschemes/Branch-SDK-Tests.xcscheme

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@
7676
</Test>
7777
</SkippedTests>
7878
</TestableReference>
79+
<TestableReference
80+
skipped = "NO">
81+
<BuildableReference
82+
BuildableIdentifier = "primary"
83+
BlueprintIdentifier = "5F8B7B3A21B5F5CD009CE0A6"
84+
BuildableName = "Branch-SDK-Unhosted-Tests.xctest"
85+
BlueprintName = "Branch-SDK-Unhosted-Tests"
86+
ReferencedContainer = "container:Branch-TestBed.xcodeproj">
87+
</BuildableReference>
88+
</TestableReference>
7989
</Testables>
8090
</TestAction>
8191
<LaunchAction
@@ -88,6 +98,7 @@
8898
ignoresPersistentStateOnLaunch = "NO"
8999
debugDocumentVersioning = "YES"
90100
stopOnEveryThreadSanitizerIssue = "YES"
101+
migratedStopOnEveryIssue = "YES"
91102
debugServiceExtension = "internal"
92103
allowLocationSimulation = "YES">
93104
<MacroExpansion>

Branch-TestBed/Branch-TestBed.xcodeproj/xcshareddata/xcschemes/Branch-TestBed.xcscheme

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@
9191
ReferencedContainer = "container:Branch-TestBed.xcodeproj">
9292
</BuildableReference>
9393
</TestableReference>
94+
<TestableReference
95+
skipped = "NO">
96+
<BuildableReference
97+
BuildableIdentifier = "primary"
98+
BlueprintIdentifier = "5F8B7B3A21B5F5CD009CE0A6"
99+
BuildableName = "Branch-SDK-Unhosted-Tests.xctest"
100+
BlueprintName = "Branch-SDK-Unhosted-Tests"
101+
ReferencedContainer = "container:Branch-TestBed.xcodeproj">
102+
</BuildableReference>
103+
</TestableReference>
94104
</Testables>
95105
</TestAction>
96106
<LaunchAction

Branch-TestBed/Branch-TestBed/AppDelegate.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ - (BOOL)application:(UIApplication *)application
2929

3030
//
3131
Branch *branch = [Branch getInstance];
32+
33+
34+
// test pre init support
35+
// [self testDispatchToIsolationQueue:branch]
3236

3337
// Comment out (for match guarantee testing) / or un-comment to toggle debugging:
3438
// Note: Unit tests will fail if 'setDebug' is set.
@@ -57,6 +61,21 @@ - (BOOL)application:(UIApplication *)application
5761
return YES;
5862
}
5963

64+
// pre init support is meant for extensions, for exmaple, when Adobe axtension needs to pass in Adobe IDs
65+
// before init session is called. This method will block the queue used by open/install requests until the
66+
// the passsed in block completes
67+
- (void) testDispatchToIsolationQueue:(Branch*)branch {
68+
[branch dispatchToIsolationQueue:^{
69+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
70+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
71+
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
72+
[branch setRequestMetadataKey:@"keykey" value:@"valuevalue"];
73+
dispatch_semaphore_signal(semaphore);
74+
});
75+
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
76+
}];
77+
}
78+
6079
- (void) handleDeepLinkParams:(NSDictionary*)params error:(NSError*)error {
6180
if (error) {
6281
NSLog(@"Branch TestBed: Error deep linking: %@.", error.localizedDescription);

0 commit comments

Comments
 (0)