mirror of
https://github.com/facebook/Haxl.git
synced 2024-12-24 01:04:21 +03:00
c8a04950cb
Summary: We have define this in tests and other libraries. Let's simply expose this functionality from `Haxl.Prelude` as it can be useful in a few cases. Reviewed By: anubhav94N Differential Revision: D16522569 fbshipit-source-id: b35726e9ad172a36d76b755498fbb53d9a9db188
64 lines
1.6 KiB
Haskell
64 lines
1.6 KiB
Haskell
-- Copyright (c) 2014-present, Facebook, Inc.
|
|
-- All rights reserved.
|
|
--
|
|
-- This source code is distributed under the terms of a BSD license,
|
|
-- found in the LICENSE file.
|
|
|
|
module FullyAsyncTest where
|
|
|
|
import Haxl.Prelude as Haxl
|
|
import Prelude()
|
|
|
|
import SleepDataSource
|
|
import Haxl.DataSource.ConcurrentIO
|
|
|
|
import Haxl.Core
|
|
import Test.HUnit
|
|
import Data.IORef
|
|
import Haxl.Core.Monad (unsafeLiftIO)
|
|
|
|
tests :: Test
|
|
tests = sleepTest
|
|
|
|
testEnv = do
|
|
st <- mkConcurrentIOState
|
|
env <- initEnv (stateSet st stateEmpty) ()
|
|
return env { flags = (flags env) { report = 2 } }
|
|
|
|
sleepTest :: Test
|
|
sleepTest = TestCase $ do
|
|
env <- testEnv
|
|
|
|
ref <- newIORef ([] :: [Int])
|
|
let tick n = unsafeLiftIO (modifyIORef ref (n:))
|
|
|
|
-- simulate running a selection of data fetches that complete at
|
|
-- different times, overlapping them as much as possible.
|
|
runHaxl env $
|
|
sequence_
|
|
[ sequence_ [sleep 100, sleep 400] `andThen` tick 5 -- A
|
|
, sleep 100 `andThen` tick 2 `andThen` sleep 200 `andThen` tick 4 -- B
|
|
, sleep 50 `andThen` tick 1 `andThen` sleep 150 `andThen` tick 3 -- C
|
|
]
|
|
|
|
ys <- readIORef ref
|
|
assertEqual "FullyAsyncTest: ordering" [1,2,3,4,5] (reverse ys)
|
|
|
|
stats <- readIORef (statsRef env)
|
|
print stats
|
|
assertEqual "FullyAsyncTest: stats" 5 (numFetches stats)
|
|
|
|
{-
|
|
A B C
|
|
50 | | tick 1
|
|
100 | tick 2 |
|
|
150 | | |
|
|
200 | | tick 3
|
|
250 | |
|
|
300 | tick 4
|
|
350 |
|
|
400 |
|
|
450 |
|
|
500 tick 5
|
|
-}
|