Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unit tests for bounded caching. Needs more.
module AsyncBoundedCacheTests
open System
open Xunit
[<Fact>]
let ``insert items`` () = async {
let f = fun x -> async {
do! Async.Sleep(1)
return x
}
let abc = AsyncBoundedCache.AsyncBoundedCache(100, 10, 10, f)
let! v1 = abc.Get("A")
let! v2 = abc.Get("B")
let! v3 = abc.Get("C")
let! v4 = abc.Get("A")
let! v5 = abc.Get("A")
let! v6 = abc.Get("B")
Assert.Equal(3, abc.Count)
Assert.Equal("A", v1)
Assert.Equal("B", v2)
Assert.Equal("C", v3)
Assert.Equal("A", v4)
}
[<Fact>]
let ``consecutive bulk retrieval satisfies bounds`` () = async {
let f = fun x -> async {
do! Async.Sleep(1)
return x
}
let maxCount = 10
let bulkRemoveSize = 10
let maxAge = 10000
let abc = AsyncBoundedCache.AsyncBoundedCache(maxCount, bulkRemoveSize, maxAge, f)
let results0 =
[0..9]
|> List.map (fun i -> abc.Get(i))
|> Async.Parallel
|> Async.RunSynchronously
Assert.InRange(abc.Count, 0, maxCount)
let results1 =
[10..19]
|> List.map (fun i -> abc.Get(i))
|> Async.Parallel
|> Async.RunSynchronously
Assert.InRange(abc.Count, 0, maxCount)
let results2 =
[20..29]
|> List.map (fun i -> abc.Get(i))
|> Async.Parallel
|> Async.RunSynchronously
Assert.InRange(abc.Count, 0, maxCount)
}
[<Fact>]
let ``huge bulk retrieval satisfies bounds`` () = async {
let f = fun x -> async {
do! Async.Sleep(1)
return x
}
let maxCount = 1000
let bulkRemoveSize = 10
let maxAge = 10000
let abc = AsyncBoundedCache.AsyncBoundedCache(maxCount, bulkRemoveSize, maxAge, f)
let results0 =
[0..10000-1]
|> List.map (fun i -> abc.Get(i))
|> Async.Parallel
|> Async.RunSynchronously
Assert.Equal(9999L*10000L/2L, results0 |> Array.map int64 |> Array.sum)
Assert.InRange(abc.Count, 0, maxCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.