-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ablity to redirect process output and error #161
Open
gravypower
wants to merge
7
commits into
mariotoffia:master
Choose a base branch
from
gravypower:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6ef3ede
Added ability to hook into DataReceived events for a process
gravypower 62b5120
Added AmbientContext and process manager
gravypower a0eb481
Merge remote-tracking branch 'mariotoffia/master'
gravypower 01716d9
Reduced the complexity of the process io redirection
gravypower 51a8b49
Added conditional access to the dataReceivedContext events.
gravypower bddf1d2
bumped dotnet sdk version
gravypower c59d9fe
Merge remote-tracking branch 'mariotoffia/master'
gravypower File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
243 changes: 243 additions & 0 deletions
243
Ductus.FluentDocker.Tests/AmbientContext/AmbientContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
using System; | ||
using System.Threading; | ||
using Ductus.FluentDocker.AmbientContex; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Ductus.FluentDocker.Tests.AmbientContext | ||
{ | ||
[TestClass] | ||
public class AmbientContext | ||
{ | ||
[TestMethod] | ||
public void Test1000000Nested() | ||
{ | ||
for (var i = 0; i < 1000000; i++) | ||
{ | ||
var thrVar = new ThreadVariable<string>(); | ||
using (thrVar.Use("my value")) | ||
{ | ||
var x = thrVar.Current; | ||
x = thrVar.Current; | ||
x = thrVar.Current; | ||
using (thrVar.Use("my value 2")) | ||
{ | ||
var x2 = thrVar.Current; | ||
x2 = thrVar.Current; | ||
x2 = thrVar.Current; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void TestSimple() | ||
{ | ||
var thrVar = new ThreadVariable<string>(); | ||
ShouldBeEmpty(thrVar); | ||
using (thrVar.Use("my value")) | ||
{ | ||
ShouldEqual(thrVar, "my value"); | ||
} | ||
|
||
ShouldBeEmpty(thrVar); | ||
} | ||
|
||
private static void ShouldEqual<T>(ThreadVariable<T> thrVar, T expected) | ||
{ | ||
Assert.IsTrue(thrVar.HasCurrent); | ||
Assert.AreEqual(thrVar.CurrentOrDefault, expected); | ||
Assert.AreEqual(thrVar.Current, expected); | ||
} | ||
|
||
private static void ShouldBeEmpty<T>(ThreadVariable<T> thrVar) | ||
{ | ||
Assert.IsFalse(thrVar.HasCurrent); | ||
Assert.ThrowsException<InvalidOperationException>(() => Assert.IsNull(thrVar.Current)); | ||
Assert.AreEqual(thrVar.CurrentOrDefault, default(T)); | ||
} | ||
|
||
[TestMethod] | ||
public void TestFallback() | ||
{ | ||
var thrVar = new ThreadVariable<string>("default"); | ||
ShouldEqual(thrVar, "default"); | ||
using (thrVar.Use("my value")) | ||
{ | ||
ShouldEqual(thrVar, "my value"); | ||
} | ||
|
||
ShouldEqual(thrVar, "default"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestValueTypeSupport() | ||
{ | ||
var thrVar = new ThreadVariable<bool>(); | ||
ShouldBeEmpty(thrVar); | ||
Assert.IsFalse(thrVar.CurrentOrDefault); | ||
using (thrVar.Use(true)) | ||
{ | ||
ShouldEqual(thrVar, true); | ||
} | ||
|
||
ShouldBeEmpty(thrVar); | ||
Assert.IsFalse(thrVar.CurrentOrDefault); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNullableSupport() | ||
{ | ||
var thrVar = new ThreadVariable<bool?>(); | ||
ShouldBeEmpty(thrVar); | ||
Assert.IsFalse(thrVar.CurrentOrDefault.HasValue); | ||
using (thrVar.Use(true)) | ||
{ | ||
ShouldEqual(thrVar, true); | ||
} | ||
|
||
ShouldBeEmpty(thrVar); | ||
Assert.IsFalse(thrVar.CurrentOrDefault.HasValue); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNested() | ||
{ | ||
var thrVar = new ThreadVariable<string>(); | ||
ShouldBeEmpty(thrVar); | ||
using (thrVar.Use("my value")) | ||
{ | ||
ShouldEqual(thrVar, "my value"); | ||
using (thrVar.Use("my value 2")) | ||
{ | ||
ShouldEqual(thrVar, "my value 2"); | ||
} | ||
|
||
ShouldEqual(thrVar, "my value"); | ||
} | ||
|
||
ShouldBeEmpty(thrVar); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMultipleValues() | ||
{ | ||
var thrVar1 = new ThreadVariable<int?>(); | ||
var thrVar2 = new ThreadVariable<int?>(); | ||
var thrVar3 = new ThreadVariable<int?>(); | ||
|
||
IDisposable scope1 = thrVar1.Use(1); | ||
IDisposable scope2 = thrVar2.Use(2); | ||
IDisposable scope3 = thrVar3.Use(3); | ||
|
||
Assert.AreEqual(thrVar1.Current, 1); | ||
Assert.AreEqual(thrVar2.Current, 2); | ||
Assert.AreEqual(thrVar3.Current, 3); | ||
|
||
scope1.Dispose(); | ||
scope2.Dispose(); | ||
scope3.Dispose(); | ||
|
||
ShouldBeEmpty(thrVar1); | ||
ShouldBeEmpty(thrVar1); | ||
ShouldBeEmpty(thrVar1); | ||
|
||
Assert.IsNull(thrVar1.CurrentOrDefault); | ||
Assert.IsNull(thrVar2.CurrentOrDefault); | ||
Assert.IsNull(thrVar3.CurrentOrDefault); | ||
|
||
Assert.IsFalse(thrVar1.CurrentOrDefault.HasValue); | ||
Assert.IsFalse(thrVar2.CurrentOrDefault.HasValue); | ||
Assert.IsFalse(thrVar3.CurrentOrDefault.HasValue); | ||
} | ||
|
||
[TestMethod] | ||
public void TestDisposeInIncorrectOrder() | ||
{ | ||
var thrVar = new ThreadVariable<int?>(); | ||
|
||
ShouldBeEmpty(thrVar); | ||
using (thrVar.Use(1)) // outer scope | ||
{ | ||
ShouldEqual(thrVar, 1); | ||
IDisposable middle = thrVar.Use(2); | ||
ShouldEqual(thrVar, 2); | ||
IDisposable inner = thrVar.Use(3); | ||
ShouldEqual(thrVar, 3); | ||
middle.Dispose(); | ||
ShouldEqual(thrVar, 1); | ||
|
||
/* When disposing 'inner', usually the value is | ||
* recovered to 'middle'.Value. | ||
* But 'middle' is already disposed, so it should | ||
* go back to 'outer' instead. | ||
* | ||
* Internally 'middle'.Dispose() should also | ||
* dispose inner scopes, e.g. 'inner', so that | ||
* 'inner'.Dispose() just does nothing. | ||
*/ | ||
inner.Dispose(); | ||
ShouldEqual(thrVar, 1); | ||
} | ||
|
||
ShouldBeEmpty(thrVar); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMultithread() | ||
{ | ||
var thrVar = new ThreadVariable<string>(); | ||
var letWi1 = new AutoResetEvent(false); | ||
var letWi2 = new AutoResetEvent(false); | ||
|
||
WaitCallback W2 = state => | ||
{ | ||
Console.WriteLine("WI 2 Startup"); | ||
|
||
ShouldBeEmpty(thrVar); | ||
Console.WriteLine("WI 2 is empty"); | ||
|
||
using (thrVar.Use("B")) | ||
{ | ||
Console.WriteLine("WI 2 Set"); | ||
ShouldEqual(thrVar, "B"); | ||
Console.WriteLine("WI 2 is B"); | ||
|
||
letWi1.Set(); // goto 001 | ||
|
||
// wait | ||
letWi2.WaitOne(); // 002 | ||
} | ||
|
||
Console.WriteLine("WI 2 Disposed"); | ||
ShouldBeEmpty(thrVar); | ||
Console.WriteLine("WI 2 is empty"); | ||
Console.WriteLine("WI 2 End"); | ||
letWi1.Set(); // goto 003 | ||
}; | ||
// start | ||
Console.WriteLine("WI 1 Startup"); | ||
using (thrVar.Use("A")) | ||
{ | ||
Console.WriteLine("WI 1 Set"); | ||
ShouldEqual(thrVar, "A"); | ||
Console.WriteLine("WI 1 is A"); | ||
ThreadPool.QueueUserWorkItem(W2); | ||
|
||
// wait | ||
letWi1.WaitOne(); // 001 | ||
ShouldEqual(thrVar, "A"); | ||
Console.WriteLine("WI 1 is still A"); | ||
letWi2.Set(); // goto 002 | ||
|
||
// wait | ||
letWi1.WaitOne(); // 003 | ||
ShouldEqual(thrVar, "A"); | ||
Console.WriteLine("WI 1 is still A"); | ||
} | ||
|
||
Console.WriteLine("WI 1 Disposed"); | ||
ShouldBeEmpty(thrVar); | ||
Console.WriteLine("WI 1 is empty"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using Ductus.FluentDocker.AmbientContex; | ||
using Ductus.FluentDocker.Executors.ProcessDataReceived; | ||
|
||
namespace Ductus.FluentDocker.AmbientContext | ||
{ | ||
public class DataReceivedContext | ||
{ | ||
private static readonly ThreadVariable<DataReceived> DataReceivedThreadVariable = new ThreadVariable<DataReceived>(new DataReceived()); | ||
public static DataReceived DataReceived => DataReceivedThreadVariable.Current; | ||
|
||
public static IDisposable UseProcessManager(DataReceived processManager) | ||
{ | ||
return DataReceivedThreadVariable.Use(processManager); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be any asserts in order to ensure that it contains the correct content?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was copied from the tests where I pulled the AmbientContext from. I can remove if you think that is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please do