Skip to content

Commit

Permalink
Add internal queue wrappers for the event store
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Jan 30, 2024
1 parent ccb7172 commit fdfa0c8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/Core/Emitter/EmitterControllerImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class EmitterControllerImpl: Controller, EmitterController {
}
}

var eventStore: EventStore? {
var eventStore: EventStore {
return emitter.eventStore
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Core/InternalQueue/EmitterControllerIQWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class EmitterControllerIQWrapper: EmitterController {
get { return InternalQueue.sync { controller.maxEventStoreAge } }
set { InternalQueue.sync { controller.maxEventStoreAge = newValue } }
}

var eventStore: EventStore {
get { return InternalQueue.sync { EventStoreIQWrapper(eventStore: controller.eventStore) } }
}

// MARK: - Methods

Expand Down
52 changes: 52 additions & 0 deletions Sources/Core/InternalQueue/EventStoreIQWrapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2013-present Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License
// Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
// http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.

import Foundation

class EventStoreIQWrapper: NSObject, EventStore {

private let eventStore: EventStore

public init(eventStore: EventStore) {
self.eventStore = eventStore
}

func addEvent(_ payload: Payload) {
InternalQueue.sync { eventStore.addEvent(payload) }
}

func removeEvent(withId storeId: Int64) -> Bool {
return InternalQueue.sync { eventStore.removeEvent(withId: storeId) }
}

func removeEvents(withIds storeIds: [Int64]) -> Bool {
return InternalQueue.sync { eventStore.removeEvents(withIds: storeIds) }
}

func removeAllEvents() -> Bool {
return InternalQueue.sync { eventStore.removeAllEvents() }
}

func count() -> UInt {
return InternalQueue.sync { eventStore.count() }
}

func emittableEvents(withQueryLimit queryLimit: UInt) -> [EmitterEvent] {
return InternalQueue.sync { eventStore.emittableEvents(withQueryLimit: queryLimit) }
}

func removeOldEvents(maxSize: Int64, maxAge: TimeInterval) {
return InternalQueue.sync { eventStore.removeOldEvents(maxSize: maxSize, maxAge: maxAge) }
}

}
2 changes: 1 addition & 1 deletion Sources/Snowplow/Controllers/EmitterController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public protocol EmitterController: EmitterConfigurationProtocol {
var isSending: Bool { get }
/// The EventStore being used by the emitter.
@objc
var eventStore: EventStore? { get }
var eventStore: EventStore { get }
@objc
func flush()
/// Pause emitting events.
Expand Down
21 changes: 21 additions & 0 deletions Tests/Configurations/TestEmitterConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ class TestEmitterConfiguration: XCTestCase {
XCTAssertEqual(0, tracker.emitter?.dbCount)
}

func testAllowsAccessToTheEventStore() {
let networkConnection = MockNetworkConnection(requestOption: .post, statusCode: 200)
let networkConfig = NetworkConfiguration(networkConnection: networkConnection)

let tracker = createTracker(networkConfig: networkConfig, emitterConfig: EmitterConfiguration())

tracker.emitter?.pause()
for i in 0..<10 {
_ = tracker.track(Structured(category: "cat", action: "act").value(NSNumber(value: i)))
}
Thread.sleep(forTimeInterval: 0.5)

XCTAssertEqual(10, tracker.emitter?.dbCount)
XCTAssertEqual(10, tracker.emitter?.eventStore.count())

XCTAssertTrue(tracker.emitter?.eventStore.removeAllEvents() ?? false)

XCTAssertEqual(0, tracker.emitter?.dbCount)
XCTAssertEqual(0, tracker.emitter?.eventStore.count())
}

private func createTracker(networkConfig: NetworkConfiguration, emitterConfig: EmitterConfiguration) -> TrackerController {
let trackerConfig = TrackerConfiguration()
trackerConfig.installAutotracking = false
Expand Down

0 comments on commit fdfa0c8

Please sign in to comment.