Use MemoryStream instead of stdout for outgoing messages during integration tests (#1696)

* filter

* test

* run all

* Redirect to memorystream

* revert temp changes
This commit is contained in:
Charles Gagnon
2022-09-16 15:38:53 -07:00
committed by GitHub
parent 006ac60923
commit 019481e8db
6 changed files with 24 additions and 15 deletions

View File

@@ -3,6 +3,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
// //
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.SqlTools.Hosting.Protocol.Serializers; using Microsoft.SqlTools.Hosting.Protocol.Serializers;
@@ -33,7 +34,9 @@ namespace Microsoft.SqlTools.Hosting.Protocol.Channel
/// Starts the channel and initializes the MessageDispatcher. /// Starts the channel and initializes the MessageDispatcher.
/// </summary> /// </summary>
/// <param name="messageProtocolType">The type of message protocol used by the channel.</param> /// <param name="messageProtocolType">The type of message protocol used by the channel.</param>
public void Start(MessageProtocolType messageProtocolType) /// <param name="inputStream">Optional stream to use for the input stream</param>
/// <param name="outputStream">Optional stream to use for the output stream</param>
public void Start(MessageProtocolType messageProtocolType, Stream? inputStream = null, Stream? outputStream = null)
{ {
IMessageSerializer messageSerializer = null; IMessageSerializer messageSerializer = null;
if (messageProtocolType == MessageProtocolType.LanguageServer) if (messageProtocolType == MessageProtocolType.LanguageServer)
@@ -45,7 +48,7 @@ namespace Microsoft.SqlTools.Hosting.Protocol.Channel
messageSerializer = new V8MessageSerializer(); messageSerializer = new V8MessageSerializer();
} }
this.Initialize(messageSerializer); this.Initialize(messageSerializer, inputStream, outputStream);
} }
/// <summary> /// <summary>
@@ -70,7 +73,9 @@ namespace Microsoft.SqlTools.Hosting.Protocol.Channel
/// assignment of the MessageReader and MessageWriter properties. /// assignment of the MessageReader and MessageWriter properties.
/// </summary> /// </summary>
/// <param name="messageSerializer">The IMessageSerializer to use for message serialization.</param> /// <param name="messageSerializer">The IMessageSerializer to use for message serialization.</param>
protected abstract void Initialize(IMessageSerializer messageSerializer); /// <param name="inputStream">Optional stream to use for the input stream</param>
/// <param name="outputStream">Optional stream to use for the output stream</param>
protected abstract void Initialize(IMessageSerializer messageSerializer, Stream? inputStream = null, Stream? outputStream = null);
/// <summary> /// <summary>
/// A method to be implemented by subclasses to handle shutdown /// A method to be implemented by subclasses to handle shutdown

View File

