|
| 1 | +module Tests |
| 2 | + |
| 3 | +open System |
| 4 | +open Expecto |
| 5 | +open StackExchange.Redis |
| 6 | +open System.Threading |
| 7 | +open System.Threading.Tasks |
| 8 | +open Akka.Streams.Dsl |
| 9 | +open FSharp.Control.Redis.Streams.Core |
| 10 | +open FSharp.Control.Redis.Streams.Akka |
| 11 | +open FSharp.Control.Tasks.V2.ContextInsensitive |
| 12 | +open Hopac |
| 13 | +open Akka.Actor |
| 14 | +open Akka.Streams |
| 15 | + |
| 16 | +let getUniqueKey (keyType : string) (key : string) = |
| 17 | + let suffix = Guid.NewGuid().ToString() |
| 18 | + sprintf "%s:%s:%s" keyType key suffix |
| 19 | + |> RedisKey.op_Implicit |
| 20 | + |
| 21 | +let printNameEntryValues (nve : NameValueEntry) = |
| 22 | + printfn "Key: %A - Value: %A" nve.Name nve.Value |
| 23 | +let printStreamEntry (se : StreamEntry) = |
| 24 | + printfn "Id: %A" se.Id |
| 25 | + se.Values |> Seq.iter(printNameEntryValues ) |
| 26 | +let printRedisStream (rs : RedisStream) = |
| 27 | + printfn "key: %A" rs.Key |
| 28 | + rs.Entries |> Seq.iter(printStreamEntry) |
| 29 | + |
| 30 | +let rkey (s : string) = RedisKey.op_Implicit s |
| 31 | +let rval (s : string) = RedisValue.op_Implicit s |
| 32 | +let nve name value = NameValueEntry(rval name, rval value) |
| 33 | + |
| 34 | +let inline map (fn: 't -> 'u) (source) : Source<'u, 'mat> = |
| 35 | + SourceOperations.Select(source, Func<_, _>(fn)) |
| 36 | + |
| 37 | +let inline runForEach (mat: #IMaterializer) (fn: 't -> unit) (source: Source<'t, 'mat>) = task { |
| 38 | + do! source.RunForeach(Action<_>(fn), mat) |
| 39 | +} |
| 40 | + |
| 41 | +let failDueToTimeout message (ts : TimeSpan) = |
| 42 | + failtestf "%s. Expected task to complete but failed after timeout of %f ms" message ts.TotalMilliseconds |
| 43 | + |
| 44 | +type StreamExpect<'a> (system : ActorSystem, predicate : seq<'a> -> bool) = |
| 45 | + let mat = system.Materializer() |
| 46 | + let values = ResizeArray<'a>() |
| 47 | + let cts = new CancellationTokenSource() |
| 48 | + let predicateConditionMet = TaskCompletionSource<unit>() |
| 49 | + let mutable debugPrint : Option<'a -> unit> = None |
| 50 | + // let mutable subscription : IDisposable = new Disposables.BooleanDisposable() :> IDisposable |
| 51 | + let checkPredicate () = |
| 52 | + if predicate values then |
| 53 | + predicateConditionMet.TrySetResult () |
| 54 | + |> ignore |
| 55 | + |
| 56 | + member this.Values |
| 57 | + with get () = values |
| 58 | + |
| 59 | + member this.DebugPrint |
| 60 | + with get () = debugPrint |
| 61 | + and set (v) = debugPrint <- v |
| 62 | + member this.CaptureFromStream (s : Source<_,_>) = |
| 63 | + let printer = |
| 64 | + match debugPrint with |
| 65 | + | Some d -> d |
| 66 | + | None -> ignore |
| 67 | + |
| 68 | + s |
| 69 | + |> map (fun x -> x |> printer; x) |
| 70 | + |> runForEach mat (values.Add >> checkPredicate) |
| 71 | + |> ignore |
| 72 | + |
| 73 | + |
| 74 | + member this.Await message (timeout : TimeSpan) = task { |
| 75 | + let generateTimeout () = task { |
| 76 | + do! Task.Delay(timeout,cts.Token) |
| 77 | + failDueToTimeout message timeout |
| 78 | + } |
| 79 | + |
| 80 | + let! firstFinishedTask = Task.WhenAny [ |
| 81 | + predicateConditionMet.Task |
| 82 | + generateTimeout () |
| 83 | + ] |
| 84 | + cts.Cancel() |
| 85 | + do! firstFinishedTask |
| 86 | + } |
| 87 | + |
| 88 | + interface IDisposable with |
| 89 | + member x.Dispose() = |
| 90 | + mat.Dispose() |
| 91 | + cts.Dispose() |
| 92 | + |
| 93 | +let ranStr n : string = |
| 94 | + let r = Random() |
| 95 | + String(Array.init n (fun _ -> char (r.Next(97,123)))) |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +let options = ConfigurationOptions.Parse("localhost", ResponseTimeout = 100000, SyncTimeout=100000, ConnectTimeout=100000) |
| 100 | +let redis = ConnectionMultiplexer.Connect(options) |
| 101 | +[<Tests>] |
| 102 | +let tests = |
| 103 | + testSequenced <| |
| 104 | + testList "samples" [ |
| 105 | + testCaseAsync "Stream should generate 2 events" <| async { |
| 106 | + use system = ActorSystem.Create("system") |
| 107 | + let db = redis.GetDatabase() |
| 108 | + let key = getUniqueKey "stream" "Foo" |
| 109 | + use expecter = new StreamExpect<_>(system, fun s -> s |> Seq.length = 2) |
| 110 | + pollStreamForever db key StreamPosition.Beginning PollOptions.Default |
| 111 | + |> expecter.CaptureFromStream |
| 112 | + |
| 113 | + let values = |
| 114 | + [| |
| 115 | + nve "Field1" "Value1" |
| 116 | + nve "Field2" "Value3" |
| 117 | + |] |
| 118 | + let! x = db.StreamAddAsync(key, values) |> Async.AwaitTask |
| 119 | + let values = |
| 120 | + [| |
| 121 | + nve "Field1" "Value4" |
| 122 | + nve "Field2" "Value6" |
| 123 | + |] |
| 124 | + let! x = db.StreamAddAsync(key, values) |> Async.AwaitTask |
| 125 | + do! expecter.Await "Should have 2 results" (TimeSpan.FromSeconds(1.)) |> Async.AwaitTask |
| 126 | + } |
| 127 | + |
| 128 | + testCaseAsync "Stream should generate 20000 events" <| async { |
| 129 | + use system = ActorSystem.Create("system") |
| 130 | + let total = 20000 |
| 131 | + let db = redis.GetDatabase() |
| 132 | + let key = getUniqueKey "stream" "Foo" |
| 133 | + use expecter = new StreamExpect<_>(system, fun s -> s |> Seq.length = total) |
| 134 | + pollStreamForever db key StreamPosition.Beginning PollOptions.Default |
| 135 | + |> expecter.CaptureFromStream |
| 136 | + |
| 137 | + job { |
| 138 | + do! |
| 139 | + [0..total] |
| 140 | + |> Seq.map(fun i -> |
| 141 | + let values = |
| 142 | + [| |
| 143 | + NameValueEntry (RedisValue.op_Implicit "Field1", RedisValue.op_Implicit total) |
| 144 | + |] |
| 145 | + job { |
| 146 | + let! x = db.StreamAddAsync(key, values) |> Async.AwaitTask |
| 147 | + return () |
| 148 | + }) |
| 149 | + |> Stream.ofSeq |
| 150 | + |> Stream.mapPipelinedJob (Environment.ProcessorCount * 4096 * 2) id |
| 151 | + |> Stream.iter |
| 152 | + } |> start |
| 153 | + |
| 154 | + do! expecter.Await "Should have 20000 results" (TimeSpan.FromSeconds(30.)) |> Async.AwaitTask |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + testCaseAsync "Stream should generate large fields" <| async { |
| 159 | + use system = ActorSystem.Create("system") |
| 160 | + let total = 200 |
| 161 | + let db = redis.GetDatabase() |
| 162 | + let key = getUniqueKey "stream" "Foo" |
| 163 | + use expecter = new StreamExpect<_>(system, fun s -> s |> Seq.length = total) |
| 164 | + pollStreamForever db key StreamPosition.Beginning PollOptions.Default |
| 165 | + |> expecter.CaptureFromStream |
| 166 | + |
| 167 | + job { |
| 168 | + do! |
| 169 | + [0..total] |
| 170 | + |> Seq.map(fun i -> |
| 171 | + let data = ranStr (20000) |
| 172 | + let values = |
| 173 | + [| |
| 174 | + NameValueEntry (RedisValue.op_Implicit "Field1", RedisValue.op_Implicit data) |
| 175 | + |] |
| 176 | + job { |
| 177 | + let! x = db.StreamAddAsync(key, values) |> Async.AwaitTask |
| 178 | + return () |
| 179 | + }) |
| 180 | + |> Stream.ofSeq |
| 181 | + |> Stream.mapPipelinedJob (Environment.ProcessorCount * 2) id |
| 182 | + |> Stream.iter |
| 183 | + } |> start |
| 184 | + |
| 185 | + do! expecter.Await "Should have 2 results" (TimeSpan.FromSeconds(30.)) |> Async.AwaitTask |
| 186 | + } |
| 187 | + ] |
0 commit comments