Skip to content

Commit 1d090a0

Browse files
niallzatoNiall Brennanalanjcharles
authored
Update Flush policies (#256)
* change visiblity and add unit test * fix: fix interval policy test * update tests * update tests --------- Co-authored-by: Niall Brennan <niall.brennan@segment.com> Co-authored-by: Alan Charles <acharles@twilio.com>
1 parent ad34912 commit 1d090a0

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Sources/Segment/Utilities/Policies/CountBasedFlushPolicy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class CountBasedFlushPolicy: FlushPolicy {
1212
internal var desiredCount: Int?
1313
@Atomic internal var count: Int = 0
1414

15-
init() { }
15+
public init() { }
1616

17-
init(count: Int) {
17+
public init(count: Int) {
1818
desiredCount = count
1919
}
2020

Sources/Segment/Utilities/Policies/IntervalBasedFlushPolicy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public class IntervalBasedFlushPolicy: FlushPolicy,
1515
internal var desiredInterval: TimeInterval?
1616
internal var flushTimer: QueueTimer? = nil
1717

18-
init() { }
18+
public init() { }
1919

20-
init(interval: TimeInterval) {
20+
public init(interval: TimeInterval) {
2121
desiredInterval = interval
2222
}
2323

Tests/Segment-Tests/FlushPolicy_Tests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,27 @@ class FlushPolicyTests: XCTestCase {
119119
// we now have ONE HA HA. TWO HA HA .. 3 ... HA HA THREE! Items to flush! <flys aways>
120120
XCTAssertTrue(countFlush.shouldFlush())
121121
}
122+
123+
func testIntervalBasedFlushPolicy() throws {
124+
let analytics = Analytics(configuration: Configuration(writeKey: "intervalFlushPolicy"))
125+
126+
//remove default flush policies
127+
analytics.removeAllFlushPolicies()
128+
129+
// make sure storage has no old events
130+
analytics.storage.hardReset(doYouKnowHowToUseThis: true)
131+
132+
let intervalFlush = IntervalBasedFlushPolicy(interval: 2)
133+
analytics.add(flushPolicy: intervalFlush)
134+
135+
waitUntilStarted(analytics: analytics)
136+
analytics.track(name: "blah", properties: nil)
137+
138+
XCTAssertTrue(analytics.hasUnsentEvents)
139+
140+
// sleep for 4 seconds for 2 second flush policy
141+
RunLoop.main.run(until: Date.init(timeIntervalSinceNow: 4))
142+
143+
XCTAssertFalse(analytics.hasUnsentEvents)
144+
}
122145
}

0 commit comments

Comments
 (0)