Skip to content

Commit

Permalink
refactor: Add message container in payload
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 23, 2024
1 parent 03bf88d commit 0541d21
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Sources/Confidence/EventSenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ final class EventSenderEngineImpl: EventSenderEngine {
}

func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct) {
var mutablePayload = context
mutablePayload["message"] = ConfidenceValue(structure: message)
writeReqChannel.send(ConfidenceEvent(
name: eventName,
payload: context.merging(message) { _, new in
new
},
payload: mutablePayload,
eventTime: Date.backport.now)
)
}
Expand Down
52 changes: 37 additions & 15 deletions Tests/ConfidenceTests/EventSenderEngineTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import XCTest
@testable import Confidence

final class MinSizeFlushPolicy: FlushPolicy {
private var maxSize = 5
private var maxSize: Int
private var size = 0

init(maxSize: Int) {
self.maxSize = maxSize
}
func reset() {
size = 0
}
Expand Down Expand Up @@ -37,8 +41,8 @@ final class ImmidiateFlushPolicy: FlushPolicy {
}

final class EventSenderEngineTest: XCTestCase {
func testAddingEventsWithSizeFlushPolicyWorks() throws {
let flushPolicies = [MinSizeFlushPolicy()]
func testPayloadOnEmit() throws {
let flushPolicies = [MinSizeFlushPolicy(maxSize: 1)]
let uploader = EventUploaderMock()
let eventSenderEngine = EventSenderEngineImpl(
clientSecret: "CLIENT_SECRET",
Expand All @@ -51,25 +55,43 @@ final class EventSenderEngineTest: XCTestCase {
let cancellable = uploader.subject.sink { _ in
expectation.fulfill()
}
eventSenderEngine.emit(
eventName: "my_event",
message: [
"a":.init(integer: 0),

Check failure on line 61 in Tests/ConfidenceTests/EventSenderEngineTest.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
"message":.init(integer: 1),

Check failure on line 62 in Tests/ConfidenceTests/EventSenderEngineTest.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
],
context: [
"a": .init(integer: 2),
"message": .init(integer: 3) // the root "message" overrides this
])

var events: [ConfidenceEvent] = []
for i in 0..<5 {
events.append(ConfidenceEvent(
name: "\(i)",
payload: [:],
eventTime: Date.backport.now)
)
eventSenderEngine.emit(eventName: "\(i)", message: [:], context: [:])
}

wait(for: [expectation], timeout: 5)
let uploadRequest = try XCTUnwrap(uploader.calledRequest)
XCTAssertTrue(uploadRequest.map { $0.eventDefinition } == events.map { $0.name })
XCTAssertEqual(uploader.calledRequest![0].eventDefinition, "my_event")

Check failure on line 72 in Tests/ConfidenceTests/EventSenderEngineTest.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
XCTAssertEqual(uploader.calledRequest![0].payload, NetworkStruct(fields: [

Check failure on line 73 in Tests/ConfidenceTests/EventSenderEngineTest.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
"message": .structure(.init(fields: [
"a" : .number(0.0),

Check failure on line 75 in Tests/ConfidenceTests/EventSenderEngineTest.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
"message": .number(1.0)
])),
"a": .number(2.0)
]))
}

func testAddingEventsWithSizeFlushPolicyWorks() throws {
let flushPolicies = [MinSizeFlushPolicy(maxSize: 5)]
let uploader = EventUploaderMock()
let eventSenderEngine = EventSenderEngineImpl(
clientSecret: "CLIENT_SECRET",
uploader: uploader,
storage: EventStorageMock(),
flushPolicies: flushPolicies
)

uploader.reset()
eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:])
// TODO: We need to wait for writeReqChannel to complete to make this test meaningful
XCTAssertNil(uploader.calledRequest)
cancellable.cancel()
}

func testRemoveEventsFromStorageOnBadRequest() throws {
Expand Down

0 comments on commit 0541d21

Please sign in to comment.