Skip to content

Commit

Permalink
test: more tests and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetodream committed Apr 21, 2024
1 parent e9ce7cf commit 6f086ad
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 227 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
env:
XCT_LOKI_URL: http://loki:3100
- name: Prepare Code Coverage
run: llvm-cov export -format="lcov" .build/debug/swift-log-lokiPackageTests.xctest -instr-profile .build/debug/codecov/default.profdata > info.lcov
run: llvm-cov export -format="lcov" .build/debug/swift-log-lokiPackageTests.xctest -instr-profile .build/debug/codecov/default.profdata -ignore-filename-regex="\/.pb.swift\/" > info.lcov
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
1 change: 1 addition & 0 deletions Sources/LoggingLoki/LokiLogProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import NIOHTTP1
import AsyncHTTPClient
import ServiceLifecycle
import AsyncAlgorithms
import NIOConcurrencyHelpers

public struct LokiLogProcessorConfiguration: Sendable {
/// The loki server URL, eg. `http://localhost:3100`.
Expand Down
178 changes: 0 additions & 178 deletions Sources/LoggingLoki/Utility/NIOLock.swift

This file was deleted.

46 changes: 0 additions & 46 deletions Sources/LoggingLoki/Utility/NIOLockedValueBox.swift

This file was deleted.

58 changes: 56 additions & 2 deletions Tests/LoggingLokiTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@ import NIOCore
import NIOHTTP1
import Atomics
import AsyncHTTPClient
import NIOConcurrencyHelpers
@testable import LoggingLoki

final class InspectableTransport: LokiTransport {
let actual = HTTPClient.shared

let transported = ManagedAtomic(0)

let errored = ManagedAtomic(0)
let errors: NIOLockedValueBox<[Error]> = NIOLockedValueBox([])

func transport(_ data: ByteBuffer, url: String, headers: HTTPHeaders) async throws {
try await actual.transport(data, url: url, headers: headers)
transported.wrappingIncrement(ordering: .relaxed)
do {
try await actual.transport(data, url: url, headers: headers)
transported.wrappingIncrement(ordering: .relaxed)
} catch {
errored.wrappingIncrement(ordering: .relaxed)
errors.withLockedValue { $0.append(error) }
}
}
}

final class BadRequestTransformer: LokiTransformer {
func transform(_ entries: [BatchEntry], headers: inout HTTPHeaders) throws -> ByteBuffer {
headers.add(name: "Content-Type", value: "application/json")
var buffer = ByteBuffer()
buffer.writeString("bad_request :(")
try buffer.writeJSONEncodable(LokiRequest.from(entries: entries))
return buffer
}
}

Expand All @@ -25,6 +44,41 @@ final class IntegrationTests: XCTestCase {
try await runHappyPath(LokiJSONTransformer())
}

func testBadRequest() async throws {
try await withThrowingDiscardingTaskGroup { group in
let clock = TestClock()
let transport = InspectableTransport()
let processor = LokiLogProcessor(
configuration: .init(lokiURL: env("XCT_LOKI_URL") ?? "http://localhost:3100", maxBatchTimeInterval: .seconds(10)),
transport: transport,
transformer: BadRequestTransformer(),
clock: clock
)
var sleepCalls = clock.sleepCalls.makeAsyncIterator()
group.addTask {
try await processor.run()
}
let handler = LokiLogHandler(label: "com.timozacherl.swift-log-loki-tests", processor: processor)
logLine(handler: handler)
await sleepCalls.next()

// move forward in time until max batch time interval is exceeded
clock.advance(by: .seconds(5)) // tick
await sleepCalls.next()
clock.advance(by: .seconds(5)) // tick
await sleepCalls.next()

await sleepCalls.next() // export
XCTAssertEqual(transport.transported.load(ordering: .relaxed), 0)
XCTAssertEqual(transport.errored.load(ordering: .relaxed), 1)
let errors = transport.errors.withLockedValue { $0 }
let error = try XCTUnwrap(errors.first as? LokiResponseError)
XCTAssertEqual(error.response.status, .badRequest)

group.cancelAll()
}
}

func runHappyPath(_ transformer: LokiTransformer, file: StaticString = #filePath, line: UInt = #line) async throws {
try await withThrowingDiscardingTaskGroup { group in
let clock = TestClock()
Expand Down
18 changes: 18 additions & 0 deletions Tests/LoggingLokiTests/LokiLogProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ final class LokiLogProcessorTests: XCTestCase {
XCTAssertNil(formatted.metadata)
XCTAssertEqual(formatted.line, #"INFO: My log message [additional_key: value with whitespace, basic_key: basic_value]"#)
}

func testLogFmtFormatEmptyMetadata() {
var configuration = LokiLogProcessorConfiguration(
lokiURL: "http://localhost:3100",
metadataFormat: .logfmt
)
configuration.encoding = .json
let processor = LokiLogProcessor(configuration: configuration)
let raw = LokiLog(
timestamp: .init(),
level: .info,
message: "My log message",
metadata: [:]
)
let formatted = processor.makeLog(raw)
XCTAssertNil(formatted.metadata)
XCTAssertEqual(formatted.line, #"[INFO] message="My log message""#)
}
}

0 comments on commit 6f086ad

Please sign in to comment.