Skip to content

Instantly share code, notes, and snippets.

@matthid
Created April 12, 2013 15:14
Show Gist options
  • Save matthid/5372761 to your computer and use it in GitHub Desktop.
Save matthid/5372761 to your computer and use it in GitHub Desktop.
This gist shows a strange behaviour when using spidermonkey from within an NUnit Testcase.
// ----------------------------------------------------------------------------
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
// ----------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
namespace NUnitFail
{
[TestFixture()]
public class Test
{
[Test()]
public void TestCase ()
{
var psi = new ProcessStartInfo(
"js", ""
) {
StandardErrorEncoding = System.Text.Encoding.UTF8,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
var stdoutSignal = new ManualResetEventSlim(false);
var stderrSignal = new ManualResetEventSlim(false);
var _Process = Process.Start(psi);
string _StdOut = null, _StdErr = null;
ThreadPool.QueueUserWorkItem(new WaitCallback((_) => {
try {
_StdOut = _Process.StandardOutput.ReadToEnd();
} catch {
}
stdoutSignal.Set();
}));
ThreadPool.QueueUserWorkItem(new WaitCallback((_) => {
try {
_StdErr = _Process.StandardError.ReadToEnd();
} catch {
}
stderrSignal.Set();
}));
Action _JoinImpl = () => {
stdoutSignal.Wait();
stderrSignal.Wait();
stderrSignal.Dispose();
stderrSignal.Dispose();
};
//_Process.StandardInput.Encoding = System.Text.Encoding.UTF8;
_Process.StandardInput.Write("var t = {}");
_Process.StandardInput.Flush();
_Process.StandardInput.Close();
_JoinImpl();
_Process.WaitForExit();
var _ExitCode = _Process.ExitCode;
if (!string.IsNullOrWhiteSpace(_StdErr)) {
Assert.Fail(_StdErr);
}
Assert.AreEqual(_ExitCode, 0);
}
[Test()]
public void TestCaseRaw ()
{
var psi = new ProcessStartInfo(
"js", ""
) {
StandardErrorEncoding = System.Text.Encoding.UTF8,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
var stdoutSignal = new ManualResetEventSlim(false);
var stderrSignal = new ManualResetEventSlim(false);
var _Process = Process.Start(psi);
string _StdOut = null, _StdErr = null;
ThreadPool.QueueUserWorkItem(new WaitCallback((_) => {
try {
_StdOut = _Process.StandardOutput.ReadToEnd();
} catch {
}
stdoutSignal.Set();
}));
ThreadPool.QueueUserWorkItem(new WaitCallback((_) => {
try {
_StdErr = _Process.StandardError.ReadToEnd();
} catch {
}
stderrSignal.Set();
}));
Action _JoinImpl = () => {
stdoutSignal.Wait();
stderrSignal.Wait();
stderrSignal.Dispose();
stderrSignal.Dispose();
};
//_Process.StandardInput.Encoding = System.Text.Encoding.UTF8;
var bytes = System.Text.Encoding.UTF8.GetBytes("var t = {}");
_Process.StandardInput.BaseStream.Write(bytes, 0, bytes.Length);
_Process.StandardInput.BaseStream.Flush();
_Process.StandardInput.BaseStream.Close();
_JoinImpl();
_Process.WaitForExit();
var _ExitCode = _Process.ExitCode;
if (!string.IsNullOrWhiteSpace(_StdErr)) {
Assert.Fail(_StdErr);
}
Assert.AreEqual(_ExitCode, 0);
}
[Test()]
public void TestCaseRaw2 ()
{
var psi = new ProcessStartInfo(
"js", ""
) {
StandardErrorEncoding = System.Text.Encoding.UTF8,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
var stdoutSignal = new ManualResetEventSlim(false);
var stderrSignal = new ManualResetEventSlim(false);
var _Process = Process.Start(psi);
string _StdOut = null, _StdErr = null;
ThreadPool.QueueUserWorkItem(new WaitCallback((_) => {
try {
_StdOut = _Process.StandardOutput.ReadToEnd();
} catch {
}
stdoutSignal.Set();
}));
ThreadPool.QueueUserWorkItem(new WaitCallback((_) => {
try {
_StdErr = _Process.StandardError.ReadToEnd();
} catch {
}
stderrSignal.Set();
}));
Action _JoinImpl = () => {
stdoutSignal.Wait();
stderrSignal.Wait();
stderrSignal.Dispose();
stderrSignal.Dispose();
};
//_Process.StandardInput.Encoding = System.Text.Encoding.UTF8;
var writer = new System.IO.StreamWriter(_Process.StandardInput.BaseStream, System.Text.Encoding.UTF8);
writer.Write("var t = {}");
writer.Flush();
writer.Close();
_JoinImpl();
_Process.WaitForExit();
var _ExitCode = _Process.ExitCode;
if (!string.IsNullOrWhiteSpace(_StdErr)) {
Assert.Fail(_StdErr);
}
Assert.AreEqual(_ExitCode, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment