-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
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
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,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Z.Linq; | ||
using Z.Linq.Async; | ||
using Z.Test.Linq.Async; | ||
using Z.Test.Linq.Async.Model; | ||
|
||
namespace Z.Lab.LinqAsync.NetCore | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var list = new List<int> { 1, 2, 3, 4, 5 }; | ||
var enumerable = new TestEnumerable<int>(list); | ||
var predicateAsync = new TestPredicateAsync<int>(i => | ||
{ | ||
switch (i) | ||
{ | ||
case 1: | ||
return Task.Delay(600).ContinueWith(task => true); | ||
case 2: | ||
return Task.Delay(200).ContinueWith(task => true); | ||
case 3: | ||
return Task.Delay(800).ContinueWith(task => true); | ||
case 4: | ||
return Task.Delay(500).ContinueWith(task => true); | ||
case 5: | ||
return Task.Delay(300).ContinueWith(task => true); | ||
default: | ||
throw new Exception("Oops!"); | ||
} | ||
}); | ||
|
||
var defaultValue = LinqAsyncManager.DefaultValue.OrderByPredicateCompletion; | ||
|
||
try | ||
{ | ||
LinqAsyncManager.DefaultValue.OrderByPredicateCompletion = false; | ||
|
||
var result = enumerable.WhereAsync(x => predicateAsync.Predicate(x)).ToList().Result; | ||
|
||
//// MUST have 5 iterations for enumerable | ||
//Assert.AreEqual(4, enumerable.CurrentIndex); | ||
|
||
//// MUST be ordered with original order | ||
//Assert.AreEqual(1, result[0]); | ||
//Assert.AreEqual(2, result[1]); | ||
//Assert.AreEqual(3, result[2]); | ||
//Assert.AreEqual(4, result[3]); | ||
//Assert.AreEqual(5, result[4]); | ||
} | ||
finally | ||
{ | ||
LinqAsyncManager.DefaultValue.OrderByPredicateCompletion = defaultValue; | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/lab/Z.Lab.LinqAsync.NetCore/Z.Lab.LinqAsync.NetCore.csproj
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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Z.Linq.Async.NetStandard20\Z.Linq.Async.NetStandard20.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,39 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Z.Test.Linq.Async.Model; | ||
|
||
namespace Z.Test.Linq.Async | ||
{ | ||
public class TestEnumerable<T> : IEnumerable<T> | ||
{ | ||
public TestEnumerable(List<T> originalValues) | ||
{ | ||
CurrentIndex = -1; | ||
OriginalValues = originalValues; | ||
} | ||
|
||
public TestEnumerable(List<T> originalValues, Func<T, bool> errorPredicate) | ||
{ | ||
CurrentIndex = -1; | ||
ErrorPredicate = errorPredicate; | ||
OriginalValues = originalValues; | ||
} | ||
|
||
public List<T> OriginalValues { get; set; } | ||
|
||
public int CurrentIndex { get; set; } | ||
|
||
public Func<T, bool> ErrorPredicate { get; set; } | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return new TestEnumerator<T>(this); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestEnumerableAsync.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,40 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Z.Test.Linq.Async.Model; | ||
|
||
namespace Z.Test.Linq.Async | ||
{ | ||
public class TestEnumerableAsync<T, TItem> : IEnumerable<T> where T : Task<TItem> | ||
{ | ||
public TestEnumerableAsync(List<TItem> originalValues) | ||
{ | ||
CurrentIndex = -1; | ||
OriginalValues = originalValues; | ||
} | ||
|
||
public TestEnumerableAsync(List<TItem> originalValues, Func<TItem, bool> errorPredicate) | ||
{ | ||
CurrentIndex = -1; | ||
ErrorPredicate = errorPredicate; | ||
OriginalValues = originalValues; | ||
} | ||
|
||
public List<TItem> OriginalValues { get; set; } | ||
|
||
public int CurrentIndex { get; set; } | ||
|
||
public Func<TItem, bool> ErrorPredicate { get; set; } | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return new TestEnumeratorAsync<T, TItem>(this); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} |
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,57 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Z.Test.Linq.Async.Model | ||
{ | ||
public class TestEnumerator<T> : IEnumerator<T> | ||
{ | ||
public TestEnumerator(TestEnumerable<T> testTestEnumerable) | ||
{ | ||
TestEnumerable = testTestEnumerable; | ||
} | ||
|
||
private TestEnumerable<T> TestEnumerable { get; } | ||
|
||
public T Current | ||
{ | ||
get | ||
{ | ||
var currentValue = TestEnumerable.OriginalValues[TestEnumerable.CurrentIndex]; | ||
if (TestEnumerable.ErrorPredicate != null) | ||
{ | ||
if (TestEnumerable.ErrorPredicate(currentValue)) | ||
{ | ||
throw new Exception("TestEnumerable;ErrorPredicate;Value=" + currentValue); | ||
} | ||
} | ||
return currentValue; | ||
} | ||
} | ||
|
||
object IEnumerator.Current | ||
{ | ||
get { return Current; } | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
if (TestEnumerable.CurrentIndex + 1 < TestEnumerable.OriginalValues.Count) | ||
{ | ||
TestEnumerable.CurrentIndex++; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
TestEnumerable.CurrentIndex = -1; | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestEnumeratorAsync.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,58 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Z.Test.Linq.Async.Model | ||
{ | ||
public class TestEnumeratorAsync<T, TItem> : IEnumerator<T> where T : Task<TItem> | ||
{ | ||
public TestEnumeratorAsync(TestEnumerableAsync<T, TItem> testTestEnumerable) | ||
{ | ||
TestEnumerable = testTestEnumerable; | ||
} | ||
|
||
private TestEnumerableAsync<T, TItem> TestEnumerable { get; } | ||
|
||
public T Current | ||
{ | ||
get | ||
{ | ||
var currentValue = TestEnumerable.OriginalValues[TestEnumerable.CurrentIndex]; | ||
if (TestEnumerable.ErrorPredicate != null) | ||
{ | ||
if (TestEnumerable.ErrorPredicate(currentValue)) | ||
{ | ||
throw new Exception("TestEnumerable;ErrorPredicate;Value=" + currentValue); | ||
} | ||
} | ||
return (T) Task.Delay(100*(int) (object) currentValue); | ||
} | ||
} | ||
|
||
object IEnumerator.Current | ||
{ | ||
get { return Current; } | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
if (TestEnumerable.CurrentIndex + 1 < TestEnumerable.OriginalValues.Count) | ||
{ | ||
TestEnumerable.CurrentIndex++; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
TestEnumerable.CurrentIndex = -1; | ||
} | ||
} | ||
} |
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,35 @@ | ||
using System; | ||
|
||
namespace Z.Test.Linq.Async.Model | ||
{ | ||
public class TestPredicate<T> | ||
{ | ||
public Func<T, bool> ErrorPredicate; | ||
public Func<T, bool> OriginalPredicate; | ||
|
||
public TestPredicate(Func<T, bool> originalPredicate) | ||
{ | ||
OriginalPredicate = originalPredicate; | ||
} | ||
|
||
public TestPredicate(Func<T, bool> originalPredicate, Func<T, bool> errorPredicate) | ||
{ | ||
ErrorPredicate = errorPredicate; | ||
OriginalPredicate = originalPredicate; | ||
} | ||
|
||
public int Count { get; set; } | ||
|
||
public bool Predicate(T item) | ||
{ | ||
Count++; | ||
|
||
if (ErrorPredicate != null && ErrorPredicate(item)) | ||
{ | ||
throw new Exception("TestPredicate;ErrorPredicate;Value=" + item); | ||
} | ||
|
||
return OriginalPredicate(item); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/lab/Z.Lab.LinqAsync.NetCore/_Model/TestPredicateAsync.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,37 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Z.Test.Linq.Async.Model | ||
{ | ||
public class TestPredicateAsync<T> | ||
{ | ||
public Func<T, Task<bool>> ErrorPredicate; | ||
public Func<T, Task<bool>> OriginalPredicate; | ||
|
||
public TestPredicateAsync(Func<T, Task<bool>> originalPredicate) | ||
{ | ||
OriginalPredicate = originalPredicate; | ||
} | ||
|
||
public TestPredicateAsync(Func<T, Task<bool>> originalPredicate, Func<T, Task<bool>> errorPredicate) | ||
{ | ||
ErrorPredicate = errorPredicate; | ||
OriginalPredicate = originalPredicate; | ||
} | ||
|
||
public int Count { get; set; } | ||
|
||
|
||
public async Task<bool> Predicate(T item) | ||
{ | ||
Count++; | ||
|
||
if (ErrorPredicate != null && await ErrorPredicate(item).ConfigureAwait(false)) | ||
{ | ||
throw new Exception("TestPredicateAsync;ErrorPredicateAsync;Value=" + item); | ||
} | ||
|
||
return await OriginalPredicate(item).ConfigureAwait(false); | ||
} | ||
} | ||
} |