Skip to content

Commit

Permalink
[Exporter.Geneva] Add test case for multithreading scenarios (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla authored Jul 30, 2022
1 parent e0fc583 commit c062c5a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
18 changes: 18 additions & 0 deletions test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Trace;
using Xunit;

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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)
{
Expand Down

0 comments on commit c062c5a

Please sign in to comment.