forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputDebouncerTests.cs
132 lines (110 loc) · 5.02 KB
/
OutputDebouncerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Microsoft.PowerShell.EditorServices.Protocol.Server;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.PowerShell.EditorServices.Test.Protocol.Server
{
public class OutputDebouncerTests
{
[Fact]
public async Task OutputDebouncerAggregatesOutputEvents()
{
TestMessageSender messageSender = new TestMessageSender();
OutputDebouncer debouncer = new OutputDebouncer(messageSender);
await SendOutput(debouncer, "This ");
await SendOutput(debouncer, "is a ");
await SendOutput(debouncer, "test", true);
await SendOutput(debouncer, "Another line");
// Make sure no output events have been written yet
Assert.Empty(messageSender.OutputEvents);
// Wait for the output to be flushed
await Task.Delay(OutputDebouncer.OutputFlushInterval + 100);
// Write some more output after the first flush
await SendOutput(debouncer, "Another test line", true);
await SendOutput(debouncer, "for great justice");
// Assert that there's only one event with the expected string
Assert.Single(messageSender.OutputEvents);
Assert.Equal(
TestUtilities.NormalizeNewlines("This is a test\nAnother line"),
messageSender.OutputEvents[0].Output);
// Wait for the next output to be flushed
await Task.Delay(OutputDebouncer.OutputFlushInterval + 100);
// Assert that there's only one event with the expected string
Assert.Equal(2, messageSender.OutputEvents.Count);
Assert.Equal(
TestUtilities.NormalizeNewlines("Another test line\nfor great justice"),
messageSender.OutputEvents[1].Output);
}
[Fact]
public async Task OutputDebouncerDoesNotDuplicateOutput()
{
TestMessageSender messageSender = new TestMessageSender();
OutputDebouncer debouncer = new OutputDebouncer(messageSender);
// Send many messages in quick succession to ensure that
// output is not being duplicated in subsequent events.
for (int i = 1; i <= 50; i++)
{
await SendOutput(debouncer, "Output " + i, true);
if (i == 25)
{
// Artificially insert a delay to force another event
await Task.Delay(OutputDebouncer.OutputFlushInterval + 100);
}
}
// Wait for the final event to be written
await Task.Delay(OutputDebouncer.OutputFlushInterval + 100);
// Ensure that the two events start with the correct lines
Assert.Equal(2, messageSender.OutputEvents.Count);
Assert.Equal("Output 1", messageSender.OutputEvents[0].Output.Split('\n')[0].Trim('\r'));
Assert.Equal("Output 26", messageSender.OutputEvents[1].Output.Split('\n')[0].Trim('\r'));
}
private static Task SendOutput(
OutputDebouncer debouncer,
string outputText,
bool includeNewLine = false)
{
return debouncer.InvokeAsync(
new OutputWrittenEventArgs(
outputText,
includeNewLine,
OutputType.Normal,
ConsoleColor.White,
ConsoleColor.Black));
}
}
internal class TestMessageSender : IMessageSender
{
public List<OutputEventBody> OutputEvents { get; } = new List<OutputEventBody>();
public Task SendEventAsync<TParams, TRegistrationOptions>(
NotificationType<TParams, TRegistrationOptions> eventType,
TParams eventParams)
{
OutputEventBody outputEvent = eventParams as OutputEventBody;
if (outputEvent != null)
{
this.OutputEvents.Add(outputEvent);
}
return Task.FromResult(true);
}
public Task<TResult> SendRequestAsync<TParams, TResult, TError, TRegistrationOptions>(
RequestType<TParams, TResult, TError, TRegistrationOptions> requestType,
TParams requestParams, bool waitForResponse)
{
// Legitimately not implemented for these tests.
throw new NotImplementedException();
}
public Task<TResult> SendRequestAsync<TResult, TError, TRegistrationOptions>(RequestType0<TResult, TError, TRegistrationOptions> requestType0)
{
// Legitimately not implemented for these tests.
throw new NotImplementedException();
}
}
}