Skip to content

Commit c501fd3

Browse files
authored
[LP-10929] Using operation queue to execute writes in bg (#324)
* Using operation queue to execute writes in bg * Fixing build errors Removing unused schemes * Fixing failing tests * Fixing failing tests * 2.6.3-beta1 test background * update version * Better naming * Reverting .pbxproj * Removing semicolons * Fixing failing tests
1 parent 781d6c1 commit c501fd3

File tree

23 files changed

+245
-341
lines changed

23 files changed

+245
-341
lines changed

Example/Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@
687687
files = (
688688
);
689689
inputPaths = (
690-
"${SRCROOT}/Pods/Target Support Files/Pods-Leanplum-SDK_Example/Pods-Leanplum-SDK_Example-frameworks.sh",
690+
"${PODS_ROOT}/Target Support Files/Pods-Leanplum-SDK_Example/Pods-Leanplum-SDK_Example-frameworks.sh",
691691
"${BUILT_PRODUCTS_DIR}/Leanplum-iOS-SDK/Leanplum.framework",
692692
);
693693
name = "[CP] Embed Pods Frameworks";
@@ -696,7 +696,7 @@
696696
);
697697
runOnlyForDeploymentPostprocessing = 0;
698698
shellPath = /bin/sh;
699-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Leanplum-SDK_Example/Pods-Leanplum-SDK_Example-frameworks.sh\"\n";
699+
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Leanplum-SDK_Example/Pods-Leanplum-SDK_Example-frameworks.sh\"\n";
700700
showEnvVarsInLog = 0;
701701
};
702702
AA36E9F20FC263CA34118B64 /* [CP] Check Pods Manifest.lock */ = {
@@ -723,7 +723,7 @@
723723
files = (
724724
);
725725
inputPaths = (
726-
"${SRCROOT}/Pods/Target Support Files/Pods-Leanplum-SDK_Tests/Pods-Leanplum-SDK_Tests-frameworks.sh",
726+
"${PODS_ROOT}/Target Support Files/Pods-Leanplum-SDK_Tests/Pods-Leanplum-SDK_Tests-frameworks.sh",
727727
"${BUILT_PRODUCTS_DIR}/Leanplum-iOS-SDK/Leanplum.framework",
728728
"${BUILT_PRODUCTS_DIR}/OCMock/OCMock.framework",
729729
"${BUILT_PRODUCTS_DIR}/OHHTTPStubs/OHHTTPStubs.framework",
@@ -736,7 +736,7 @@
736736
);
737737
runOnlyForDeploymentPostprocessing = 0;
738738
shellPath = /bin/sh;
739-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Leanplum-SDK_Tests/Pods-Leanplum-SDK_Tests-frameworks.sh\"\n";
739+
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Leanplum-SDK_Tests/Pods-Leanplum-SDK_Tests-frameworks.sh\"\n";
740740
showEnvVarsInLog = 0;
741741
};
742742
/* End PBXShellScriptBuildPhase section */

Example/Tests/Classes/Extensions/LeanplumRequest+Categories.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ + (void)swizzle_methods
3838
withMethod:@selector(swizzle_sendNow)
3939
error:&error
4040
class:[LeanplumRequest class]];
41+
success &= [LPSwizzle swizzleMethod:@selector(sendEventually:)
42+
withMethod:@selector(swizzle_sendEventually:)
43+
error:&error
44+
class:[LeanplumRequest class]];
4145
success &= [LPSwizzle swizzleClassMethod:@selector(get:params:)
4246
withClassMethod:@selector(swizzle_get:params:)
4347
error:&error
@@ -63,6 +67,11 @@ - (void)swizzle_sendNow
6367
func(self, selector);
6468
}
6569

