Last active
February 27, 2021 14:06
-
-
Save gsscoder/93618a67f5b5404031b44f8f5c651f19 to your computer and use it in GitHub Desktop.
Simple F# function that returns an asynchronous function that may inject chaos
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
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s | |
// $ dotnet fsi chaos3.fsx | |
// latency test | |
// 0.490776 | |
open System | |
open System.Threading | |
let chaosAsync (name:string) (shouldChaosAsync:unit -> Async<bool>) (chaosAsync:unit -> Async<unit>) = | |
fun (serviceAsync:unit -> Async<'t>) -> | |
async { | |
if shouldChaosAsync () |> Async.RunSynchronously then | |
printfn "%s" name | |
chaosAsync () |> Async.RunSynchronously | |
else () | |
return! serviceAsync () | |
} | |
let alwaysAsync = fun () -> async { return true } | |
let latencyAsync (ms:int) = fun () -> async { Thread.Sleep(ms) } | |
let computeRandomAsync = fun () -> async { return (new Random()).NextDouble() } | |
let injectChaos = chaosAsync "latency test" alwaysAsync (latencyAsync 3000) | |
let result = injectChaos computeRandomAsync |> Async.RunSynchronously | |
printfn "%f" result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment