Skip to content

Commit

Permalink
Fixed a bug, where the tracker would stop automatic dispatching. (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
brototyp authored Jul 23, 2017
1 parent 6cd6563 commit 7416221
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* **feature** Transmitting the Screen resolution to the Piwik Backend. [#149](https://github.com/piwik/piwik-sdk-ios/issues/149) (by @akshaykolte)
* **feature** Added a Logger that can log messages in different levels. [#147](https://github.com/piwik/piwik-sdk-ios/issues/147)
* **feature** Added macOS and tvOS compatibility. [#134](https://github.com/piwik/piwik-sdk-ios/issues/134)
* **bugfix** Fixed a bug, where the tracker would stop automatic dispatching. [#167](https://github.com/piwik/piwik-sdk-ios/issues/167)

## 4.0.0
* **feature** Renamed the Tracker to PiwikTracker. [#146](https://github.com/piwik/piwik-sdk-ios/issues/146)
Expand Down
2 changes: 1 addition & 1 deletion PiwikTracker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
1F1949F51E17B2A400458199 /* Fixtures */ = {
isa = PBXGroup;
children = (
1F6F0CDA1E61E377008170FC /* TrackerFixtures.swift */,
1F1949F71E17B2C800458199 /* MemoryQueueFixtures.swift */,
);
name = Fixtures;
Expand Down Expand Up @@ -167,7 +168,6 @@
1F1949F31E17B06600458199 /* MemoryQueueSpec.swift */,
1F6F0CD81E61E377008170FC /* DispatcherStub.swift */,
1F6F0CD91E61E377008170FC /* QueueStub.swift */,
1F6F0CDA1E61E377008170FC /* TrackerFixtures.swift */,
1F6F0CDB1E61E377008170FC /* TrackerSpec.swift */,
1FCA6D4B1DBE0B2F0033F01C /* Info.plist */,
);
Expand Down
12 changes: 9 additions & 3 deletions PiwikTracker/PiwikTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ final public class PiwikTracker: NSObject {
guard events.count > 0 else {
// there are no more events queued, finish dispatching
self.isDispatching = false
self.startDispatchTimer()
self.logger.info("Finished dispatching events")
return
}
Expand All @@ -104,16 +105,21 @@ final public class PiwikTracker: NSObject {
self.logger.info("Dispatched batch of \(events.count) events.")
self.dispatchBatch()
})
}, failure: { shouldContinue in
self.logger.warning("Failed dispatching events with error \(shouldContinue)")
}, failure: { error in
self.isDispatching = false
self.startDispatchTimer()
self.logger.warning("Failed dispatching events with error \(error)")
})
}
}

// MARK: dispatch timer

private let dispatchInterval: TimeInterval = 30.0 // Discussion: move this into a configuration?
public var dispatchInterval: TimeInterval = 30.0 {
didSet {
startDispatchTimer()
}
}
private var dispatchTimer: Timer?

private func startDispatchTimer() {
Expand Down
3 changes: 1 addition & 2 deletions PiwikTrackerTests/DispatcherStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ final class DispatcherStub: Dispatcher {

let userAgent: String? = "DispatcherStub"


func send(event: Event, success: @escaping ()->(), failure: @escaping (Error)->()){
func send(event: Event, success: @escaping () -> (), failure: @escaping (Error) -> ()) {
send(events: [event], success: success, failure: failure)
}

Expand Down
18 changes: 15 additions & 3 deletions PiwikTrackerTests/QueueStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@ final class QueueStub: Queue {
}

func enqueue(events: [Event], completion: (()->())?) {
queueEvents?(events, completion ?? {})
if let closure = queueEvents {
closure(events, completion ?? {})
} else {
completion?()
}
}

func first(limit: Int, completion: ([Event]) -> ()) {
firstItems?(limit, completion)
if let closure = firstItems {
closure(limit, completion)
} else {
completion([])
}
}

func remove(events: [Event], completion: () -> ()) {
removeEventsCallback?(events, completion)
if let closure = removeEventsCallback {
closure(events, completion)
} else {
completion()
}
}

}
6 changes: 6 additions & 0 deletions PiwikTrackerTests/TrackerFixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ struct TrackerFixture {
dispatcher.sendEvents = sendEvents
return TrackerFixture(queue: queue, dispatcher: dispatcher)
}

static func withSendEventsCallback(sendEvents: @escaping DispatcherStub.Callback.SendEvents) -> TrackerFixture {
let dispatcher = DispatcherStub()
dispatcher.sendEvents = sendEvents
return TrackerFixture(queue: MemoryQueue(), dispatcher: dispatcher)
}
}
24 changes: 23 additions & 1 deletion PiwikTrackerTests/TrackerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,31 @@ class TrackerSpec: QuickSpec {

}
it("should stop dispatching if the queue is empty") {

}
}
it("should start a new DispatchTimer if dispatching failed") {
var numberOfDispatches = 0
let trackerFixture = TrackerFixture.withSendEventsCallback() { events, success, failure in
numberOfDispatches += 1
failure(NSError(domain: "spec", code: 0))
}
trackerFixture.tracker.queue(event: EventFixture.event())
trackerFixture.tracker.dispatchInterval = 0.1
expect(numberOfDispatches).toEventually(equal(5), timeout: 5)
}
it("should start a new DispatchTimer if dispatching succeeded") {
var numberOfDispatches = 0
let trackerFixture = TrackerFixture.withSendEventsCallback() { events, success, failure in
numberOfDispatches += 1
success()
}
trackerFixture.tracker.queue(event: EventFixture.event())
let _ = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
trackerFixture.tracker.queue(event: EventFixture.event())
}
trackerFixture.tracker.dispatchInterval = 0.1
expect(numberOfDispatches).toEventually(equal(5), timeout: 5)
}
context("with an already dispatching tracker") {
it("should not ask the queue for events") {
// TODO
Expand Down

0 comments on commit 7416221

Please sign in to comment.