Skip to content

Commit

Permalink
refactor: Add message container to payload (#99)
Browse files Browse the repository at this point in the history
* refactor: EventSender signatures

* Update usage in Demo App

* refactor: Add message container in payload

* Formatting
  • Loading branch information
fabriziodemaria authored Apr 23, 2024
1 parent 85b89ed commit f0bf363
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 31 deletions.
4 changes: 2 additions & 2 deletions ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ extension ConfidenceDemoApp {
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider, initialContext: ctx)
}
confidence.send(
definition: "all-types",
payload: [
eventName: "all-types",
message: [
"my_string": ConfidenceValue(string: "hello_from_world"),
"my_timestamp": ConfidenceValue(timestamp: Date()),
"my_bool": ConfidenceValue(boolean: true),
Expand Down
4 changes: 2 additions & 2 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class Confidence: ConfidenceEventSender {
self.parent = parent
}

public func send(definition: String, payload: ConfidenceStruct) {
eventSenderEngine.emit(definition: definition, payload: payload, context: getContext())
public func send(eventName: String, message: ConfidenceStruct) {
eventSenderEngine.emit(eventName: eventName, message: message, context: getContext())
}


Expand Down
2 changes: 1 addition & 1 deletion Sources/Confidence/ConfidenceEventSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import Foundation

/// Sends events to Confidence. Contextual data is appended to each event
public protocol ConfidenceEventSender: Contextual {
func send(definition: String, payload: ConfidenceStruct)
func send(eventName: String, message: ConfidenceStruct)
}
12 changes: 6 additions & 6 deletions Sources/Confidence/EventSenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ protocol FlushPolicy {
}

protocol EventSenderEngine {
func emit(definition: String, payload: ConfidenceStruct, context: ConfidenceStruct)
func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct)
func shutdown()
}

Expand Down Expand Up @@ -78,12 +78,12 @@ final class EventSenderEngineImpl: EventSenderEngine {
.store(in: &cancellables)
}

func emit(definition: String, payload: ConfidenceStruct, context: ConfidenceStruct) {
func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct) {
var mutablePayload = context
mutablePayload["message"] = ConfidenceValue(structure: message)
writeReqChannel.send(ConfidenceEvent(
name: definition,
payload: context.merging(payload) { _, new in
new
},
name: eventName,
payload: mutablePayload,
eventTime: Date.backport.now)
)
}
Expand Down
60 changes: 41 additions & 19 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),
"message": .init(integer: 1),
],
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(definition: "\(i)", payload: [:], context: [:])
}

wait(for: [expectation], timeout: 5)
let uploadRequest = try XCTUnwrap(uploader.calledRequest)
XCTAssertTrue(uploadRequest.map { $0.eventDefinition } == events.map { $0.name })
XCTAssertEqual(try XCTUnwrap(uploader.calledRequest)[0].eventDefinition, "my_event")
XCTAssertEqual(try XCTUnwrap(uploader.calledRequest)[0].payload, NetworkStruct(fields: [
"message": .structure(.init(fields: [
"a": .number(0.0),
"message": .number(1.0)
])),
"a": .number(2.0)
]))
cancellable.cancel()
}

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(definition: "Hello", payload: [:], context: [:])
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 All @@ -87,7 +109,7 @@ final class EventSenderEngineTest: XCTestCase {
storage: storage,
flushPolicies: flushPolicies
)
eventSenderEngine.emit(definition: "testEvent", payload: ConfidenceStruct(), context: ConfidenceStruct())
eventSenderEngine.emit(eventName: "testEvent", message: ConfidenceStruct(), context: ConfidenceStruct())
let expectation = expectation(description: "events batched")
storage.eventsRemoved{
expectation.fulfill()
Expand All @@ -113,7 +135,7 @@ final class EventSenderEngineTest: XCTestCase {
flushPolicies: flushPolicies
)

eventSenderEngine.emit(definition: "testEvent", payload: ConfidenceStruct(), context: ConfidenceStruct())
eventSenderEngine.emit(eventName: "testEvent", message: ConfidenceStruct(), context: ConfidenceStruct())

XCTAssertEqual(storage.isEmpty(), false)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceTests/Helpers/EventSenderEngineMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
@testable import Confidence

class EventSenderEngineMock: EventSenderEngine {
func emit(definition: String, payload: ConfidenceStruct, context: ConfidenceStruct) {
func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct) {
// NO-OP
}

Expand Down

0 comments on commit f0bf363

Please sign in to comment.