Skip to content

Commit

Permalink
Fix history batch bug, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christophhagen committed Dec 6, 2023
1 parent f13a7a2 commit 4de599d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Clairvoyant/Metric/LogFileWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ final class LogFileWriter<T> where T: MetricValue {
if start <= end {
return await getHistory(in: start...end, count: count)
} else {
return await getHistory(in: start...end, count: count)
return await getHistoryReversed(in: end...start, count: count)
}
}

Expand Down
30 changes: 30 additions & 0 deletions Tests/ClairvoyantTests/ClairvoyantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ final class ClairvoyantTests: XCTestCase {
let last = await metric.lastValue()
XCTAssertEqual(last?.value, 2)
}

func testReverseHistoryBatch() async throws {
let observer = createObserver()
let metric: Metric<Int> = observer.addMetric(id: "myInt")
let now = Date.now
let values = (1...100).reversed().map { Timestamped(value: $0, timestamp: now.advanced(by: TimeInterval(-$0))) }
try await metric.update(values)

let newestBatch = await metric.history(from: now, to: .distantPast, limit: 100)
XCTAssertEqual(newestBatch, values.suffix(100).reversed())
}

func testEncodedReverseHistoryBatch() async throws {
let observer = createObserver()
let metric: Metric<Int> = observer.addMetric(id: "myInt")
let now = Date.now
let values = (1...100).reversed().map { Timestamped(value: $0, timestamp: now.advanced(by: TimeInterval(-$0))) }
try await metric.update(values)

let newestBatchData = await metric.encodedHistoryData(from: now, to: .distantPast, maximumValueCount: 100)
let newestBatch: [Timestamped<Int>] = try JSONDecoder().decode(from: newestBatchData)
XCTAssertEqual(newestBatch, values.suffix(100).reversed())
}

}

Expand All @@ -253,3 +276,10 @@ private extension Date {
.init(timeIntervalSince1970: time)
}
}

extension Timestamped: Equatable where Value: Equatable {

public static func == (lhs: Timestamped, rhs: Timestamped) -> Bool {
lhs.value == rhs.value && lhs.timestamp == rhs.timestamp
}
}

0 comments on commit 4de599d

Please sign in to comment.