diff --git a/test/OpenTelemetry.OpAmp.Client.Tests/WsTransportTest.cs b/test/OpenTelemetry.OpAmp.Client.Tests/WsTransportTest.cs index e8aa1c5cf9..691093830a 100644 --- a/test/OpenTelemetry.OpAmp.Client.Tests/WsTransportTest.cs +++ b/test/OpenTelemetry.OpAmp.Client.Tests/WsTransportTest.cs @@ -245,7 +245,14 @@ public async Task WsTransport_DropsOversizedResponseWithoutDispatchingFrame() using var wsTransport = CreateTransport(opAmpServer.Endpoint, frameProcessor); await wsTransport.StartAsync(CancellationToken.None); - await wsTransport.SendAsync(FrameGenerator.GenerateMockAgentFrame().Frame, CancellationToken.None); + + // SendAsync can race with the receiver aborting the socket after detecting the oversized response. + // On .NET Framework this may surface as a WebSocketException (Aborted state) or OperationCanceledException. + var sendException = await Record.ExceptionAsync( + () => wsTransport.SendAsync(FrameGenerator.GenerateMockAgentFrame().Frame, CancellationToken.None)); + Assert.True( + sendException is null || sendException is WebSocketException || sendException is OperationCanceledException, + $"Unexpected exception from SendAsync: {sendException}"); Assert.True(opAmpServer.TryGetClientCloseStatus(TimeSpan.FromSeconds(5), out var closeStatus)); Assert.Equal(WebSocketCloseStatus.MessageTooBig, closeStatus); @@ -313,7 +320,14 @@ public async Task WsTransport_LogsOversizedResponseWarning() using var wsTransport = CreateTransport(opAmpServer.Endpoint, new FrameProcessor()); await wsTransport.StartAsync(CancellationToken.None); - await wsTransport.SendAsync(FrameGenerator.GenerateMockAgentFrame().Frame, CancellationToken.None); + + // SendAsync can race with the receiver aborting the socket after detecting the oversized response. + // On .NET Framework this may surface as a WebSocketException (Aborted state) or OperationCanceledException. + var sendException = await Record.ExceptionAsync( + () => wsTransport.SendAsync(FrameGenerator.GenerateMockAgentFrame().Frame, CancellationToken.None)); + Assert.True( + sendException is null || sendException is WebSocketException || sendException is OperationCanceledException, + $"Unexpected exception from SendAsync: {sendException}"); Assert.True(opAmpServer.TryGetClientCloseStatus(TimeSpan.FromSeconds(5), out var closeStatus)); Assert.Equal(WebSocketCloseStatus.MessageTooBig, closeStatus);