Last active
February 27, 2021 13:39
-
-
Save gsscoder/01d99255532503ef0bd7e4c6553193ea to your computer and use it in GitHub Desktop.
Simple F# function that returns a function that may inject chaos
This file contains hidden or 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 chaos2.fsx | |
| // latency test | |
| // 0.969491 | |
| open System | |
| open System.Threading | |
| let chaos (name:string) (shouldChaos:unit -> bool) (chaos:unit -> unit) = | |
| fun (service:unit -> 't) -> | |
| if shouldChaos () then | |
| printfn "%s" name | |
| do chaos | |
| else () | |
| service () | |
| let always = fun () -> true | |
| let latency (ms:int) = fun () -> Thread.Sleep(ms) | |
| let computeRandom = fun () -> (new Random()).NextDouble() | |
| let injectChaos = chaos "latency test" always (latency 3000) | |
| let result = injectChaos computeRandom | |
| printfn "%f" result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment