@@ -448,27 +448,91 @@ let ``AsyncSeq.bufferByTimeAndCount empty``() =
448448
449449[<Test>]
450450let ``AsyncSeq.bufferByTime`` () =
451-
452- let s = asyncSeq {
453- yield 1
454- yield 2
455- do ! Async.Sleep 100
456- yield 3
457- yield 4
458- do ! Async.Sleep 100
459- yield 5
460- yield 6
461- }
462451
463- let actual =
464- s
465- |> AsyncSeq.bufferByTime 100
466- |> AsyncSeq.map ( List.ofArray)
467- |> AsyncSeq.toList
452+ let Y = Choice1Of2
453+ let S = Choice2Of2
468454
469- let expected = [ [ 1 ; 2 ] ; [ 3 ; 4 ] ; [ 5 ; 6 ] ]
455+ let timeMs = 500
470456
471- Assert.True (( actual = expected))
457+ let inp0 = [ ]
458+ let exp0 = [ ]
459+
460+ let inp1 = [ Y 1 ; Y 2 ; S timeMs ; Y 3 ; Y 4 ; S timeMs ; Y 5 ; Y 6 ]
461+ let exp1 = [ [ 1 ; 2 ] ; [ 3 ; 4 ] ; [ 5 ; 6 ] ]
462+
463+ // let inp2 : Choice<int, int> list = [ S 500 ]
464+ // let exp2 : int list list = [ [] ; [] ; [] ; [] ]
465+
466+ let toSeq ( xs : Choice < int , int > list ) = asyncSeq {
467+ for x in xs do
468+ match x with
469+ | Choice1Of2 v -> yield v
470+ | Choice2Of2 s -> do ! Async.Sleep s }
471+
472+ for ( inp, exp) in [ ( inp0, exp0) ; ( inp1, exp1) ] do
473+
474+ let actual =
475+ toSeq inp
476+ |> AsyncSeq.bufferByTime ( timeMs - 5 )
477+ |> AsyncSeq.map List.ofArray
478+ |> AsyncSeq.toList
479+
480+ //let ls = toSeq inp |> AsyncSeq.toList
481+ //let actualLs = actual |> List.concat
482+
483+ Assert.True (( actual = exp))
484+
485+ // WARNING: Too timing sensitive
486+ //let rec prependToAll (a:'a) (ls:'a list) : 'a list =
487+ // match ls with
488+ // | [] -> []
489+ // | hd::tl -> a::hd::prependToAll a tl
490+ //
491+ //let rec intersperse (a:'a) (ls:'a list) : 'a list =
492+ // match ls with
493+ // | [] -> []
494+ // | hd::tl -> hd::prependToAll a tl
495+ //
496+ //let intercalate (l:'a list) (xs:'a list list) : 'a list =
497+ // intersperse l xs |> List.concat
498+ //
499+ //let batch (size:int) (ls:'a list) : 'a list list =
500+ // let rec go batch ls =
501+ // match ls with
502+ // | [] -> [List.rev batch]
503+ // | _ when List.length batch = size -> (List.rev batch)::go [] ls
504+ // | hd::tl -> go (hd::batch) tl
505+ // go [] ls
506+ //
507+ //[<Test>]
508+ //let ``AsyncSeq.bufferByTime2`` () =
509+ //
510+ // let Y = Choice1Of2
511+ // let S = Choice2Of2
512+ // let sleepMs = 100
513+ //
514+ // let toSeq (xs:Choice<int, int> list) = asyncSeq {
515+ // for x in xs do
516+ // match x with
517+ // | Choice1Of2 v -> yield v
518+ // | Choice2Of2 s -> do! Async.Sleep s }
519+ //
520+ // for (size,batchSize) in [ (0,0) ; (10,2) ; (100,2) ] do
521+ //
522+ // let expected =
523+ // List.init size id
524+ // |> batch batchSize
525+ //
526+ // let actual =
527+ // expected
528+ // |> List.map (List.map Y)
529+ // |> intercalate [S sleepMs]
530+ // |> toSeq
531+ // |> AsyncSeq.bufferByTime sleepMs
532+ // |> AsyncSeq.map List.ofArray
533+ // |> AsyncSeq.toList
534+ //
535+ // Assert.True ((actual = expected))
472536
473537[<Test>]
474538let ``AsyncSeq.bufferByCountAndTime should not block`` () =
0 commit comments