From c546416ec00fc9d088bbba63b979b267ca5fea94 Mon Sep 17 00:00:00 2001 From: George Barnett Date: Mon, 4 Sep 2023 11:09:44 +0100 Subject: [PATCH] Avoid race in datagram tests Motivation: The datagram tests use a handler which sets its event loop in `channelRegistered`. A function on the handler is called which relies on the EL being set. However, this can race: the function can be called before `channelRegistered` is called. Modifications: - set the EL in `handlerAdded` Result: Fewer crashes --- .../NIOTSDatagramConnectionChannelTests.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tests/NIOTransportServicesTests/NIOTSDatagramConnectionChannelTests.swift b/Tests/NIOTransportServicesTests/NIOTSDatagramConnectionChannelTests.swift index ad5dced..0cc1e4b 100644 --- a/Tests/NIOTransportServicesTests/NIOTSDatagramConnectionChannelTests.swift +++ b/Tests/NIOTransportServicesTests/NIOTSDatagramConnectionChannelTests.swift @@ -67,10 +67,13 @@ final class ReadRecorder: ChannelInboundHandler { var readWaiters: [Int: EventLoopPromise<[DataType]>] = [:] var readCompleteCount = 0 + func handlerAdded(context: ChannelHandlerContext) { + self.loop = context.eventLoop + } + func channelRegistered(context: ChannelHandlerContext) { XCTAssertEqual(.fresh, self.state) self.state = .registered - self.loop = context.eventLoop } func channelActive(context: ChannelHandlerContext) {