-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathGaugeTests.swift
101 lines (85 loc) · 2.99 KB
/
GaugeTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import XCTest
import NIO
@testable import Prometheus
@testable import CoreMetrics
final class GaugeTests: XCTestCase {
let baseLabels = DimensionLabels([("myValue", "labels")])
var prom: PrometheusClient!
var group: EventLoopGroup!
var eventLoop: EventLoop {
return group.next()
}
override func setUp() {
self.prom = PrometheusClient()
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
MetricsSystem.bootstrapInternal(PrometheusMetricsFactory(client: prom))
}
override func tearDown() {
self.prom = nil
try! self.group.syncShutdownGracefully()
}
func testGaugeSwiftMetrics() {
let gauge = Gauge(label: "my_gauge")
gauge.record(10)
gauge.record(12)
gauge.record(20)
let gaugeTwo = Gauge(label: "my_gauge", dimensions: [("myValue", "labels")])
gaugeTwo.record(10)
let promise = self.eventLoop.makePromise(of: String.self)
prom.collect(promise.succeed)
XCTAssertEqual(try! promise.futureResult.wait(), """
# TYPE my_gauge gauge
my_gauge 20.0
my_gauge{myValue=\"labels\"} 10.0\n
""")
}
func testGaugeTime() {
let gauge = prom.createGauge(forType: Double.self, named: "my_gauge")
let delay = 0.05
gauge.time {
Thread.sleep(forTimeInterval: delay)
}
// Using starts(with:) here since the exact subseconds might differ per-test.
XCTAssert(gauge.collect().starts(with: """
# TYPE my_gauge gauge
my_gauge \(isCITestRun ? "" : "0.05")
"""))
}
func testGaugeStandalone() {
let gauge = prom.createGauge(forType: Int.self, named: "my_gauge", helpText: "Gauge for testing", initialValue: 10)
XCTAssertEqual(gauge.get(), 10)
gauge.inc(10)
XCTAssertEqual(gauge.get(), 20)
gauge.dec(12)
XCTAssertEqual(gauge.get(), 8)
gauge.set(20)
gauge.inc(10, baseLabels)
XCTAssertEqual(gauge.get(), 20)
XCTAssertEqual(gauge.get(baseLabels), 20)
let gaugeTwo = prom.createGauge(forType: Int.self, named: "my_gauge", helpText: "Gauge for testing", initialValue: 10)
XCTAssertEqual(gaugeTwo.get(), 20)
gaugeTwo.inc()
XCTAssertEqual(gauge.get(), 21)
XCTAssertEqual(gaugeTwo.get(), 21)
XCTAssertEqual(gauge.collect(), """
# HELP my_gauge Gauge for testing
# TYPE my_gauge gauge
my_gauge 21
my_gauge{myValue="labels"} 20
""")
}
func testGaugeDoesNotReportWithNoLabelUsed() {
let gauge = prom.createGauge(forType: Int.self, named: "my_gauge")
gauge.inc(1, [("a", "b")])
XCTAssertEqual(gauge.collect(), """
# TYPE my_gauge gauge
my_gauge{a="b"} 1
""")
gauge.inc()
XCTAssertEqual(gauge.collect(), """
# TYPE my_gauge gauge
my_gauge 1
my_gauge{a="b"} 1
""")
}
}