Skip to content

Commit

Permalink
Lots of AsyncEx tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAngryByrd committed Jul 4, 2023
1 parent 634ea90 commit a7d83c4
Show file tree
Hide file tree
Showing 8 changed files with 855 additions and 135 deletions.
279 changes: 162 additions & 117 deletions src/IcedTasks/AsyncEx.fs

Large diffs are not rendered by default.

635 changes: 635 additions & 0 deletions tests/IcedTasks.Tests/AsyncExTests.fs

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions tests/IcedTasks.Tests/CancellableTaskTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ open Expecto
open System.Threading
open System.Threading.Tasks
open IcedTasks
#if NET7_0_OR_GREATER
open IcedTasks.ValueTaskExtensions

#endif
module CancellableTaskTests =
open System.Collections.Concurrent
open TimeProviderExtensions
Expand Down Expand Up @@ -180,7 +181,7 @@ module CancellableTaskTests =

Expect.equal actual expected ""
}

#if NET7_0_OR_GREATER
testCaseAsync "Can Bind Cancellable TaskLike"
<| async {
let fooTask = fun (ct: CancellationToken) -> Task.Yield()
Expand All @@ -197,7 +198,7 @@ module CancellableTaskTests =
|> Async.AwaitValueTask
// Compiling is sufficient expect
}

#endif
testCaseAsync "Can Bind Task"
<| async {
let outerTask = cancellableTask { do! Task.CompletedTask }
Expand Down Expand Up @@ -395,6 +396,7 @@ module CancellableTaskTests =
}


#if NET7_0_OR_GREATER
testCaseAsync "use IAsyncDisposable sync"
<| async {
let data = 42
Expand Down Expand Up @@ -486,7 +488,7 @@ module CancellableTaskTests =
Expect.equal actual data "Should be able to use use"
Expect.isTrue wasDisposed ""
}

#endif
testCaseAsync "null"
<| async {
let data = 42
Expand Down Expand Up @@ -749,11 +751,11 @@ module CancellableTaskTests =
TimeSpan.FromMilliseconds(100)
)

let t = fooTask cts.Token
let runningTask = fooTask cts.Token
do! timeProvider.ForwardTimeAsync(TimeSpan.FromMilliseconds(50))
Expect.equal t.IsCanceled false ""
Expect.isFalse runningTask.IsCanceled ""
do! timeProvider.ForwardTimeAsync(TimeSpan.FromMilliseconds(50))
do! t
do! runningTask
}
)

Expand Down
25 changes: 21 additions & 4 deletions tests/IcedTasks.Tests/CancellableValueTaskTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ open System.Threading
open System.Threading.Tasks
open IcedTasks

#if NET7_0_OR_GREATER
module CancellableValueTaskTests =
open TimeProviderExtensions

let builderTests =
testList "CancellableValueTaskBuilder" [
Expand Down Expand Up @@ -759,21 +761,35 @@ module CancellableValueTaskTests =
testCaseAsync
"Can extract context's CancellationToken via CancellableValueTask.getCancellationToken in a deeply nested CE"
<| async {
let timeProvider = ManualTimeProvider()

do!
Expect.CancellationRequested(
cancellableValueTask {
let fooTask = cancellableValueTask {
return! cancellableValueTask {
do! cancellableValueTask {
let! ct = CancellableValueTask.getCancellationToken ()
do! Task.Delay(1000, ct)

do!
timeProvider.Delay(
TimeSpan.FromMilliseconds(1000),
ct
)
}
}
}

use cts = new CancellationTokenSource()
cts.CancelAfter(100)
do! fooTask cts.Token
use cts =
timeProvider.CreateCancellationTokenSource(
TimeSpan.FromMilliseconds(100)
)

let runningTask = fooTask cts.Token
do! timeProvider.ForwardTimeAsync(TimeSpan.FromMilliseconds(50))
Expect.isFalse runningTask.IsCanceled ""
do! timeProvider.ForwardTimeAsync(TimeSpan.FromMilliseconds(50))
do! runningTask
}
)

Expand Down Expand Up @@ -1016,3 +1032,4 @@ module CancellableValueTaskTests =
asyncBuilderTests
functionTests
]
#endif
3 changes: 2 additions & 1 deletion tests/IcedTasks.Tests/ColdTaskTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ module ColdTaskTests =
}


#if NET7_0_OR_GREATER
testCaseAsync "use IAsyncDisposable sync"
<| async {
let data = 42
Expand Down Expand Up @@ -498,7 +499,7 @@ module ColdTaskTests =
Expect.equal actual data "Should be able to use use"
Expect.isTrue wasDisposed ""
}

#endif
testCaseAsync "null"
<| async {
let data = 42
Expand Down
18 changes: 16 additions & 2 deletions tests/IcedTasks.Tests/Expect.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ type Expect =
(fun () -> operation)
"Should have been cancelled"

#if NET7_0_OR_GREATER
static member CancellationRequested(operation: ValueTask<unit>) =
Expect.CancellationRequested(Async.AwaitValueTask operation)
|> Async.AsValueTask

#endif
static member CancellationRequested(operation: Task<_>) =
Expect.CancellationRequested(Async.AwaitTask operation)
|> Async.StartImmediateAsTask
Expand All @@ -64,10 +65,11 @@ type Expect =
Expect.CancellationRequested(Async.AwaitCancellableTask operation)
|> Async.AsCancellableTask

#if NET7_0_OR_GREATER
static member CancellationRequested(operation: CancellableValueTask<_>) =
Expect.CancellationRequested(Async.AwaitCancellableValueTask operation)
|> Async.AsCancellableTask

#endif

open TimeProviderExtensions
open System.Runtime.CompilerServices
Expand All @@ -82,3 +84,15 @@ type ManualTimeProviderExtensions =
do! Task.Yield()
do! Task.Delay(5)
}


module CustomAwaiter =

type CustomAwaiter<'T>(onGetResult, onIsCompleted) =

member this.GetResult() : 'T = onGetResult ()
member this.IsCompleted: bool = onIsCompleted ()

interface ICriticalNotifyCompletion with
member this.UnsafeOnCompleted(continuation) = failwith "Not Implemented"
member this.OnCompleted(continuation: Action) : unit = failwith "Not Implemented"
10 changes: 7 additions & 3 deletions tests/IcedTasks.Tests/IcedTasks.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../../src/IcedTasks/IcedTasks.fsproj" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0'">
<ProjectReference Include="../../src/IcedTasks/IcedTasks.fsproj" AdditionalProperties="TargetFramework=netstandard2.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0'">
<ProjectReference Include="../../src/IcedTasks/IcedTasks.fsproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Expect.fs" />
<Compile Include="AsyncExTests.fs" />
<Compile Include="ValueTaskTests.fs" />
<Compile Include="CancellableTaskTests.fs" />
<Compile Include="CancellableValueTaskTests.fs" />
Expand Down
4 changes: 3 additions & 1 deletion tests/IcedTasks.Tests/ValueTaskTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open Expecto
open System.Threading
open System.Threading.Tasks
open IcedTasks

#if NET7_0

module ValueTaskTests =

Expand Down Expand Up @@ -720,3 +720,5 @@ module ValueTaskTests =
// asyncBuilderTests
functionTests
]

#endif

0 comments on commit a7d83c4

Please sign in to comment.