@@ -50,7 +50,7 @@ namespace Microsoft.SqlTools.Hosting.Protocol.Channel
} }
} }
protected override void Initialize(IMessageSerializer messageSerializer) protected override void Initialize(IMessageSerializer messageSerializer, Stream? inputStream = null, Stream? outputStream = null)
{ {
this.serviceProcess = new Process this.serviceProcess = new Process
{ {
@@ -73,8 +73,8 @@ namespace Microsoft.SqlTools.Hosting.Protocol.Channel
this.ProcessId = this.serviceProcess.Id; this.ProcessId = this.serviceProcess.Id;
// Open the standard input/output streams // Open the standard input/output streams
this.inputStream = this.serviceProcess.StandardOutput.BaseStream; this.inputStream = inputStream ?? this.serviceProcess.StandardOutput.BaseStream;
this.outputStream = this.serviceProcess.StandardInput.BaseStream; this.outputStream = outputStream ?? this.serviceProcess.StandardInput.BaseStream;
// Set up the message reader and writer // Set up the message reader and writer
this.MessageReader = this.MessageReader =

View File

@@ -20,7 +20,7 @@ namespace Microsoft.SqlTools.Hosting.Protocol.Channel
private Stream inputStream; private Stream inputStream;
private Stream outputStream; private Stream outputStream;
protected override void Initialize(IMessageSerializer messageSerializer) protected override void Initialize(IMessageSerializer messageSerializer, Stream? inputStream = null, Stream? outputStream = null)
{ {
#if !NanoServer #if !NanoServer
// Ensure that the console is using UTF-8 encoding // Ensure that the console is using UTF-8 encoding
@@ -29,8 +29,8 @@ namespace Microsoft.SqlTools.Hosting.Protocol.Channel
#endif #endif
// Open the standard input/output streams // Open the standard input/output streams
this.inputStream = System.Console.OpenStandardInput(); this.inputStream = inputStream ?? System.Console.OpenStandardInput();
this.outputStream = System.Console.OpenStandardOutput(); this.outputStream = outputStream ?? System.Console.OpenStandardOutput();
// Set up the reader and writer // Set up the reader and writer
this.MessageReader = this.MessageReader =

View File

@@ -6,6 +6,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.SqlTools.Hosting.Protocol.Channel; using Microsoft.SqlTools.Hosting.Protocol.Channel;
@@ -68,12 +69,12 @@ namespace Microsoft.SqlTools.Hosting.Protocol
/// <summary> /// <summary>
/// Initializes /// Initializes
/// </summary> /// </summary>
public void Initialize() public void Initialize(Stream inputStream = null, Stream outputStream = null)
{ {
if (!this.isInitialized) if (!this.isInitialized)
{ {
// Start the provided protocol channel // Start the provided protocol channel
this.protocolChannel.Start(this.messageProtocolType); this.protocolChannel.Start(this.messageProtocolType, inputStream, outputStream);
// Start the message dispatcher // Start the message dispatcher
this.MessageDispatcher = new MessageDispatcher(this.protocolChannel); this.MessageDispatcher = new MessageDispatcher(this.protocolChannel);

View File

@@ -40,6 +40,7 @@ using Microsoft.SqlTools.ServiceLayer.ModelManagement;
using Microsoft.SqlTools.ServiceLayer.TableDesigner; using Microsoft.SqlTools.ServiceLayer.TableDesigner;
using Microsoft.SqlTools.ServiceLayer.AzureBlob; using Microsoft.SqlTools.ServiceLayer.AzureBlob;
using Microsoft.SqlTools.ServiceLayer.ExecutionPlan; using Microsoft.SqlTools.ServiceLayer.ExecutionPlan;
using System.IO;
namespace Microsoft.SqlTools.ServiceLayer namespace Microsoft.SqlTools.ServiceLayer
{ {
@@ -52,7 +53,7 @@ namespace Microsoft.SqlTools.ServiceLayer
private static object lockObject = new object(); private static object lockObject = new object();
private static bool isLoaded; private static bool isLoaded;
internal static ServiceHost CreateAndStartServiceHost(SqlToolsContext sqlToolsContext) internal static ServiceHost CreateAndStartServiceHost(SqlToolsContext sqlToolsContext, Stream? inputStream = null, Stream? outputStream = null)
{ {
ServiceHost serviceHost = ServiceHost.Instance; ServiceHost serviceHost = ServiceHost.Instance;
lock (lockObject) lock (lockObject)
@@ -60,7 +61,7 @@ namespace Microsoft.SqlTools.ServiceLayer
if (!isLoaded) if (!isLoaded)
{ {
// Grab the instance of the service host // Grab the instance of the service host
serviceHost.Initialize(); serviceHost.Initialize(inputStream, outputStream);
InitializeRequestHandlersAndServices(serviceHost, sqlToolsContext); InitializeRequestHandlersAndServices(serviceHost, sqlToolsContext);

View File

@@ -5,6 +5,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -188,8 +189,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Common
var hostDetails = new HostDetails(hostName, hostProfileId, hostVersion); var hostDetails = new HostDetails(hostName, hostProfileId, hostVersion);
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails); SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails);
// Grab the instance of the service host // Initialize the ServiceHost, using a MemoryStream for the output stream so that we don't fill up the logs
ServiceHost serviceHost = HostLoader.CreateAndStartServiceHost(sqlToolsContext); // with a bunch of outgoing messages (which aren't used for anything during tests)
ServiceHost serviceHost = HostLoader.CreateAndStartServiceHost(sqlToolsContext, null, new MemoryStream());
} }
} }