Skip to content

Commit

Permalink
Unit test proofs that lazy is not needed, but Task serialization defi…
Browse files Browse the repository at this point in the history
…nitely is.
  • Loading branch information
Thorium committed Aug 25, 2016
1 parent 2d4b05e commit 44fd4ba
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/Paket.Core/Utils.fs
Expand Up @@ -31,9 +31,7 @@ let internal memoize (f: 'a -> 'b) : 'a -> 'b =
let internal memoizeAsync f =
let cache = System.Collections.Concurrent.ConcurrentDictionary<'a, System.Threading.Tasks.Task<'b>>()
fun (x: 'a) -> // task.Result serialization to sync after done.
cache.GetOrAdd(x, fun x -> // Lazy is needed for "if managed to start multiple before added"
let threadfix = lazy( f(x) |> Async.StartAsTask )
threadfix.Force()) |> Async.AwaitTask
cache.GetOrAdd(x, fun x -> f(x) |> Async.StartAsTask) |> Async.AwaitTask

type Auth =
| Credentials of Username : string * Password : string
Expand Down
4 changes: 4 additions & 0 deletions tests/Paket.Tests/DependenciesFile/ParserSpecs.fs
Expand Up @@ -1359,6 +1359,8 @@ let ``async cache should work``() =
do! memFunc "a" |> Async.Ignore
do! memFunc "a" |> Async.Ignore
do! memFunc "a" |> Async.Ignore
do! [|1 .. 30|] |> Seq.map(fun _ -> (memFunc "a"))
|> Async.Parallel |> Async.Ignore
for i = 1 to 30 do
Async.Start( memFunc "a" |> Async.Ignore )
Async.Start( memFunc "a" |> Async.Ignore )
Expand All @@ -1367,6 +1369,8 @@ let ``async cache should work``() =
do! memFunc "a" |> Async.Ignore
for i = 1 to 30 do
Async.Start( memFunc "a" |> Async.Ignore )
do! [|1 .. 30|] |> Seq.map(fun _ -> (memFunc "a"))
|> Async.Parallel |> Async.Ignore
} |> Async.RunSynchronously
x |> shouldEqual 1

0 comments on commit 44fd4ba

Please sign in to comment.