Newer
Older
module Quasar.ResourcesSpec (spec) where
import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception
import Control.Monad.Catch
import Quasar.Prelude
import Test.Hspec
import Quasar.Awaitable
import Quasar.Resources
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
data TestException = TestException
deriving stock (Eq, Show)
instance Exception TestException
spec :: Spec
spec = pure ()
--spec = parallel $ do
-- describe "ResourceManager" $ do
-- it "can be created" $ io do
-- withRootResourceManager $ pure ()
--
-- it "can be created and disposed" $ io do
-- withRootResourceManager do
-- resourceManager <- askResourceManager
-- disposeEventually_ resourceManager
--
-- it "is disposed when exiting withRootResourceManager" $ io do
-- resourceManager <- withRootResourceManager askResourceManager
--
-- peekAwaitable (isDisposed resourceManager) `shouldReturn` Just ()
--
-- it "can be created and disposed with a delay" $ io do
-- withRootResourceManager $ liftIO $ threadDelay 100000
--
-- it "can \"dispose\" a noDisposable" $ io do
-- withRootResourceManager do
-- registerDisposable noDisposable
--
-- it "can attach a dispose action" $ io do
-- var <- newTVarIO False
-- withRootResourceManager do
-- registerDisposeAction $ atomically $ writeTVar var True
--
-- atomically (readTVar var) `shouldReturn` True
--
-- it "can attach a slow dispose action" $ io do
-- withRootResourceManager do
-- registerDisposeAction $ threadDelay 100000
--
-- it "re-throws an exception" $ do
-- shouldThrow
-- do
-- withRootResourceManager do
-- liftIO $ throwIO TestException
-- \TestException -> True
--
-- it "handles an exception while disposing" $ io do
-- (`shouldThrow` \(_ :: CombinedException) -> True) do
-- withRootResourceManager do
-- registerDisposeAction $ throwIO TestException
-- liftIO $ threadDelay 100000
--
-- it "passes an exception to the root resource manager" $ io do
-- (`shouldThrow` \(_ :: CombinedException) -> True) do
-- withRootResourceManager do
-- withScopedResourceManager do
-- registerDisposeAction $ throwIO TestException
-- liftIO $ threadDelay 100000
--
-- it "passes an exception to the root resource manager when closing the inner resource manager first" $ io do
-- (`shouldThrow` \(_ :: CombinedException) -> True) do
-- withRootResourceManager do
-- withScopedResourceManager do
-- registerDisposeAction $ throwIO TestException
-- liftIO $ threadDelay 100000
--
-- it "can attach an disposable that is disposed asynchronously" $ io do
-- withRootResourceManager do
-- disposable <- captureDisposable_ $ registerDisposeAction $ threadDelay 100000
-- liftIO $ void $ forkIO $ dispose disposable
--
-- it "does not abort disposing when encountering an exception" $ do
-- var1 <- newTVarIO False
-- var2 <- newTVarIO False
-- (`shouldThrow` \(_ :: CombinedException) -> True) do
-- withRootResourceManager do
-- registerDisposeAction $ atomically (writeTVar var1 True)
-- registerDisposeAction $ throwIO TestException
-- registerDisposeAction $ atomically (writeTVar var2 True)
-- atomically (readTVar var1) `shouldReturn` True
-- atomically (readTVar var2) `shouldReturn` True
--
-- it "withRootResourceManager will start disposing when receiving an exception" $ io do
-- (`shouldThrow` \(_ :: CombinedException) -> True) do
-- withRootResourceManager do
-- linkExecution do
-- throwToResourceManager TestException
-- sleepForever
--
-- it "combines exceptions from resources with exceptions on the thread" $ io do
-- (`shouldThrow` \(combinedExceptions -> exceptions) -> length exceptions == 2) do
-- withRootResourceManager do
-- throwToResourceManager TestException
-- throwM TestException
--
-- it "can dispose a resource manager loop" $ io do
-- withRootResourceManager do
-- rm1 <- newResourceManager
-- rm2 <- newResourceManager
-- liftIO $ atomically do
-- attachDisposable rm1 rm2
-- attachDisposable rm2 rm1
--
-- it "can dispose a resource manager loop" $ io do
-- withRootResourceManager do
-- rm1 <- newResourceManager
-- rm2 <- newResourceManager
-- liftIO $ atomically do
-- attachDisposable rm1 rm2
-- attachDisposable rm2 rm1
-- dispose rm1
--
-- it "can dispose a resource manager loop with a shared disposable" $ io do
-- var <- newTVarIO (0 :: Int)
-- d <- atomically $ newDisposable $ atomically $ modifyTVar var (+ 1)
-- withRootResourceManager do
-- rm1 <- newResourceManager
-- rm2 <- newResourceManager
-- liftIO $ atomically do
-- attachDisposable rm1 rm2
-- attachDisposable rm2 rm1
-- attachDisposable rm1 d
-- attachDisposable rm2 d
--
-- atomically (readTVar var) `shouldReturn` 1
--
--
-- describe "linkExecution" do
-- it "does not generate an exception after it is completed" $ io do
-- (`shouldThrow` \(_ :: CombinedException) -> True) do
-- withRootResourceManager do
-- linkExecution do
-- pure ()
-- throwToResourceManager TestException
-- liftIO $ threadDelay 100000
-- From DisposableSpec.hs:
--spec :: Spec
--spec = parallel $ do
-- describe "Disposable" $ do
-- describe "noDisposable" $ do
-- it "can be disposed" $ io do
-- dispose noDisposable
--
-- it "can be awaited" $ io do
-- await (isDisposed noDisposable)
--
-- describe "newDisposable" $ do
-- it "signals it's disposed state" $ io do
-- disposable <- atomically $ newDisposable $ pure ()
-- void $ forkIO $ threadDelay 100000 >> dispose disposable
-- await (isDisposed disposable)
--
-- it "can be disposed multiple times" $ io do
-- disposable <- atomically $ newDisposable $ pure ()
-- dispose disposable
-- dispose disposable
-- await (isDisposed disposable)
--
-- it "can be disposed in parallel" $ do
-- disposable <- atomically $ newDisposable $ threadDelay 100000
-- void $ forkIO $ dispose disposable
-- dispose disposable
-- await (isDisposed disposable)