From c062c5a36bc86b3353511f53b74dc1af821eeb01 Mon Sep 17 00:00:00 2001 From: Utkarsh Umesan Pillai Date: Fri, 29 Jul 2022 17:48:59 -0700 Subject: [PATCH] [Exporter.Geneva] Add test case for multithreading scenarios (#549) --- .../GenevaLogExporterTests.cs | 18 ++++++++++++++++++ .../GenevaTraceExporterTests.cs | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs index eacb679eba..91d82c38c3 100644 --- a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs @@ -24,6 +24,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using System.Threading.Tasks; using Microsoft.Extensions.Logging; using OpenTelemetry.Logs; using Xunit; @@ -871,6 +872,23 @@ public void SuccessfulExportOnLinux() // Validation Assert.Equal(messagePackDataSize, receivedDataSize); + + logRecordList.Clear(); + + // Emit log on a different thread to test for multithreading scenarios + var thread = new Thread(() => + { + logger.LogInformation("Hello from another thread {food} {price}.", "artichoke", 3.99); + }); + thread.Start(); + thread.Join(); + + // logRecordList should have a singleLogRecord entry after the logger.LogInformation call + Assert.Single(logRecordList); + + messagePackDataSize = exporter.SerializeLogRecord(logRecordList[0]); + receivedDataSize = serverSocket.Receive(receivedData); + Assert.Equal(messagePackDataSize, receivedDataSize); } finally { diff --git a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaTraceExporterTests.cs b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaTraceExporterTests.cs index ba3929e81e..a75d66c5f6 100644 --- a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaTraceExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaTraceExporterTests.cs @@ -24,6 +24,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using System.Threading.Tasks; using OpenTelemetry.Trace; using Xunit; @@ -414,6 +415,7 @@ public void GenevaTraceExporter_Success_Linux() }) .Build(); using Socket serverSocket = server.Accept(); + serverSocket.ReceiveTimeout = 10000; // Create a test exporter to get MessagePack byte data for validation of the data received via Socket. var exporter = new GenevaTraceExporter(new GenevaExporterOptions @@ -429,7 +431,8 @@ public void GenevaTraceExporter_Success_Linux() // Emit trace and grab a copy of internal buffer for validation. var source = new ActivitySource(sourceName); - int messagePackDataSize; + int messagePackDataSize = 0; + using (var activity = source.StartActivity("Foo", ActivityKind.Internal)) { messagePackDataSize = exporter.SerializeActivity(activity); @@ -441,6 +444,20 @@ public void GenevaTraceExporter_Success_Linux() // Validation Assert.Equal(messagePackDataSize, receivedDataSize); + + // Create activity on a different thread to test for multithreading scenarios + var thread = new Thread(() => + { + using (var activity = source.StartActivity("ActivityFromAnotherThread", ActivityKind.Internal)) + { + messagePackDataSize = exporter.SerializeActivity(activity); + } + }); + thread.Start(); + thread.Join(); + + receivedDataSize = serverSocket.Receive(receivedData); + Assert.Equal(messagePackDataSize, receivedDataSize); } catch (Exception) {