Skip to content

Commit 04da1b8

Browse files
authored
Unit tests for request batching (#417)
* added unit tests for request batching * clean db before tests
1 parent 3aac194 commit 04da1b8

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

Example/Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@
119119
A8F4479E2449769000F6978A /* LPNetworkOperationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = A8F4479D2449769000F6978A /* LPNetworkOperationTest.m */; };
120120
B449EB2A21F27AB2005C93B6 /* TiedPrioritiesDifferentDelay.json in Resources */ = {isa = PBXBuildFile; fileRef = B449EB2921F27AB2005C93B6 /* TiedPrioritiesDifferentDelay.json */; };
121121
B449EB2B21F27B0B005C93B6 /* TiedPrioritiesDifferentDelay.json in Resources */ = {isa = PBXBuildFile; fileRef = B449EB2921F27AB2005C93B6 /* TiedPrioritiesDifferentDelay.json */; };
122+
C9C0C87025898CE900346291 /* LPRequestBatchTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C9C0C86F25898CE900346291 /* LPRequestBatchTest.m */; };
123+
C9C0C87425898D0100346291 /* LPRequestSenderTimerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C9C0C87325898D0100346291 /* LPRequestSenderTimerTest.m */; };
122124
D9BF440B9FAFF9040FD1DB96 /* Pods_Leanplum_SDK_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A5CE8E3EA862202D684BDD6 /* Pods_Leanplum_SDK_Example.framework */; };
123125
E773031E24AB871E00EDF983 /* Leanplum-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = E773031D24AB871E00EDF983 /* Leanplum-Info.plist */; };
124126
/* End PBXBuildFile section */
@@ -248,6 +250,8 @@
248250
B5CCCA351D74B64D005ADEE1 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
249251
B5CCCA371D74B64D005ADEE1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
250252
B5CCCA391D74B64D005ADEE1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
253+
C9C0C86F25898CE900346291 /* LPRequestBatchTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPRequestBatchTest.m; sourceTree = "<group>"; };
254+
C9C0C87325898D0100346291 /* LPRequestSenderTimerTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPRequestSenderTimerTest.m; sourceTree = "<group>"; };
251255
E773031D24AB871E00EDF983 /* Leanplum-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Leanplum-Info.plist"; sourceTree = "<group>"; };
252256
F5CF859C9DD307380E6F9A30 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
253257
F84138A05B446D2B1EA99E98 /* Pods-Leanplum-SDK_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Leanplum-SDK_Example.debug.xcconfig"; path = "Target Support Files/Pods-Leanplum-SDK_Example/Pods-Leanplum-SDK_Example.debug.xcconfig"; sourceTree = "<group>"; };
@@ -291,6 +295,8 @@
291295
A8F4479D2449769000F6978A /* LPNetworkOperationTest.m */,
292296
65E189C321643631005E6B93 /* LPRequestSenderTest.m */,
293297
65E189C5216BDB82005E6B93 /* LPRequestFactoryTest.m */,
298+
C9C0C86F25898CE900346291 /* LPRequestBatchTest.m */,
299+
C9C0C87325898D0100346291 /* LPRequestSenderTimerTest.m */,
294300
07E5C86C1F052B7800A4B092 /* LPAppIconManagerTest.m */,
295301
07E5C86D1F052B7800A4B092 /* LPEventDataManagerTest.m */,
296302
07E5C86E1F052B7800A4B092 /* ExceptionsTest.m */,
@@ -849,6 +855,7 @@
849855
A824579B2411CB600044B1EB /* LPRichInterstitialMessageSnapshotTest.m in Sources */,
850856
9E53F6DF2474363D0097955F /* LPNotificationsManagerTest.m in Sources */,
851857
07E5C8BA1F052B7800A4B092 /* LPInboxTest.m in Sources */,
858+
C9C0C87425898D0100346291 /* LPRequestSenderTimerTest.m in Sources */,
852859
6A7815E324F6A89C004F3DC7 /* LPDeferMessageManagerTest.m in Sources */,
853860
07E5C8AB1F052B7800A4B092 /* LPActionManagerTest.m in Sources */,
854861
653192A121517D4F00D7DDDC /* LPFeatureFlagManagerTest.m in Sources */,
@@ -857,6 +864,7 @@
857864
07E5C8AF1F052B7800A4B092 /* LeanplumReachability+Category.m in Sources */,
858865
9CB691E11F1601BA002D83D8 /* LPEventCallbackManagerTest.m in Sources */,
859866
9EE5FFA524D08E6700153AF8 /* LPRequest+Extension.m in Sources */,
867+
C9C0C87025898CE900346291 /* LPRequestBatchTest.m in Sources */,
860868
07E5C8BE1F052B7800A4B092 /* LPUtilsTest.m in Sources */,
861869
07E5C8B01F052B7800A4B092 /* LPRequestSender+Categories.m in Sources */,
862870
);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// LPRequestBatchTest.m
3+
// Leanplum-SDK_Tests
4+
//
5+
// Copyright © 2020 Leanplum. All rights reserved.
6+
//
7+
8+
#import <XCTest/XCTest.h>
9+
#import <OCMock/OCMock.h>
10+
#import <OHHTTPStubs/HTTPStubs.h>
11+
#import <OHHTTPStubs/HTTPStubsPathHelpers.h>
12+
#import "LeanplumHelper.h"
13+
#import "LPDatabase.h"
14+
#import "LPRequestBatch.h"
15+
#import "LPRequestBatchFactory.h"
16+
#import "LPEventDataManager.h"
17+
#import "LPNetworkConstants.h"
18+
#import "LPOperationQueue.h"
19+
20+
@interface LPRequestBatchTest : XCTestCase
21+
22+
@end
23+
24+
@implementation LPRequestBatchTest
25+
26+
- (void)setUp
27+
{
28+
// Put setup code here. This method is called before the invocation of each test method in the class.
29+
id mockedDB = OCMClassMock([LPDatabase class]);
30+
OCMStub([mockedDB sqliteFilePath]).andReturn(@":memory:");
31+
}
32+
33+
- (void)tearDown
34+
{
35+
// Put teardown code here. This method is called after the invocation of each test method in the class.
36+
[LeanplumHelper clean_up];
37+
[HTTPStubs removeAllStubs];
38+
}
39+
40+
- (NSDictionary *)sampleData
41+
{
42+
return @{@"action":@"track", @"deviceId":@"123", @"userId":@"QA_TEST", @"client":@"ios",
43+
@"sdkVersion":@"3", @"devMode":@NO, @"time":@"1489007921.162919"};
44+
}
45+
46+
- (void)testGetEventsCount
47+
{
48+
[LPEventDataManager deleteEventsWithLimit:LP_MAX_EVENTS_PER_API_CALL];
49+
50+
LPRequestBatch *testBatch = [LPRequestBatchFactory createNextBatch];
51+
XCTAssertTrue([testBatch getEventsCount] == 0);
52+
53+
[LPEventDataManager addEvent:[self sampleData]];
54+
testBatch = [LPRequestBatchFactory createNextBatch];
55+
XCTAssertTrue([testBatch getEventsCount] == 1);
56+
57+
[LPEventDataManager addEvent:[self sampleData]];
58+
testBatch = [LPRequestBatchFactory createNextBatch];
59+
XCTAssertTrue([testBatch getEventsCount] == 2);
60+
61+
[LPRequestBatchFactory deleteFinishedBatch:testBatch];
62+
}
63+
64+
- (void)testBatchIsEmpty
65+
{
66+
[LPEventDataManager deleteEventsWithLimit:LP_MAX_EVENTS_PER_API_CALL];
67+
68+
LPRequestBatch *testBatch = [LPRequestBatchFactory createNextBatch];
69+
XCTAssertTrue([testBatch isEmpty]);
70+
71+
[LPEventDataManager addEvent:[self sampleData]];
72+
testBatch = [LPRequestBatchFactory createNextBatch];
73+
XCTAssertFalse([testBatch isEmpty]);
74+
75+
[LPRequestBatchFactory deleteFinishedBatch:testBatch];
76+
}
77+
78+
- (void)testBatchIsFull
79+
{
80+
[LPEventDataManager deleteEventsWithLimit:LP_MAX_EVENTS_PER_API_CALL];
81+
82+
LPRequestBatch *testBatch = [LPRequestBatchFactory createNextBatch];
83+
XCTAssertFalse([testBatch isFull]);
84+
85+
for (int i = 0; i < LP_MAX_EVENTS_PER_API_CALL; i++) {
86+
[LPEventDataManager addEvent:[self sampleData]];
87+
}
88+
89+
testBatch = [LPRequestBatchFactory createNextBatch];
90+
XCTAssertTrue([testBatch isFull]);
91+
[LPRequestBatchFactory deleteFinishedBatch:testBatch];
92+
}
93+
94+
@end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// LPRequestSenderTimerTest.m
3+
// Leanplum-SDK_Tests
4+
//
5+
// Copyright © 2020 Leanplum. All rights reserved.
6+
//
7+
8+
#import <XCTest/XCTest.h>
9+
#import "LPRequestSenderTimer.h"
10+
#import "NSTimer+Blocks.h"
11+
//#import "LeanplumHelper.h"
12+
13+
@interface LPRequestSenderTimerTest : XCTestCase
14+
15+
@end
16+
17+
@implementation LPRequestSenderTimerTest
18+
19+
- (void)setUp
20+
{
21+
// Put setup code here. This method is called before the invocation of each test method in the class.
22+
}
23+
24+
- (void)tearDown
25+
{
26+
// Put teardown code here. This method is called after the invocation of each test method in the class.
27+
}
28+
29+
- (void)testDefaultTimerIsSetTo15Min
30+
{
31+
XCTAssertTrue([LPRequestSenderTimer sharedInstance].timerInterval == AT_MOST_15_MINUTES);
32+
}
33+
34+
- (void)testStartTimerWillFireForTimeInterval
35+
{
36+
LPRequestSenderTimer *timer = [LPRequestSenderTimer sharedInstance];
37+
[timer setTimerInterval:AT_MOST_5_MINUTES];
38+
NSTimeInterval heartbeatInterval = timer.timerInterval; //testing in sec
39+
XCTestExpectation *timerExpectation = [self expectationWithDescription:@"timerExpectation"];
40+
[LPTimerBlocks scheduledTimerWithTimeInterval:heartbeatInterval block:^() {
41+
[timerExpectation fulfill];
42+
} repeats:NO];
43+
44+
[self waitForExpectationsWithTimeout:6 handler:nil];
45+
46+
}
47+
48+
@end

0 commit comments

Comments
 (0)