70+
- (void)swizzle_sendEventually:(BOOL) sync
71+
{
72+
[self swizzle_sendEventually:YES];
73+
}
74+
6675
- (void)swizzle_download
6776
{
6877

Example/Tests/Classes/LPActionManagerTest.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#import "LPNetworkEngine+Category.h"
3434
#import "Leanplum+Extensions.h"
3535
#import "LPUIAlert.h"
36+
#import "LPOperationQueue.h"
3637

3738
@interface LPActionManager (Test)
3839
- (void)requireMessageContent:(NSString *)messageId
@@ -260,7 +261,7 @@ - (void)test_push_token
260261
return YES;
261262
}];
262263
[manager leanplum_application:app didRegisterForRemoteNotificationsWithDeviceToken:token];
263-
264+
264265
// Test push token is sent if the token changes.
265266
token = [@"sample2" dataUsingEncoding:NSUTF8StringEncoding];
266267
formattedToken = [token description];
@@ -276,6 +277,7 @@ - (void)test_push_token
276277
return YES;
277278
}];
278279
[manager leanplum_application:app didRegisterForRemoteNotificationsWithDeviceToken:token];
280+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
279281
[self waitForExpectationsWithTimeout:2 handler:nil];
280282
}
281283

Example/Tests/Classes/LPCountAggregatorTest.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ - (void)test_sendAllCounts {
140140
OCMStub([leanplumRequestMock post:LP_METHOD_LOG params:[OCMArg any]]).andReturn(leanplumRequestMock);
141141

142142
[countAggregator sendAllCounts];
143-
144-
OCMVerify([leanplumRequestMock sendEventually]);
143+
144+
[[[leanplumRequestMock verify] ignoringNonObjectArgs] sendEventually:[OCMArg any]];
145145
}
146146

147147
@end

Example/Tests/Classes/LPEventDataManagerTest.m

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#import "LPNetworkEngine+Category.h"
3535
#import "LeanplumReachability+Category.h"
3636
#import "LPJSON.h"
37+
#import "LPOperationQueue.h"
3738

3839
@interface LPEventDataManager(UnitTest)
3940

@@ -44,7 +45,6 @@ + (void)migrateRequests;
4445
@interface LeanplumRequest(UnitTest)
4546

4647
- (void)sendNow:(BOOL)async;
47-
+ (NSOperationQueue *)sendNowQueue;
4848

4949
@end
5050

@@ -112,10 +112,12 @@ - (void)test_track_save
112112
[LPEventDataManager deleteEventsWithLimit:10000];
113113

114114
[Leanplum track:@"sample"];
115+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
115116
NSArray *events = [LPEventDataManager eventsWithLimit:10000];
116117
XCTAssertTrue(events.count == 1);
117118

118119
[Leanplum track:@"sample2"];
120+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
119121
events = [LPEventDataManager eventsWithLimit:10000];
120122
XCTAssertTrue(events.count == 2);
121123

@@ -163,8 +165,8 @@ - (void)test_request_synchronous
163165
XCTAssertTrue(timedOut == 0);
164166

165167
// Clean up.
166-
[[LeanplumRequest sendNowQueue] cancelAllOperations];
167-
[[LeanplumRequest sendNowQueue] waitUntilAllOperationsAreFinished];
168+
[[LPOperationQueue serialQueue] cancelAllOperations];
169+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
168170
[OHHTTPStubs removeAllStubs];
169171
}
170172

@@ -217,6 +219,9 @@ - (void)test_uuid
217219
[Leanplum track:@"sample"];
218220
[Leanplum track:@"sample2"];
219221
[[LeanplumRequest post:@"sample3" params:nil] sendNow:YES];
222+
223+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
224+
220225
NSArray *events = [LPEventDataManager eventsWithLimit:10000];
221226
XCTAssertTrue(events.count == 3);
222227
XCTAssertTrue([events[0][@"uuid"] isEqual:events[1][@"uuid"]]);
@@ -225,6 +230,9 @@ - (void)test_uuid
225230
// After sending, the last one should have a different uuid.
226231
[NSThread sleepForTimeInterval:0.4];
227232
[Leanplum track:@"sample4"];
233+
234+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
235+
228236
events = [LPEventDataManager eventsWithLimit:10000];
229237
XCTAssertTrue(events.count == 4);
230238
XCTAssertTrue([events[0][@"uuid"] isEqual:events[1][@"uuid"]]);
@@ -244,13 +252,15 @@ - (void)test_uuid
244252

245253
[[LeanplumRequest post:@"sample4" params:nil] sendNow:NO];
246254
[NSThread sleepForTimeInterval:0.1];
255+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
247256
events = [LPEventDataManager eventsWithLimit:10000];
248257
XCTAssertTrue(events.count == 0);
249258

