Skip to content

Commit

Permalink
Add unit tests for SelfDiagnosticsModule (#1706)
Browse files Browse the repository at this point in the history
* Add unit tests for SelfDiagnosticsModule

* Add a line for debugging in case the test fails

* Clean up config file after use in each unit test case.

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
  • Loading branch information
xiang17 and cijothomas authored Jan 29, 2021
1 parent ac6689c commit 416a6c1
Showing 1 changed file with 80 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,97 @@
using System.IO;
using System.Text;
using Xunit;
using Xunit.Abstractions;

namespace OpenTelemetry.Internal.Tests
{
public class SelfDiagnosticsConfigRefresherTest
{
private static readonly string ConfigFilePath = SelfDiagnosticsConfigParser.ConfigFileName;
private static readonly byte[] MessageOnNewFile = SelfDiagnosticsConfigRefresher.MessageOnNewFile;
private static readonly string MessageOnNewFileString = Encoding.UTF8.GetString(SelfDiagnosticsConfigRefresher.MessageOnNewFile);

private readonly ITestOutputHelper output;

public SelfDiagnosticsConfigRefresherTest(ITestOutputHelper output)
{
this.output = output;
}

[Fact]
[Trait("Platform", "Any")]
public void SelfDiagnosticsConfigRefresher_OmitAsConfigured()
{
try
{
CreateConfigFile();
using var configRefresher = new SelfDiagnosticsConfigRefresher();

// Emitting event of EventLevel.Warning
OpenTelemetrySdkEventSource.Log.SpanProcessorQueueIsExhausted();

int bufferSize = 512;
byte[] actualBytes = ReadFile(bufferSize);
string logText = Encoding.UTF8.GetString(actualBytes);
this.output.WriteLine(logText); // for debugging in case the test fails
Assert.StartsWith(MessageOnNewFileString, logText);

// The event was omitted
Assert.Equal('\0', (char)actualBytes[MessageOnNewFile.Length]);
}
finally
{
CleanupConfigFile();
}
}

[Fact]
[Trait("Platform", "Any")]
public void SelfDiagnosticsConfigRefresher_FileShare()
public void SelfDiagnosticsConfigRefresher_CaptureAsConfigured()
{
try
{
CreateConfigFile();
using var configRefresher = new SelfDiagnosticsConfigRefresher();

var outputFileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName) + "."
+ Process.GetCurrentProcess().Id + ".log";
var outputFilePath = Path.Combine(".", outputFileName);
using var file = File.Open(outputFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
byte[] actualBytes = new byte[MessageOnNewFile.Length];
file.Read(actualBytes, 0, actualBytes.Length);
Assert.Equal(MessageOnNewFile, actualBytes);
// Emitting event of EventLevel.Error
OpenTelemetrySdkEventSource.Log.SpanProcessorException("Event string sample", "Exception string sample");

int bufferSize = 512;
byte[] actualBytes = ReadFile(bufferSize);
string logText = Encoding.UTF8.GetString(actualBytes);
Assert.StartsWith(MessageOnNewFileString, logText);

// The event was captured
string logLine = logText.Substring(MessageOnNewFileString.Length);
string logMessage = ParseLogMessage(logLine);
string expectedMessage = "Unknown error in SpanProcessor event '{0}': '{1}'.{Event string sample}{Exception string sample}";
Assert.StartsWith(expectedMessage, logMessage);
}
finally
{
try
{
File.Delete(ConfigFilePath);
}
catch
{
}
CleanupConfigFile();
}
}

private static string ParseLogMessage(string logLine)
{
int timestampPrefixLength = "2020-08-14T20:33:24.4788109Z:".Length;
Assert.Matches(@"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{7}Z:", logLine.Substring(0, timestampPrefixLength));
return logLine.Substring(timestampPrefixLength);
}

private static byte[] ReadFile(int byteCount)
{
var outputFileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName) + "."
+ Process.GetCurrentProcess().Id + ".log";
var outputFilePath = Path.Combine(".", outputFileName);
using var file = File.Open(outputFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
byte[] actualBytes = new byte[byteCount];
file.Read(actualBytes, 0, byteCount);
return actualBytes;
}

private static void CreateConfigFile()
{
string configJson = @"{
Expand All @@ -66,5 +120,16 @@ private static void CreateConfigFile()
byte[] configBytes = Encoding.UTF8.GetBytes(configJson);
file.Write(configBytes, 0, configBytes.Length);
}

private static void CleanupConfigFile()
{
try
{
File.Delete(ConfigFilePath);
}
catch
{
}
}
}
}

0 comments on commit 416a6c1

Please sign in to comment.