Skip to content

Commit

Permalink
make it easier to see why the application of an E2E test couldn't start
Browse files Browse the repository at this point in the history
Signed-off-by: paule96 <paul-jeschke@outlook.com>
  • Loading branch information
paule96 committed Oct 23, 2024
1 parent 698b837 commit 96ba8ad
Showing 1 changed file with 79 additions and 5 deletions.
84 changes: 79 additions & 5 deletions test/Dapr.E2E.Test/DaprCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ namespace Dapr.E2E.Test
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
using Xunit.Abstractions;

public class DaprCommand
{
private readonly ITestOutputHelper output;
private readonly CircularBuffer<string> logBuffer = new CircularBuffer<string>(1000);

public DaprCommand(ITestOutputHelper output)
{
Expand Down Expand Up @@ -66,7 +68,11 @@ public void Run()
var done = outputReceived.WaitOne(this.Timeout);
if (!done)
{
throw new Exception($"Command: \"{this.Command}\" timed out while waiting for output: \"{this.OutputToMatch}\"");
var ex = new Exception($"Command: \"{this.Command}\" timed out while waiting for output: \"{this.OutputToMatch}\"");
// we add here the log buffer of the last 1000 lines, of the application log
// to make it easier to debug failing tests
ex.Data.Add("log", this.logBuffer.ToArray());
throw ex;
}
}

Expand All @@ -79,8 +85,7 @@ private void CheckOutput(object sendingProcess, DataReceivedEventArgs e)

try
{
// see: https://github.com/xunit/xunit/issues/2146
this.output.WriteLine(e.Data.TrimEnd(Environment.NewLine.ToCharArray()));
WriteLine(e.Data);
}
catch (InvalidOperationException)
{
Expand All @@ -101,12 +106,81 @@ private void OnErrorOutput(object sender, DataReceivedEventArgs e)

try
{
// see: https://github.com/xunit/xunit/issues/2146
this.output.WriteLine(e.Data.TrimEnd(Environment.NewLine.ToCharArray()));
WriteLine(e.Data);
}
catch (InvalidOperationException)
{
}
}

private void WriteLine(string message)
{
// see: https://github.com/xunit/xunit/issues/2146
var formattedMessage = message.TrimEnd(Environment.NewLine.ToCharArray());
this.output.WriteLine(formattedMessage);
this.logBuffer.Add(formattedMessage);
}
}

/// <summary>
/// A circular buffer that can be used to store a fixed number of items.
/// When the buffer is full, the oldest item is overwritten.
/// The buffer can be read in the same order as the items were added.
/// More information can be found <see href="https://en.wikipedia.org/wiki/Circular_buffer">here</see>.
/// </summary>
/// <remarks>
/// The buffer gets initialized by the call to the constructor and will allocate,
/// the memory for the buffer. The buffer is not resizable.
/// That means be carefull with <see cref="size"/>, because it can cause an <see cref="OutOfMemoryException"/>.
/// </remarks>
/// <typeparam name="T">The type of what the cicular buffer is off.</typeparam>
internal class CircularBuffer<T>{
private readonly int size;
private readonly T[] buffer;
private int readPosition = 0;
private int writePosition = 0;
/// <summary>
/// Initialize the buffer with the buffer size of <paramref name="size"/>.
/// </summary>
/// <param name="size">
/// The size the buffer will have
/// </param>
public CircularBuffer(int size)
{
this.size = size;
buffer = new T[size];
}
/// <summary>
/// Adds an item and move the write position to the next value
/// </summary>
/// <param name="item">The item that should be written.</param>
public void Add(T item)
{
buffer[writePosition] = item;
writePosition = (writePosition + 1) % size;
}
/// <summary>
/// Reads on value and move the position to the next value
/// </summary>
/// <returns></returns>
public T Read(){
var value = buffer[readPosition];
readPosition = (readPosition + 1) % size;
return value;
}
/// <summary>
/// Read the full buffer.
/// While the buffer is read, the read position is moved to the next value
/// </summary>
/// <returns></returns>
public T[] ToArray()
{
var result = new T[size];
for (int i = 0; i < size; i++)
{
result[i] = Read();
}
return result;
}
}
}

0 comments on commit 96ba8ad

Please sign in to comment.