250259
// UUID should be different after the 10k mark.
251260
for (int i=0; i<MAX_EVENTS_PER_API_CALL+1; i++) {
252261
[Leanplum track:@"s"];
253262
}
263+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
254264
events = [LPEventDataManager eventsWithLimit:900000];
255265
XCTAssertTrue(events.count == MAX_EVENTS_PER_API_CALL+1);
256266
XCTAssertFalse([events[0][@"uuid"] isEqual:events[MAX_EVENTS_PER_API_CALL][@"uuid"]]);
@@ -294,14 +304,14 @@ - (void)test_response_index
294304
[self expectationWithDescription:@"operationExpectation"];
295305
[LPNetworkEngine validate_operation:^(LPNetworkOperation *operation) {
296306
NSArray *events = [LPEventDataManager eventsWithLimit:900000];
297-
XCTAssertTrue(events.count == 3);
307+
XCTAssertTrue(events.count == 1);
298308
[operationExpectation fulfill];
299309
return YES;
300310
}];
301311

302312
// Freeze the operation queue for a bit to let events queue up.
303-
NSOperationQueue *sendNowQueue = [LeanplumRequest sendNowQueue];
304-
[sendNowQueue addOperationWithBlock:^{
313+
NSOperationQueue *serialQueue = [LPOperationQueue serialQueue];
314+
[serialQueue addOperationWithBlock:^{
305315
[NSThread sleepForTimeInterval:0.2];
306316
}];
307317

@@ -315,11 +325,14 @@ - (void)test_response_index
315325
[responseExpectation fulfill];
316326
}];
317327
[request sendNow:YES];
318-
328+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
329+
330+
319331
// Add extra events.
320332
[Leanplum track:@"s"];
321333
[Leanplum track:@"s2"];
322-
334+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
335+
323336
[self waitForExpectationsWithTimeout:2 handler:nil];
324337
}
325338

Example/Tests/Classes/LPRequestSenderTest.m

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
#import <Leanplum/LPRequestFactory.h>
1616
#import <Leanplum/LPRequest.h>
1717
#import <Leanplum/LPCountAggregator.h>
18+
#import "LeanplumHelper.h"
19+
#import "LPOperationQueue.h"
1820

1921
@interface LPRequestSender(UnitTest)
2022

2123
@property (nonatomic, strong) id<LPNetworkEngineProtocol> engine;
2224

2325
- (void)sendNow:(id<LPRequesting>)request sync:(BOOL)sync;
2426
- (void)sendRequests:(BOOL)sync;
25-
- (NSOperationQueue *)sendNowQueue;
2627

2728
@end
2829

@@ -32,8 +33,11 @@ @interface LPRequestSenderTest : XCTestCase
3233

3334
@implementation LPRequestSenderTest
3435

