Skip to content

Commit 0eb249a

Browse files
committed
Added more tests for stdout and stderror processing.
1 parent 3fc7559 commit 0eb249a

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

test/NodeJS/HttpNodeJSServiceIntegrationTests.cs

+53-7
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ public async void AllInvokeMethods_InvokeASpecificExportIfExportNameIsProvided()
390390

391391
// Tests the interaction between the Http server and OutOfProcessNodeJSService.TryCreateMessage
392392
[Theory(Timeout = _timeoutMS)]
393-
[MemberData(nameof(AllInvokeMethods_ReceiveAndLogMessages_Data))]
394-
public async void AllInvokeMethods_ReceiveAndLogMessages(string dummyLogArgument, string expectedResult)
393+
[MemberData(nameof(AllInvokeMethods_ReceiveAndLogStdoutOutput_Data))]
394+
public async void AllInvokeMethods_ReceiveAndLogStdoutOutput(string dummyLogArgument, string expectedResult)
395395
{
396396
// Arrange
397397
var resultStringBuilder = new StringBuilder();
@@ -400,7 +400,6 @@ public async void AllInvokeMethods_ReceiveAndLogMessages(string dummyLogArgument
400400
// Act
401401
await testSubject.
402402
InvokeFromStringAsync<string>($@"module.exports = (callback) => {{
403-
var test = {{}};
404403
console.log({dummyLogArgument});
405404
callback();
406405
@@ -426,13 +425,60 @@ await testSubject.
426425
Assert.Equal($"{nameof(LogLevel.Information)}: {expectedResult}\n", result, ignoreLineEndingDifferences: true);
427426
}
428427

429-
public static IEnumerable<object[]> AllInvokeMethods_ReceiveAndLogMessages_Data()
428+
public static IEnumerable<object[]> AllInvokeMethods_ReceiveAndLogStdoutOutput_Data()
430429
{
431430
return new object[][]
432431
{
433-
//new object[] { "'dummySingleLineString'", "dummySingleLineString" },
434-
//new object[] { "`dummy\nMultiline\nString\n`", "dummy\nMultiline\nString" }, // backtick for multiline strings in js
435-
new object[] { "`[Jering.Javascript.NodeJS: Listening on IP - ${test.a} Port - ${test.b}]`", "" },
432+
new object[] { "'dummySingleLineString'", "dummySingleLineString" },
433+
new object[] { "`dummy\nMultiline\nString\n`", "dummy\nMultiline\nString\n" }, // backtick for multiline strings in js
434+
new object[] { "''", "" },
435+
new object[] { "undefined", "undefined" },
436+
new object[] { "null", "null" },
437+
new object[] { "", "" },
438+
new object[] { "{}", "{}" }
439+
};
440+
}
441+
442+
[Theory(Timeout = _timeoutMS)]
443+
[MemberData(nameof(AllInvokeMethods_ReceiveAndLogStderrOutput_Data))]
444+
public async void AllInvokeMethods_ReceiveAndLogStderrOutput(string dummyLogArgument, string expectedResult)
445+
{
446+
// Arrange
447+
var resultStringBuilder = new StringBuilder();
448+
HttpNodeJSService testSubject = CreateHttpNodeJSService(resultStringBuilder);
449+
450+
// Act
451+
await testSubject.
452+
InvokeFromStringAsync<string>($@"module.exports = (callback) => {{
453+
console.error({dummyLogArgument});
454+
callback();
455+
}}").ConfigureAwait(false);
456+
// Disposing of HttpNodeServices causes Process.Kill and Process.WaitForExit(500) to be called on the node process, this gives it time for it to flush its output.
457+
//
458+
// TODO On Linux and macOS, Node.js does not flush stderr completely when Process.Kill is called, even if Process.WaitForExit is called immediately after.
459+
// Note that console.log just writes to stderr under the hood -https://nodejs.org/docs/latest-v8.x/api/console.html#console_console_log_data_args.
460+
// There flakiness causes this test to fail randomly. The whole stderr flushing issue seems like a persistent Node.js problem - https://github.com/nodejs/node/issues/6456.
461+
// Several attempts have been made to flush/write to stderr synchronously in the js above, to no avail.
462+
// The following Thread.Sleep(500) works almost all the time, but isn't a clean solution.
463+
Thread.Sleep(500);
464+
((IDisposable)_serviceProvider).Dispose();
465+
string result = resultStringBuilder.ToString();
466+
467+
// Assert
468+
Assert.Equal($"{nameof(LogLevel.Error)}: {expectedResult}\n", result, ignoreLineEndingDifferences: true);
469+
}
470+
471+
public static IEnumerable<object[]> AllInvokeMethods_ReceiveAndLogStderrOutput_Data()
472+
{
473+
return new object[][]
474+
{
475+
new object[] { "'dummySingleLineString'", "dummySingleLineString" },
476+
new object[] { "`dummy\nMultiline\nString\n`", "dummy\nMultiline\nString\n" }, // backtick for multiline strings in js
477+
new object[] { "''", "" },
478+
new object[] { "undefined", "undefined" },
479+
new object[] { "null", "null" },
480+
new object[] { "", "" },
481+
new object[] { "{}", "{}" }
436482
};
437483
}
438484

0 commit comments

Comments
 (0)