Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Last active February 27, 2021 13:39
Show Gist options
  • Select an option

  • Save gsscoder/01d99255532503ef0bd7e4c6553193ea to your computer and use it in GitHub Desktop.

Select an option

Save gsscoder/01d99255532503ef0bd7e4c6553193ea to your computer and use it in GitHub Desktop.
Simple F# function that returns a function that may inject chaos
// 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