-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d394896
commit 6645150
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,53 @@ | ||
@testable import NIOPosix | ||
import NIOCore | ||
import NIOConcurrencyHelpers | ||
import XCTest | ||
|
||
final class RecorderDelegate: NIOEventLoopMetricsDelegate, Sendable { | ||
|
||
private let _infos: NIOLockedValueBox<[NIOEventLoopTickInfo]> = .init([]) | ||
|
||
var infos: [NIOEventLoopTickInfo] { | ||
_infos.withLockedValue {$0 } | ||
} | ||
|
||
func processedTick(info: NIOPosix.NIOEventLoopTickInfo) { | ||
_infos.withLockedValue { | ||
$0.append(info) | ||
} | ||
} | ||
} | ||
|
||
final class EventLoopMetricsDelegateTests: XCTestCase { | ||
func testMetricsDelegateNotCalledWhenNoEvents() { | ||
let delegate = RecorderDelegate() | ||
_ = MultiThreadedEventLoopGroup(numberOfThreads: 1, metricsDelegate: delegate) | ||
XCTAssertEqual(delegate.infos.count, 0) | ||
} | ||
|
||
func testMetricsDelegateTickInfo() { | ||
let delegate = RecorderDelegate() | ||
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1, metricsDelegate: delegate) | ||
let el = elg.any() | ||
let testStartTime = NIODeadline.now() | ||
|
||
XCTAssertEqual(delegate.infos.count, 0) | ||
|
||
let promise = el.makePromise(of: Void.self) | ||
el.scheduleTask(in: .seconds(1)){ | ||
promise.succeed() | ||
} | ||
promise.futureResult.whenSuccess { | ||
XCTAssertEqual(delegate.infos.count, 1) | ||
// 2 tasks: one is the scheduleTask call and one is the whenSuccess call | ||
XCTAssertEqual(delegate.infos.first?.numberOfTasks, 2) | ||
XCTAssertEqual(delegate.infos.first?.eventLoopID, ObjectIdentifier(el)) | ||
if let tickStartTime = delegate.infos.first?.startTime { | ||
let timeSinceStart = tickStartTime - testStartTime | ||
XCTAssertLessThan(timeSinceStart.nanoseconds, 1_000_000_000) // 1 second | ||
XCTAssertGreaterThan(timeSinceStart.nanoseconds, 0) | ||
} | ||
} | ||
try? promise.futureResult.wait() | ||
} | ||
} |