Skip to content

Commit

Permalink
Update Flush policies (#256)
Browse files Browse the repository at this point in the history
* change visiblity and add unit test

* fix: fix interval policy test

* update tests

* update tests

---------

Co-authored-by: Niall Brennan <[email protected]>
Co-authored-by: Alan Charles <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2023
1 parent ad34912 commit 1d090a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
23 changes: 23 additions & 0 deletions Tests/Segment-Tests/FlushPolicy_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,27 @@ class FlushPolicyTests: XCTestCase {
// we now have ONE HA HA. TWO HA HA .. 3 ... HA HA THREE! Items to flush! <flys aways>
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)
}
}

0 comments on commit 1d090a0

Please sign in to comment.