diff --git a/Sources/Segment/Utilities/Policies/CountBasedFlushPolicy.swift b/Sources/Segment/Utilities/Policies/CountBasedFlushPolicy.swift index 2cadfa45..0756067f 100644 --- a/Sources/Segment/Utilities/Policies/CountBasedFlushPolicy.swift +++ b/Sources/Segment/Utilities/Policies/CountBasedFlushPolicy.swift @@ -12,9 +12,9 @@ public class CountBasedFlushPolicy: FlushPolicy { internal var desiredCount: Int? @Atomic internal var count: Int = 0 - init() { } + public init() { } - init(count: Int) { + public init(count: Int) { desiredCount = count } diff --git a/Sources/Segment/Utilities/Policies/IntervalBasedFlushPolicy.swift b/Sources/Segment/Utilities/Policies/IntervalBasedFlushPolicy.swift index 98a13264..aa5d4c9b 100644 --- a/Sources/Segment/Utilities/Policies/IntervalBasedFlushPolicy.swift +++ b/Sources/Segment/Utilities/Policies/IntervalBasedFlushPolicy.swift @@ -15,9 +15,9 @@ public class IntervalBasedFlushPolicy: FlushPolicy, internal var desiredInterval: TimeInterval? internal var flushTimer: QueueTimer? = nil - init() { } + public init() { } - init(interval: TimeInterval) { + public init(interval: TimeInterval) { desiredInterval = interval } diff --git a/Tests/Segment-Tests/FlushPolicy_Tests.swift b/Tests/Segment-Tests/FlushPolicy_Tests.swift index bb36908f..f3813b35 100644 --- a/Tests/Segment-Tests/FlushPolicy_Tests.swift +++ b/Tests/Segment-Tests/FlushPolicy_Tests.swift @@ -119,4 +119,27 @@ class FlushPolicyTests: XCTestCase { // we now have ONE HA HA. TWO HA HA .. 3 ... HA HA THREE! Items to flush! XCTAssertTrue(countFlush.shouldFlush()) } + + func testIntervalBasedFlushPolicy() throws { + let analytics = Analytics(configuration: Configuration(writeKey: "intervalFlushPolicy")) + + //remove default flush policies + analytics.removeAllFlushPolicies() + + // make sure storage has no old events + analytics.storage.hardReset(doYouKnowHowToUseThis: true) + + let intervalFlush = IntervalBasedFlushPolicy(interval: 2) + analytics.add(flushPolicy: intervalFlush) + + waitUntilStarted(analytics: analytics) + analytics.track(name: "blah", properties: nil) + + XCTAssertTrue(analytics.hasUnsentEvents) + + // sleep for 4 seconds for 2 second flush policy + RunLoop.main.run(until: Date.init(timeIntervalSinceNow: 4)) + + XCTAssertFalse(analytics.hasUnsentEvents) + } }