Skip to content

Commit

Permalink
Change default buffer option to single (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Nov 30, 2023
1 parent 3b5c86a commit 7cb7e54
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Sources/Snowplow/Configurations/EmitterConfiguration.swift
Expand Up @@ -16,7 +16,7 @@ import Foundation
@objc(SPEmitterConfigurationProtocol)
public protocol EmitterConfigurationProtocol: AnyObject {
/// Sets whether the buffer should send events instantly or after the buffer
/// has reached it's limit. By default, this is set to BufferOption Default.
/// has reached it's limit. By default, this is set to BufferOption single.
@objc
var bufferOption: BufferOption { get set }
/// Maximum number of events collected from the EventStore to be sent in a request.
Expand Down
22 changes: 14 additions & 8 deletions Sources/Snowplow/Emitter/BufferOption.swift
Expand Up @@ -16,14 +16,16 @@ import Foundation
/// An enum for buffer options.
@objc(SPBufferOption)
public enum BufferOption : Int {
/// Sends both GET and POST requests with only a single event. Can cause a spike in
/// network traffic if used in correlation with a large amount of events.
/// Sends both GET and POST requests with only a single event.
/// This is the default setting.
/// Can cause a spike in network traffic if used in correlation with a large amount of events.
case single = 1
/// Sends POST requests in groups of 10 events. This is the default amount of events too
/// package into a POST. All GET requests will still emit one at a time.
case defaultGroup = 10
/// Sends POST requests in groups of 25 events. Useful for situations where many events
/// need to be sent. All GET requests will still emit one at a time.
/// Sends POST requests in groups of 10 events.
/// All GET requests will still emit one at a time.
case smallGroup = 10
/// Sends POST requests in groups of 25 events.
/// Useful for situations where many events need to be sent.
/// All GET requests will still emit one at a time.
case largeGroup = 25
}

Expand All @@ -32,8 +34,12 @@ extension BufferOption {
switch value {
case "Single":
return .single
case "SmallGroup":
return .smallGroup
case "DefaultGroup":
return .defaultGroup
return .smallGroup
case "LargeGroup":
return .largeGroup
case "HeavyGroup":
return .largeGroup
default:
Expand Down
2 changes: 1 addition & 1 deletion Sources/Snowplow/Emitter/EmitterDefaults.swift
Expand Up @@ -21,6 +21,6 @@ public class EmitterDefaults {
public private(set) static var byteLimitGet = 40000
public private(set) static var byteLimitPost = 40000
public private(set) static var serverAnonymisation = false
public private(set) static var bufferOption: BufferOption = .defaultGroup
public private(set) static var bufferOption: BufferOption = .single
public private(set) static var retryFailedRequests = true
}
28 changes: 26 additions & 2 deletions Tests/Legacy Tests/LegacyTestEmitter.swift
Expand Up @@ -235,7 +235,7 @@ class LegacyTestEmitter: XCTestCase {
let payloads = generatePayloads(15)

let networkConnection = MockNetworkConnection(requestOption: .post, statusCode: 500)
let emitter = self.emitter(with: networkConnection, bufferOption: .defaultGroup)
let emitter = self.emitter(with: networkConnection, bufferOption: .smallGroup)

for i in 0..<14 {
addPayload(payloads[i], emitter)
Expand Down Expand Up @@ -274,7 +274,7 @@ class LegacyTestEmitter: XCTestCase {

func testEmitOversizeEventsPostAsGroup() {
let networkConnection = MockNetworkConnection(requestOption: .post, statusCode: 500)
let emitter = self.emitter(with: networkConnection, bufferOption: .defaultGroup)
let emitter = self.emitter(with: networkConnection, bufferOption: .single)
emitter.byteLimitPost = 5

let payloads = generatePayloads(15)
Expand Down Expand Up @@ -369,6 +369,30 @@ class LegacyTestEmitter: XCTestCase {

flush(emitter)
}

func testDoesntMakeRequestUnlessBufferSizeIsReached() {
let networkConnection = MockNetworkConnection(requestOption: .post, statusCode: 200)
let emitter = self.emitter(with: networkConnection, bufferOption: .smallGroup)
emitter.retryFailedRequests = false

for payload in generatePayloads(9) {
addPayload(payload, emitter)
}

Thread.sleep(forTimeInterval: 1)

// all events waiting in queue
XCTAssertEqual(9, dbCount(emitter))

addPayload(generatePayloads(1).first!, emitter)

Thread.sleep(forTimeInterval: 1)

// all events sent
XCTAssertEqual(0, dbCount(emitter))

flush(emitter)
}

// MARK: - Emitter builder

Expand Down

0 comments on commit 7cb7e54

Please sign in to comment.