diff --git a/test/OpenTelemetry.Sampler.AWS.Tests/MockServerRequestHandler.cs b/test/OpenTelemetry.Sampler.AWS.Tests/MockServerRequestHandler.cs index d639f17968..e3266ccc96 100644 --- a/test/OpenTelemetry.Sampler.AWS.Tests/MockServerRequestHandler.cs +++ b/test/OpenTelemetry.Sampler.AWS.Tests/MockServerRequestHandler.cs @@ -23,19 +23,29 @@ public void SetResponse(string path, string responseBody) public void Handle(HttpListenerContext context) { - var path = context.Request.Url?.AbsolutePath; - if (path != null && this.responses.TryGetValue(path, out var responseBody)) + try { - context.Response.StatusCode = 200; - context.Response.ContentType = "application/json"; - using var writer = new StreamWriter(context.Response.OutputStream); - writer.Write(responseBody); + var path = context.Request.Url?.AbsolutePath; + if (path != null && this.responses.TryGetValue(path, out var responseBody)) + { + context.Response.StatusCode = 200; + context.Response.ContentType = "application/json"; + +#if NET + using var writer = new StreamWriter(context.Response.OutputStream, leaveOpen: true); +#else + using var writer = new StreamWriter(context.Response.OutputStream, new System.Text.UTF8Encoding(false), 4096, leaveOpen: true); +#endif + writer.Write(responseBody); + } + else + { + context.Response.StatusCode = 404; + } } - else + finally { - context.Response.StatusCode = 404; + context.Response.Close(); } - - context.Response.Close(); } } diff --git a/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs b/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs index cda4ee9c90..6057ea0de7 100644 --- a/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs +++ b/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs @@ -47,12 +47,8 @@ public void TestSamplerWithDefaults() Assert.NotNull(xraySampler?.Client); } -#if NETFRAMEWORK - [Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/1219")] -#else [Fact] -#endif - public void TestSamplerUpdateAndSample() + public async Task TestSamplerUpdateAndSample() { // setup mock server var clock = new TestClock(); @@ -76,8 +72,7 @@ public void TestSamplerUpdateAndSample() // GetSamplingRules mock response requestHandler.SetResponse("/GetSamplingRules", File.ReadAllText("Data/GetSamplingRulesResponseOptionalFields.json")); - // rules will be polled in 10 milliseconds - Thread.Sleep(2000); + await Task.Delay(TimeSpan.FromSeconds(2)); // sampler will drop because rule has 0 reservoir and 0 fixed rate Assert.Equal(SamplingDecision.Drop, this.DoSample(sampler, "cat-service")); @@ -85,13 +80,29 @@ public void TestSamplerUpdateAndSample() // GetSamplingTargets mock response requestHandler.SetResponse("/SamplingTargets", File.ReadAllText("Data/GetSamplingTargetsResponseOptionalFields.json")); - // targets will be polled in 10 seconds - Thread.Sleep(13000); + var decision = SamplingDecision.Drop; + var expected = SamplingDecision.RecordAndSample; + + // targets should be polled in 10 seconds + using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(20))) + { + while (!cts.IsCancellationRequested) + { + decision = this.DoSample(sampler, "cat-service"); + + if (decision == expected) + { + break; + } + + await Task.Delay(TimeSpan.FromSeconds(1), cts.Token); + } + } // sampler will always sampler since target has 100% fixed rate - Assert.Equal(SamplingDecision.RecordAndSample, this.DoSample(sampler, "cat-service")); - Assert.Equal(SamplingDecision.RecordAndSample, this.DoSample(sampler, "cat-service")); - Assert.Equal(SamplingDecision.RecordAndSample, this.DoSample(sampler, "cat-service")); + Assert.Equal(expected, this.DoSample(sampler, "cat-service")); + Assert.Equal(expected, this.DoSample(sampler, "cat-service")); + Assert.Equal(expected, this.DoSample(sampler, "cat-service")); } private SamplingDecision DoSample(Trace.Sampler sampler, string serviceName) diff --git a/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRaySamplerClient.cs b/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRaySamplerClient.cs index 88a4f15638..c57a65fa21 100644 --- a/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRaySamplerClient.cs +++ b/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRaySamplerClient.cs @@ -16,7 +16,7 @@ public TestAWSXRaySamplerClient() { this.requestHandler = new MockServerRequestHandler(); this.mockServer = TestHttpServer.RunServer( - ctx => this.requestHandler.Handle(ctx), + this.requestHandler.Handle, out var host, out var port); this.client = new AWSXRaySamplerClient($"http://{host}:{port}");