From 1d090a0ca33f196555a7d004f3e14528b0a5f70a Mon Sep 17 00:00:00 2001 From: Niall Brennan Date: Wed, 4 Oct 2023 16:05:02 +0100 Subject: [PATCH] Update Flush policies (#256) * change visiblity and add unit test * fix: fix interval policy test * update tests * update tests --------- Co-authored-by: Niall Brennan Co-authored-by: Alan Charles --- .../Policies/CountBasedFlushPolicy.swift | 4 ++-- .../Policies/IntervalBasedFlushPolicy.swift | 4 ++-- Tests/Segment-Tests/FlushPolicy_Tests.swift | 23 +++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) 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) + } }