Skip to content

Commit

Permalink
feat: support calibrate local timestamp (#57)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
  • Loading branch information
zhu-xiaowei and xiaoweii authored Mar 12, 2024
1 parent 0837645 commit a850795
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/Clickstream/Network/NetRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum NetRequest {
URLQueryItem(name: "appId", value: configuration.appId),
URLQueryItem(name: "compression", value: compression),
URLQueryItem(name: "event_bundle_sequence_id", value: String(describing: bundleSequenceId)),
URLQueryItem(name: "upload_timestamp", value: String(describing: Date().millisecondsSince1970)),
URLQueryItem(name: "hashCode", value: requestData.hashCode())
]

Expand Down
17 changes: 17 additions & 0 deletions Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EventRecorderTest: XCTestCase {
let testFailEndpoint = "http://localhost:8080/collect/fail"
let testSuccessWithDelayEndpoint = "http://localhost:8080/collect/success/delay"
let testHashCodeEndpoint = "http://localhost:8080/collect/hashcode"
let testUploadTimestampEndpoint = "http://localhost:8080/collect/timestamp"
var dbUtil: ClickstreamDBProtocol!
var clickstreamEvent: ClickstreamEvent!
var eventRecorder: EventRecorder!
Expand Down Expand Up @@ -426,6 +427,22 @@ class EventRecorderTest: XCTestCase {
let result = NetRequest.uploadEventWithURLSession(eventsJson: eventJson, configuration: clickstream.configuration, bundleSequenceId: 1)
XCTAssertTrue(result)
}

func testVerifyUploadTimestampInRequestParameter() {
clickstream.configuration.endpoint = testUploadTimestampEndpoint
server["/collect/timestamp"] = { request in
let queryParams = request.queryParams
// swift lambda for get the upload_timestamp value in queryParams dictionary
let uploadTimestamp = queryParams.first(where: { $0.0 == "upload_timestamp" })?.1
let requestUploadTimestamp = Int64(uploadTimestamp!)!
if Date().millisecondsSince1970 - requestUploadTimestamp < 5_000 {
return .ok(.text("Success"))
}
return .badRequest(.text("Fail"))
}
let result = NetRequest.uploadEventWithURLSession(eventsJson: "[]", configuration: clickstream.configuration, bundleSequenceId: 1)
XCTAssertTrue(result)
}
}

extension String {
Expand Down

0 comments on commit a850795

Please sign in to comment.