Skip to content

Commit 5c49986

Browse files
Dylan Yudakenfacebook-github-bot
authored andcommitted
check fallback datacache for cacheResultWithInsert (#141)
Summary: Pull Request resolved: #141 There are two ways into the DataFetch cache, through cacheResult, and dataFetch. The fallback only worked for dataFetch, and this diff adds cacheResult. This allows tests to inject state such as time through the fallback, and that should allow consistent tests Differential Revision: D28534164 fbshipit-source-id: 64811913742c98708eb7f0cf3d651d3a12816323
1 parent 54fcaf7 commit 5c49986

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

Haxl/Core/Fetch.hs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,19 @@ cacheResultWithInsert showFn insertFn req val = GenHaxl $ \e@Env{..} -> do
353353
mbRes <- DataCache.lookup req dataCache
354354
case mbRes of
355355
Nothing -> do
356-
eitherResult <- Exception.try val
357-
case eitherResult of
358-
Left e -> rethrowAsyncExceptions e
359-
_ -> return ()
360-
let result = eitherToResultThrowIO eitherResult
356+
let
357+
getResult = do
358+
eitherResult <- Exception.try val
359+
case eitherResult of
360+
Left e -> rethrowAsyncExceptions e
361+
_ -> return ()
362+
return $ eitherToResultThrowIO eitherResult
363+
-- if there is a fallback configured try that
364+
result <- case dataCacheFetchFallback of
365+
Nothing -> getResult
366+
Just (DataCacheLookup dcl) -> do
367+
mbFallbackRes <- dcl req
368+
maybe getResult return mbFallbackRes
361369
ivar <- newFullIVar result
362370
k <- nextCallId e
363371
insertFn req (DataCacheItem ivar k) dataCache

tests/DataCacheTest.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ instance StateKey TestReq where
4646

4747
instance ShowP TestReq where showp = show
4848

49+
data CacheableReq x where CacheableInt :: Int -> CacheableReq Int
50+
deriving Typeable
51+
deriving instance Eq (CacheableReq x)
52+
deriving instance Show (CacheableReq x)
53+
instance Hashable (CacheableReq x) where
54+
hashWithSalt s (CacheableInt val) = hashWithSalt s (0::Int, val)
55+
56+
4957
newResult :: a -> IO (IVar u w a)
5058
newResult a = IVar <$> newIORef (IVarFull (Ok a NilWrites))
5159

@@ -105,11 +113,15 @@ dcStrictnessTest = TestLabel "DataCache strictness" $ TestCase $ do
105113
dcFallbackTest :: Test
106114
dcFallbackTest = TestLabel "DataCache fallback" $ TestCase $ do
107115
env <- addLookup <$> initEnv (stateSet TestReqState stateEmpty) ()
108-
r <- runHaxl env (dataFetch req)
116+
(r,cached) <- runHaxl env (do
117+
a <- dataFetch req
118+
b <- cacheResult (CacheableInt 1234) (return 99999)
119+
return (a,b))
109120
(Stats stats) <- readIORef (statsRef env)
110121
assertEqual "fallback still has stats" 1
111122
(Prelude.length [x | x@FetchStats{} <- stats])
112123
assertEqual "dcFallbackTest found" 1 r
124+
assertEqual "dcFallbackTest cached" 1234 cached
113125
rbad <- Control.Exception.try $ runHaxl env (dataFetch reqBad)
114126
assertBool "dcFallbackTest not found" $
115127
case rbad of
@@ -128,17 +140,23 @@ dcFallbackTest = TestLabel "DataCache fallback" $ TestCase $ do
128140
-- have to coerce on the way out as results are not Typeable
129141
-- so you better be sure you do it right!
130142
return $ unsafeCoerce . doReq <$> cast r
143+
| typeOf r == typeRep (Proxy :: Proxy (CacheableReq Int)) =
144+
return $ unsafeCoerce . doCache <$> cast r
131145
| otherwise = return Nothing
132146

133147
doReq :: TestReq Int -> ResultVal Int ()
134148
doReq (Req r) = Ok r NilWrites
135149

150+
doCache :: CacheableReq Int -> ResultVal Int ()
151+
doCache (CacheableInt i) = Ok i NilWrites
152+
136153
req :: TestReq Int
137154
req = Req 1
138155

139156
reqBad :: TestReq String
140157
reqBad = Req 2
141158

159+
142160
-- tests :: Assertion
143161
tests = TestList [ dcSoundnessTest
144162
, dcStrictnessTest

0 commit comments

Comments
 (0)