Skip to content

Commit c228abb

Browse files
committed
Rewrite progress handling to allow for debouncing messages
This had to be redone in order to allow us to "wake up" and notice that there are pending messages. I also wrote it so there can be a stateful interface (the `ProgressTracker`) which I think might make it easier to use in that weird case in `ghcide`. I haven't exposed that yet, though.
1 parent 6fd1db3 commit c228abb

File tree

5 files changed

+202
-110
lines changed

5 files changed

+202
-110
lines changed

lsp-test/func-test/FuncTest.hs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Main where
88
import Colog.Core
99
import Colog.Core qualified as L
1010
import Control.Applicative.Combinators
11+
import Control.Concurrent.Extra (newBarrier, waitBarrier, signalBarrier)
1112
import Control.Exception
1213
import Control.Lens hiding (Iso, List)
1314
import Control.Monad
@@ -25,6 +26,7 @@ import System.Process
2526
import Test.Hspec
2627
import UnliftIO
2728
import UnliftIO.Concurrent
29+
import Debug.Trace
2830

2931
runSessionWithServer ::
3032
LogAction IO (WithSeverity LspServerLog) ->
@@ -53,7 +55,10 @@ spec = do
5355
let logger = L.cmap show L.logStringStderr
5456
describe "server-initiated progress reporting" $ do
5557
it "sends updates" $ do
56-
startBarrier <- newEmptyMVar
58+
startBarrier <- newBarrier
59+
b1 <- newBarrier
60+
b2 <- newBarrier
61+
b3 <- newBarrier
5762

5863
let definition =
5964
ServerDefinition
@@ -71,10 +76,13 @@ spec = do
7176
handlers =
7277
requestHandler (SMethod_CustomMethod (Proxy @"something")) $ \_req resp -> void $ forkIO $ do
7378
withProgress "Doing something" Nothing NotCancellable $ \updater -> do
74-
takeMVar startBarrier
79+
liftIO $ waitBarrier startBarrier
7580
updater $ ProgressAmount (Just 25) (Just "step1")
81+
liftIO $ waitBarrier b1
7682
updater $ ProgressAmount (Just 50) (Just "step2")
83+
liftIO $ waitBarrier b2
7784
updater $ ProgressAmount (Just 75) (Just "step3")
85+
liftIO $ waitBarrier b3
7886

7987
runSessionWithServer logger definition Test.defaultConfig Test.fullCaps "." $ do
8088
Test.sendRequest (SMethod_CustomMethod (Proxy @"something")) J.Null
@@ -86,25 +94,28 @@ spec = do
8694
guard $ has (L.params . L.value . _workDoneProgressBegin) x
8795

8896
-- allow the hander to send us updates
89-
putMVar startBarrier ()
97+
liftIO $ signalBarrier startBarrier ()
9098

9199
do
92100
u <- Test.message SMethod_Progress
93101
liftIO $ do
94102
u ^? L.params . L.value . _workDoneProgressReport . L.message `shouldBe` Just (Just "step1")
95103
u ^? L.params . L.value . _workDoneProgressReport . L.percentage `shouldBe` Just (Just 25)
104+
liftIO $ signalBarrier b1 ()
96105

97106
do
98107
u <- Test.message SMethod_Progress
99108
liftIO $ do
100109
u ^? L.params . L.value . _workDoneProgressReport . L.message `shouldBe` Just (Just "step2")
101110
u ^? L.params . L.value . _workDoneProgressReport . L.percentage `shouldBe` Just (Just 50)
111+
liftIO $ signalBarrier b2 ()
102112

103113
do
104114
u <- Test.message SMethod_Progress
105115
liftIO $ do
106116
u ^? L.params . L.value . _workDoneProgressReport . L.message `shouldBe` Just (Just "step3")
107117
u ^? L.params . L.value . _workDoneProgressReport . L.percentage `shouldBe` Just (Just 75)
118+
liftIO $ signalBarrier b3 ()
108119

109120
-- Then make sure we get a $/progress end notification
110121
skipManyTill Test.anyMessage $ do
@@ -196,6 +207,11 @@ spec = do
196207

197208
describe "client-initiated progress reporting" $ do
198209
it "sends updates" $ do
210+
startBarrier <- newBarrier
211+
b1 <- newBarrier
212+
b2 <- newBarrier
213+
b3 <- newBarrier
214+
199215
let definition =
200216
ServerDefinition
201217
{ parseConfig = const $ const $ Right ()
@@ -212,9 +228,13 @@ spec = do
212228
handlers =
213229
requestHandler SMethod_TextDocumentCodeLens $ \req resp -> void $ forkIO $ do
214230
withProgress "Doing something" (req ^. L.params . L.workDoneToken) NotCancellable $ \updater -> do
231+
liftIO $ waitBarrier startBarrier
215232
updater $ ProgressAmount (Just 25) (Just "step1")
233+
liftIO $ waitBarrier b1
216234
updater $ ProgressAmount (Just 50) (Just "step2")
235+
liftIO $ waitBarrier b2
217236
updater $ ProgressAmount (Just 75) (Just "step3")
237+
liftIO $ waitBarrier b3
218238

219239
runSessionWithServer logger definition Test.defaultConfig Test.fullCaps "." $ do
220240
Test.sendRequest SMethod_TextDocumentCodeLens (CodeLensParams (Just $ ProgressToken $ InR "hello") Nothing (TextDocumentIdentifier $ Uri "."))
@@ -224,23 +244,28 @@ spec = do
224244
x <- Test.message SMethod_Progress
225245
guard $ has (L.params . L.value . _workDoneProgressBegin) x
226246

247+
liftIO $ signalBarrier startBarrier ()
248+
227249
do
228250
u <- Test.message SMethod_Progress
229251
liftIO $ do
230252
u ^? L.params . L.value . _workDoneProgressReport . L.message `shouldBe` Just (Just "step1")
231253
u ^? L.params . L.value . _workDoneProgressReport . L.percentage `shouldBe` Just (Just 25)
254+
liftIO $ signalBarrier b1 ()
232255

233256
do
234257
u <- Test.message SMethod_Progress
235258
liftIO $ do
236259
u ^? L.params . L.value . _workDoneProgressReport . L.message `shouldBe` Just (Just "step2")
237260
u ^? L.params . L.value . _workDoneProgressReport . L.percentage `shouldBe` Just (Just 50)
261+
liftIO $ signalBarrier b2 ()
238262

239263
do
240264
u <- Test.message SMethod_Progress
241265
liftIO $ do
242266
u ^? L.params . L.value . _workDoneProgressReport . L.message `shouldBe` Just (Just "step3")
243267
u ^? L.params . L.value . _workDoneProgressReport . L.percentage `shouldBe` Just (Just 75)
268+
liftIO $ signalBarrier b3 ()
244269

245270
-- Then make sure we get a $/progress end notification
246271
skipManyTill Test.anyMessage $ do

lsp-test/lsp-test.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ test-suite func-test
128128
, base
129129
, aeson
130130
, co-log-core
131+
, extra
131132
, hspec
132133
, lens
133134
, lsp

lsp/lsp.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ library
7676
, text >=1 && <2.2
7777
, text-rope ^>=0.2
7878
, transformers >=0.5 && <0.7
79+
, unliftio ^>=0.2
7980
, unliftio-core ^>=0.2
8081
, unordered-containers ^>=0.2
8182
, uuid >=1.3

0 commit comments

Comments
 (0)