|
1 | 1 | module TaskSeq.Tests.``Conversion-To`` |
2 | 2 |
|
3 | 3 | open System.Collections.Generic |
| 4 | +open System.Threading.Channels |
4 | 5 |
|
5 | 6 | open Xunit |
6 | 7 | open FsUnit.Xunit |
@@ -186,3 +187,72 @@ module SideEffects = |
186 | 187 | let (results2: seq<_>) = tq |> TaskSeq.toSeq |
187 | 188 | results1 |> Seq.toArray |> should equal [| 1..10 |] |
188 | 189 | results2 |> Seq.toArray |> should equal [| 11..20 |] |
| 190 | + |
| 191 | +module Channel = |
| 192 | + |
| 193 | + [<Fact>] |
| 194 | + let ``TaskSeq-toChannelAsync with null writer raises`` () = |
| 195 | + assertNullArg |
| 196 | + <| fun () -> |
| 197 | + TaskSeq.toChannelAsync null (TaskSeq.ofArray [| 1 |]) |
| 198 | + |> ignore |
| 199 | + |
| 200 | + [<Fact>] |
| 201 | + let ``TaskSeq-toChannelAsync with null source raises`` () = |
| 202 | + let ch = Channel.CreateUnbounded<int>() |
| 203 | + |
| 204 | + assertNullArg |
| 205 | + <| fun () -> TaskSeq.toChannelAsync ch.Writer null |> ignore |
| 206 | + |
| 207 | + [<Fact>] |
| 208 | + let ``TaskSeq-ofChannel with null reader raises`` () = |
| 209 | + assertNullArg |
| 210 | + <| fun () -> TaskSeq.ofChannel<int> null |> ignore |
| 211 | + |
| 212 | + [<Fact>] |
| 213 | + let ``TaskSeq-toChannelAsync with empty source completes the channel`` () = task { |
| 214 | + let ch = Channel.CreateUnbounded<int>() |
| 215 | + do! TaskSeq.toChannelAsync ch.Writer TaskSeq.empty |
| 216 | + ch.Reader.Completion.IsCompleted |> should be True |
| 217 | + let! results = TaskSeq.ofChannel ch.Reader |> TaskSeq.toArrayAsync |
| 218 | + results |> should be Empty |
| 219 | + } |
| 220 | + |
| 221 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 222 | + let ``TaskSeq-toChannelAsync writes all elements and completes the channel`` variant = task { |
| 223 | + let tq = Gen.getSeqImmutable variant |
| 224 | + let ch = Channel.CreateUnbounded<int>() |
| 225 | + do! TaskSeq.toChannelAsync ch.Writer tq |
| 226 | + let! results = TaskSeq.ofChannel ch.Reader |> TaskSeq.toArrayAsync |
| 227 | + results |> should equal [| 1..10 |] |
| 228 | + // Completion resolves once the channel is marked done and the buffer is drained |
| 229 | + do! ch.Reader.Completion |
| 230 | + } |
| 231 | + |
| 232 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 233 | + let ``TaskSeq-ofChannel yields all elements written to the channel`` variant = task { |
| 234 | + let tq = Gen.getSeqImmutable variant |
| 235 | + let ch = Channel.CreateUnbounded<int>() |
| 236 | + do! TaskSeq.toChannelAsync ch.Writer tq |
| 237 | + let! results = TaskSeq.ofChannel ch.Reader |> TaskSeq.toArrayAsync |
| 238 | + results |> should equal [| 1..10 |] |
| 239 | + } |
| 240 | + |
| 241 | + [<Fact>] |
| 242 | + let ``TaskSeq-ofChannel ends when channel is completed and drained`` () = task { |
| 243 | + let ch = Channel.CreateUnbounded<int>() |
| 244 | + do! ch.Writer.WriteAsync 42 |
| 245 | + do! ch.Writer.WriteAsync 99 |
| 246 | + ch.Writer.Complete() |
| 247 | + let! results = TaskSeq.ofChannel ch.Reader |> TaskSeq.toArrayAsync |
| 248 | + results |> should equal [| 42; 99 |] |
| 249 | + } |
| 250 | + |
| 251 | + [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] |
| 252 | + let ``TaskSeq-toChannelAsync executes side effects`` variant = task { |
| 253 | + let tq = Gen.getSeqWithSideEffect variant |
| 254 | + let ch = Channel.CreateUnbounded<int>() |
| 255 | + do! TaskSeq.toChannelAsync ch.Writer tq |
| 256 | + let! results = TaskSeq.ofChannel ch.Reader |> TaskSeq.toArrayAsync |
| 257 | + results |> should equal [| 1..10 |] |
| 258 | + } |
0 commit comments