@@ -52,9 +52,13 @@ @interface LPEventDataManagerTest : XCTestCase
5252@implementation LPEventDataManagerTest
5353
5454- (void )setUp {
55+ [super setUp ];
5556 id mockedDB = OCMClassMock ([LPDatabase class ]);
5657 OCMStub ([mockedDB sqliteFilePath ]).andReturn (@" :memory:" );
57- [super setUp ];
58+ [LeanplumHelper setup_method_swizzling ];
59+ [LeanplumHelper start_production_test ];
60+ [LPNetworkEngine setupValidateOperation ];
61+ [Leanplum_Reachability online: YES ];
5862}
5963
6064- (void )tearDown {
@@ -71,16 +75,16 @@ - (NSDictionary *)sampleData
7175- (void )test_publicEventMethods
7276{
7377 [LPEventDataManager deleteEventsWithLimit: 10000 ];
74-
78+
7579 // Add Event.
7680 [LPEventDataManager addEvent: [self sampleData ]];
7781 NSArray *events = [LPEventDataManager eventsWithLimit: 10000 ];
7882 XCTAssertTrue (events.count == 1 );
79-
83+
8084 [LPEventDataManager addEvent: [self sampleData ]];
8185 events = [LPEventDataManager eventsWithLimit: 10000 ];
8286 XCTAssertTrue (events.count == 2 );
83-
87+
8488 // Add Multiple Events.
8589 NSMutableArray *mulitpleEvents = [NSMutableArray new ];
8690 for (int i=0 ; i<5 ; i++) {
@@ -89,16 +93,16 @@ - (void)test_publicEventMethods
8993 [LPEventDataManager addEvents: mulitpleEvents];
9094 events = [LPEventDataManager eventsWithLimit: 10000 ];
9195 XCTAssertTrue (events.count == 7 );
92-
96+
9397 // Get Events with limit.
9498 events = [LPEventDataManager eventsWithLimit: 2 ];
9599 XCTAssertTrue (events.count == 2 );
96-
100+
97101 // Delete events with limit.
98102 [LPEventDataManager deleteEventsWithLimit: 3 ];
99103 events = [LPEventDataManager eventsWithLimit: 10000 ];
100104 XCTAssertTrue (events.count == 4 );
101-
105+
102106 // Delete the rest.
103107 [LPEventDataManager deleteEventsWithLimit: 10000 ];
104108 events = [LPEventDataManager eventsWithLimit: 10000 ];
@@ -107,31 +111,21 @@ - (void)test_publicEventMethods
107111
108112- (void )test_track_save
109113{
110- [LeanplumHelper start_production_test ];
111114 [LPEventDataManager deleteEventsWithLimit: 10000 ];
112-
115+
113116 [Leanplum track: @" sample" ];
114117 [[LPOperationQueue serialQueue ] waitUntilAllOperationsAreFinished ];
115118 NSArray *events = [LPEventDataManager eventsWithLimit: 10000 ];
116119 XCTAssertTrue (events.count == 1 );
117-
120+
118121 [Leanplum track: @" sample2" ];
119122 [[LPOperationQueue serialQueue ] waitUntilAllOperationsAreFinished ];
120123 events = [LPEventDataManager eventsWithLimit: 10000 ];
121124 XCTAssertTrue (events.count == 2 );
122-
123- [LPEventDataManager deleteEventsWithLimit: 10000 ];
124- [LeanplumHelper clean_up ];
125125}
126126
127127- (void )test_request_synchronous
128128{
129- [LeanplumHelper setup_method_swizzling ];
130- [LeanplumHelper clean_up ];
131- [LeanplumHelper start_production_test ];
132- [LPNetworkEngine setupValidateOperation ];
133- [Leanplum_Reachability online: YES ];
134-
135129 // Simulate slow network.
136130 [HTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest * _Nonnull request) {
137131 return [request.URL.host isEqualToString: API_HOST];;
@@ -141,30 +135,33 @@ - (void)test_request_synchronous
141135 headers: @{@" Content-Type" :@" application/json" }]
142136 requestTime: 1.0 responseTime: 1.0 ];
143137 }];
144-
138+
145139 // Determine whether the requests are sent in order using the timestamp.
146140 NSDate *date = [NSDate date ];
147- dispatch_semaphore_t semaphore = dispatch_semaphore_create (0 );
141+ dispatch_semaphore_t semaphoreTest1 = dispatch_semaphore_create (0 );
142+ dispatch_semaphore_t semaphoreTest2 = dispatch_semaphore_create (0 );
148143 LPRequest *request = [LPRequestFactory createPostForApiMethod: @" test1" params: nil ];
149144 [[LPRequestSender sharedInstance ] sendNow: request];
150145 [LPNetworkEngine validate_operation: ^(LPNetworkOperation *operation) {
151146 // Make sure the call is for test2.
152147 NSDictionary *data = [LPJSON JSONFromString: operation.requestParam[@" data" ]];
153148 if ([data[@" data" ][0 ][@" action" ] isEqual: @" test1" ]) {
149+ dispatch_semaphore_signal (semaphoreTest1);
154150 return NO ;
155151 }
156152
157153 NSTimeInterval operationTime = [[NSDate date ] timeIntervalSinceDate: date];
158154 XCTAssertTrue (operationTime > 0.7 );
159- dispatch_semaphore_signal (semaphore );
155+ dispatch_semaphore_signal (semaphoreTest2 );
160156 return YES ;
161157 }];
162- [NSThread sleepForTimeInterval: 0.1 ];
158+ long timedOut = dispatch_semaphore_wait (semaphoreTest1, [LeanplumHelper default_dispatch_time ]);
159+ XCTAssertTrue (timedOut == 0 );
163160 LPRequest *request2 = [LPRequestFactory createPostForApiMethod: @" test2" params: nil ];
164161 [[LPRequestSender sharedInstance ] sendNow: request2];
165- long timedOut = dispatch_semaphore_wait (semaphore , [LeanplumHelper default_dispatch_time ]);
162+ timedOut = dispatch_semaphore_wait (semaphoreTest2 , [LeanplumHelper default_dispatch_time ]);
166163 XCTAssertTrue (timedOut == 0 );
167-
164+
168165 // Clean up.
169166 [[LPOperationQueue serialQueue ] cancelAllOperations ];
170167 [[LPOperationQueue serialQueue ] waitUntilAllOperationsAreFinished ];
@@ -173,41 +170,40 @@ - (void)test_request_synchronous
173170
174171- (void )test_response_code
175172{
176- [LeanplumHelper setup_method_swizzling ];
177- [LeanplumHelper start_production_test ];
173+ [LeanplumHelper clean_up ];
178174 [LPEventDataManager deleteEventsWithLimit: 10000 ];
179- [LPNetworkEngine setupValidateOperation ];
180- [Leanplum_Reachability online: YES ];
181-
175+
182176 // Simulate error from http response code.
183177 [HTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
184178 return [request.URL.host isEqualToString: API_HOST];;
185179 } withStubResponse: ^HTTPStubsResponse * _Nonnull (NSURLRequest * _Nonnull request) {
186180 NSData *data = [@" Fail" dataUsingEncoding: NSUTF8StringEncoding];
187181 return [HTTPStubsResponse responseWithData: data statusCode: 500 headers: nil ];
188182 }];
189-
183+
190184 NSArray *events = [LPEventDataManager eventsWithLimit: 10000 ];
191185 XCTAssertTrue (events.count == 0 );
192-
186+
187+ dispatch_semaphore_t semaphore = dispatch_semaphore_create (0 );
188+ [LPNetworkEngine validate_operation: ^BOOL (LPNetworkOperation *operation) {
189+ dispatch_semaphore_signal (semaphore);
190+ return YES ;
191+ }];
192+
193193 LPRequest *request = [LPRequestFactory createPostForApiMethod: @" sample3" params: nil ];
194194 [[LPRequestSender sharedInstance ] sendNow: request];
195- [NSThread sleepForTimeInterval: 0.2 ];
195+ long timedOut =dispatch_semaphore_wait (semaphore, [LeanplumHelper default_dispatch_time ]);
196+ XCTAssertTrue (timedOut == 0 );
196197 events = [LPEventDataManager eventsWithLimit: 10000 ];
197198 XCTAssertTrue (events.count == 1 );
198-
199- [LPEventDataManager deleteEventsWithLimit: 10000 ];
199+
200200 [HTTPStubs removeAllStubs ];
201201}
202202
203203- (void )test_uuid
204204{
205- [LeanplumHelper setup_method_swizzling ];
206- [LeanplumHelper start_production_test ];
207205 [LPEventDataManager deleteEventsWithLimit: 10000 ];
208- [LPNetworkEngine setupValidateOperation ];
209- [Leanplum_Reachability online: YES ];
210-
206+
211207 // Simulate Error.
212208 id <HTTPStubsDescriptor> stubs = [HTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
213209 return [request.URL.host isEqualToString: API_HOST];;
@@ -216,7 +212,7 @@ - (void)test_uuid
216212 code: NSURLErrorBadServerResponse userInfo: nil ];
217213 return [HTTPStubsResponse responseWithError: error];
218214 }];
219-
215+
220216 // UUID should be the same.
221217 [Leanplum track: @" sample" ];
222218 [Leanplum track: @" sample2" ];
@@ -229,9 +225,8 @@ - (void)test_uuid
229225 XCTAssertTrue (events.count == 3 );
230226 XCTAssertTrue ([events[0 ][@" uuid" ] isEqual: events[1 ][@" uuid" ]]);
231227 XCTAssertTrue ([events[0 ][@" uuid" ] isEqual: events[2 ][@" uuid" ]]);
232-
228+
233229 // After sending, the last one should have a different uuid.
234- [NSThread sleepForTimeInterval: 0.4 ];
235230 [Leanplum track: @" sample4" ];
236231
237232 [[LPOperationQueue serialQueue ] waitUntilAllOperationsAreFinished ];
@@ -242,7 +237,7 @@ - (void)test_uuid
242237 XCTAssertTrue ([events[0 ][@" uuid" ] isEqual: events[2 ][@" uuid" ]]);
243238 // Third one should be different.
244239 XCTAssertFalse ([events[0 ][@" uuid" ] isEqual: events[3 ][@" uuid" ]]);
245-
240+
246241 // Delete events on success.
247242 [HTTPStubs removeStub: stubs];
248243 [HTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest * _Nonnull request) {
@@ -252,14 +247,20 @@ - (void)test_uuid
252247 return [HTTPStubsResponse responseWithData: responseData statusCode: 200
253248 headers: @{@" Content-Type" :@" application/json" }];
254249 }];
255-
250+
251+ dispatch_semaphore_t semaphoreSample4 = dispatch_semaphore_create (0 );
252+ [LPNetworkEngine validate_operation: ^BOOL (LPNetworkOperation *operation) {
253+ dispatch_semaphore_signal (semaphoreSample4);
254+ return YES ;
255+ }];
256256 LPRequest *request2 = [LPRequestFactory createPostForApiMethod: @" sample4" params: nil ];
257257 [[LPRequestSender sharedInstance ] sendNow: request2];
258- [NSThread sleepForTimeInterval: 0.1 ];
258+ long timedOut = dispatch_semaphore_wait (semaphoreSample4, [LeanplumHelper default_dispatch_time ]);
259+ XCTAssertTrue (timedOut == 0 );
259260 [[LPOperationQueue serialQueue ] waitUntilAllOperationsAreFinished ];
260261 events = [LPEventDataManager eventsWithLimit: 10000 ];
261262 XCTAssertTrue (events.count == 0 );
262-
263+
263264 // UUID should be different after the 10k mark.
264265 for (int i=0 ; i<MAX_EVENTS_PER_API_CALL+1 ; i++) {
265266 [Leanplum track: @" s" ];
@@ -268,7 +269,7 @@ - (void)test_uuid
268269 events = [LPEventDataManager eventsWithLimit: 900000 ];
269270 XCTAssertTrue (events.count == MAX_EVENTS_PER_API_CALL+1 );
270271 XCTAssertFalse ([events[0 ][@" uuid" ] isEqual: events[MAX_EVENTS_PER_API_CALL][@" uuid" ]]);
271-
272+
272273 // Make sure there will be 2 requests.
273274 dispatch_semaphore_t semaphore = dispatch_semaphore_create (0 );
274275 NSInteger __block requestCount = 0 ;
@@ -282,28 +283,23 @@ - (void)test_uuid
282283 }];
283284 LPRequest *request3 = [LPRequestFactory createPostForApiMethod: @" test2" params: nil ];
284285 [[LPRequestSender sharedInstance ] sendNow: request3];
285- long timedOut = dispatch_semaphore_wait (semaphore, [LeanplumHelper default_dispatch_time ]);
286+ timedOut = dispatch_semaphore_wait (semaphore, [LeanplumHelper default_dispatch_time ]);
286287 XCTAssertTrue (timedOut == 0 );
287288 XCTAssertTrue (requestCount == 2 );
288289}
289290
290291- (void )test_response_index
291292{
292- [LeanplumHelper setup_method_swizzling ];
293- [LeanplumHelper start_production_test ];
294293 [LPEventDataManager deleteEventsWithLimit: 10000 ];
295- [LPNetworkEngine setupValidateOperation ];
296- [Leanplum_Reachability online: YES ];
297- [NSThread sleepForTimeInterval: 0.1 ];
298-
294+
299295 [HTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
300296 return [request.URL.host isEqualToString: API_HOST];
301297 } withStubResponse: ^HTTPStubsResponse * _Nonnull (NSURLRequest * _Nonnull request) {
302298 NSString *response_file = OHPathForFile (@" batch_response.json" , self.class );
303299 return [HTTPStubsResponse responseWithFileAtPath: response_file statusCode: 200
304300 headers: @{@" Content-Type" :@" application/json" }];
305301 }];
306-
302+
307303 // Make sure there are 3 events to send.
308304 XCTestExpectation *operationExpectation =
309305 [self expectationWithDescription: @" operationExpectation" ];
@@ -313,13 +309,7 @@ - (void)test_response_index
313309 [operationExpectation fulfill ];
314310 return YES ;
315311 }];
316-
317- // Freeze the operation queue for a bit to let events queue up.
318- NSOperationQueue *serialQueue = [LPOperationQueue serialQueue ];
319- [serialQueue addOperationWithBlock: ^{
320- [NSThread sleepForTimeInterval: 0.2 ];
321- }];
322-
312+
323313 // Queue up the events and test if the callback is in the correct index.
324314 XCTestExpectation *responseExpectation =
325315 [self expectationWithDescription: @" responseExpectation" ];
0 commit comments