Skip to content

Commit 0681d37

Browse files
committed
Add debug logging for integration test streams
1 parent 2846bef commit 0681d37

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using System.Text;
8+
9+
namespace PowerShellEditorServices.Test.E2E
10+
{
11+
internal class LoggingStream : Stream
12+
{
13+
private static readonly string s_banner = new('=', 20);
14+
15+
private readonly Stream _underlyingStream;
16+
17+
public LoggingStream(Stream underlyingStream)
18+
{
19+
_underlyingStream = underlyingStream;
20+
}
21+
22+
public override bool CanRead => _underlyingStream.CanRead;
23+
24+
public override bool CanSeek => _underlyingStream.CanSeek;
25+
26+
public override bool CanWrite => _underlyingStream.CanWrite;
27+
28+
public override long Length => _underlyingStream.Length;
29+
30+
public override long Position { get => _underlyingStream.Position; set => _underlyingStream.Position = value; }
31+
32+
public override void Flush() => _underlyingStream.Flush();
33+
34+
public override int Read(byte[] buffer, int offset, int count)
35+
{
36+
int actualCount = _underlyingStream.Read(buffer, offset, count);
37+
LogData("READ", buffer, offset, actualCount);
38+
return actualCount;
39+
}
40+
41+
public override long Seek(long offset, SeekOrigin origin) => _underlyingStream.Seek(offset, origin);
42+
43+
public override void SetLength(long value) => _underlyingStream.SetLength(value);
44+
45+
public override void Write(byte[] buffer, int offset, int count)
46+
{
47+
LogData("WRITE", buffer, offset, count);
48+
_underlyingStream.Write(buffer, offset, count);
49+
}
50+
51+
private static void LogData(string header, byte[] buffer, int offset, int count)
52+
{
53+
Debug.WriteLine($"{header} |{s_banner.Substring(0, Math.Max(s_banner.Length - header.Length - 2, 0))}");
54+
string data = Encoding.UTF8.GetString(buffer, offset, count);
55+
Debug.WriteLine(data);
56+
Debug.WriteLine(s_banner);
57+
Debug.WriteLine("\n");
58+
}
59+
}
60+
}

test/PowerShellEditorServices.Test.E2E/Processes/ServerProcess.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ namespace PowerShellEditorServices.Test.E2E
1515
public abstract class ServerProcess : IDisposable
1616
{
1717
private readonly ISubject<System.Reactive.Unit> _exitedSubject;
18+
19+
private readonly Lazy<Stream> _inStreamLazy;
20+
21+
private readonly Lazy<Stream> _outStreamLazy;
22+
1823
/// <summary>
1924
/// Create a new <see cref="ServerProcess"/>.
2025
/// </summary>
@@ -37,6 +42,9 @@ protected ServerProcess(ILoggerFactory loggerFactory)
3742
ServerExitCompletion.SetResult(null); // Start out as if the server has already exited.
3843

3944
Exited = _exitedSubject = new AsyncSubject<System.Reactive.Unit>();
45+
46+
_inStreamLazy = new Lazy<Stream>(() => new LoggingStream(GetInputStream()));
47+
_outStreamLazy = new Lazy<Stream>(() => new LoggingStream(GetOutputStream()));
4048
}
4149

4250
/// <summary>
@@ -105,21 +113,25 @@ protected virtual void Dispose(bool disposing)
105113
/// </summary>
106114
public Task HasExited => ServerExitCompletion.Task;
107115

116+
protected abstract Stream GetInputStream();
117+
118+
protected abstract Stream GetOutputStream();
119+
108120
/// <summary>
109121
/// The server's input stream.
110122
/// </summary>
111123
/// <remarks>
112124
/// The connection will write to the server's input stream, and read from its output stream.
113125
/// </remarks>
114-
public abstract Stream InputStream { get; }
126+
public Stream InputStream => _inStreamLazy.Value;
115127

116128
/// <summary>
117129
/// The server's output stream.
118130
/// </summary>
119131
/// <remarks>
120132
/// The connection will read from the server's output stream, and write to its input stream.
121133
/// </remarks>
122-
public abstract Stream OutputStream { get; }
134+
public Stream OutputStream => _outStreamLazy.Value;
123135

124136
/// <summary>
125137
/// Start or connect to the server.

0 commit comments

Comments
 (0)