35-
- (void)setUp {
36-
// Put setup code here. This method is called before the invocation of each test method in the class.
36+
+ (void)setUp
37+
{
38+
[super setUp];
39+
// Called only once to setup method swizzling.
40+
[LeanplumHelper setup_method_swizzling];
3741
}
3842

3943
- (void)tearDown {
@@ -73,7 +77,7 @@ - (void)testSend {
7377
OCMStub([configMock accessKey]).andReturn(@"accessKey");
7478
[requestSender send:request];
7579

76-
OCMVerify([requestSenderMock sendEventually:request]);
80+
[[[requestSenderMock verify] ignoringNonObjectArgs] sendEventually:request sync:[OCMArg any]];
7781

7882
[requestSenderMock stopMocking];
7983
[configMock stopMocking];
@@ -89,9 +93,9 @@ - (void)testSendNow {
8993
OCMStub([configMock accessKey]).andReturn(@"accessKey");
9094
[requestSender sendNow:request];
9195

92-
OCMVerify([requestSenderMock sendNow:request sync:false]);
93-
OCMVerify([requestSenderMock sendEventually:request]);
94-
OCMVerify([requestSenderMock sendRequests:false]);
96+
[[[requestSenderMock verify] ignoringNonObjectArgs] sendNow:request sync:[OCMArg any]];
97+
[[[requestSenderMock verify] ignoringNonObjectArgs] sendEventually:request sync:[OCMArg any]];
98+
[[[requestSenderMock verify] ignoringNonObjectArgs] sendRequests:[OCMArg any]];
9599

96100
[requestSenderMock stopMocking];
97101
[configMock stopMocking];
@@ -101,7 +105,7 @@ - (void)testSendEventually {
101105
LPRequest *request = [LPRequest post:@"test" params:@{}];
102106
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
103107
id eventDataManagerMock = OCMClassMock([LPEventDataManager class]);
104-
[requestSender sendEventually:request];
108+
[requestSender sendEventually:request sync:YES];
105109

106110
OCMVerify([eventDataManagerMock addEvent:[OCMArg isNotNil]]);
107111

@@ -131,7 +135,7 @@ - (void)testSendIfNotConnected {
131135
OCMStub([reachabilityMock isReachable]).andReturn(false);
132136
[requestSender sendIfConnected:request];
133137

134-
OCMVerify([requestSenderMock sendEventually:request]);
138+
[[[requestSenderMock verify] ignoringNonObjectArgs] sendEventually:request sync:[OCMArg any]];
135139

136140
[requestSenderMock stopMocking];
137141
[reachabilityMock stopMocking];
@@ -199,18 +203,4 @@ - (void) testSendRequestsSync {
199203
[eventDataManagerMock stopMocking];
200204
}
201205

202-
- (void) testSendRequestsAsync {
203-
LPRequestSender *requestSender = OCMPartialMock([[LPRequestSender alloc] init]);
204-
id requestOperationMock = OCMClassMock([NSBlockOperation class]);
205-
OCMStub([requestOperationMock new]).andReturn(requestOperationMock);
206-
id mockQueue = OCMClassMock([NSOperationQueue class]);
207-
OCMStub([requestSender sendNowQueue]).andReturn(mockQueue);
208-
[requestSender sendRequests:false];
209-
210-
OCMVerify([requestOperationMock addExecutionBlock:[OCMArg any]]);
211-
OCMVerify([mockQueue addOperation:requestOperationMock]);
212-
213-
[requestOperationMock stopMocking];
214-
[mockQueue stopMocking];
215-
}
216206
@end

Example/Tests/Classes/LeanplumTest.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#import "LPConstants.h"
4040
#import "LPRegisterDevice.h"
4141
#import "Leanplum.h"
42+
#import "LPOperationQueue.h"
4243

4344
/**
4445
* Tests leanplum public methods, we seed predefined response that comes from backend
@@ -810,9 +811,9 @@ - (void) test_track_events_priority_countDown
810811
XCTAssertEqual(outputDict[messageIdOne][@"countdown"], outputDict[messageIdOne][@"countdown"], @"Countdown not Equal");
811812
}];
812813
[Leanplum track:trackName];
814+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
813815
[Leanplum forceContentUpdate];
814816
XCTAssertTrue([Leanplum hasStarted]);
815-
816817
}
817818

818819
/**

Example/Tests/Classes/Utilities/LeanplumHelper.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#import "LPNetworkEngine+Category.h"
3737
#import "LPNetworkOperation+Category.h"
3838
#import "LPAPIConfig.h"
39+
#import "LPOperationQueue.h"
3940

4041
NSString *APPLICATION_ID = @"app_nLiaLr3lXvCjXhsztS1Gw8j281cPLO6sZetTDxYnaSk";
4142
NSString *DEVELOPMENT_KEY = @"dev_2bbeWLmVJyNrqI8F21Kn9nqyUPRkVCUoLddBkHEyzmk";
@@ -45,12 +46,6 @@
4546

4647
NSInteger DISPATCH_WAIT_TIME = 4;
4748

48-
@interface LeanplumRequest(LeanplumHelper)
49-
50-
+ (NSOperationQueue *)sendNowQueue;
51-
52-
@end
53-
5449
@implementation LeanplumHelper
5550

5651
static BOOL swizzled = NO;
@@ -129,8 +124,8 @@ + (void)clean_up {
129124
[[LPAPIConfig sharedConfig] setToken:nil];
130125
[LeanplumRequest reset];
131126
[LeanplumHelper reset_user_defaults];
132-
[[LeanplumRequest sendNowQueue] cancelAllOperations];
133-
[[LeanplumRequest sendNowQueue] waitUntilAllOperationsAreFinished];
127+
[[LPOperationQueue serialQueue] cancelAllOperations];
128+
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
134129
}
135130

136131
+ (dispatch_time_t)default_dispatch_time {

0 commit comments

Comments
 (0)