|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE MonadComprehensions #-} |
| 3 | +{-# LANGUAGE NamedFieldPuns #-} |
| 4 | +module Observation.SchemaCacheSpec where |
| 5 | + |
| 6 | +import Network.Wai (Application) |
| 7 | +import ObsHelper |
| 8 | +import qualified PostgREST.AppState as AppState |
| 9 | +import PostgREST.Config (configDbSchemas) |
| 10 | +import PostgREST.Observation |
| 11 | +import Protolude |
| 12 | +import Test.Hspec (SpecWith, describe, it) |
| 13 | +import Test.Hspec.Wai (getState) |
| 14 | + |
| 15 | +spec :: SpecWith (SpecState, Application) |
| 16 | +spec = describe "Server started with metrics enabled" $ do |
| 17 | + |
| 18 | + it "Should emit PoolFlushed, SchemaCacheQueriedObs and SchemaCacheLoadedObs when schema cache is reloaded" $ do |
| 19 | + SpecState{specAppState = appState, specObsChan} <- getState |
| 20 | + let waitFor = waitForObs specObsChan |
| 21 | + |
| 22 | + liftIO $ do |
| 23 | + AppState.schemaCacheLoader appState |
| 24 | + |
| 25 | + waitFor (1 * sec) "PoolFlushed" $ \x -> [ o | o@PoolFlushed <- pure x ] |
| 26 | + waitFor (1 * sec) "SchemaCacheQueriedObs" $ \x -> [ o | o@SchemaCacheQueriedObs{} <- pure x ] |
| 27 | + waitFor (1 * sec) "SchemaCacheLoadedObs" $ \x -> [ o | o@SchemaCacheLoadedObs{} <- pure x ] |
| 28 | + |
| 29 | + |
| 30 | + it "Should flush pool multiple times when schema reloading retries" $ do |
| 31 | + SpecState{specAppState = appState, specObsChan} <- getState |
| 32 | + let waitFor = waitForObs specObsChan |
| 33 | + |
| 34 | + liftIO $ do |
| 35 | + AppState.getConfig appState >>= \cfg -> do |
| 36 | + AppState.putConfig appState $ cfg { configDbSchemas = pure "bad_schema" } |
| 37 | + AppState.schemaCacheLoader appState |
| 38 | + |
| 39 | + waitFor (1 * sec) "PoolFlushed 1" $ \x -> [ o | o@PoolFlushed <- pure x ] |
| 40 | + waitFor (1 * sec) "SchemaCacheErrorObs" $ \x -> [ o | o@SchemaCacheErrorObs{} <- pure x ] |
| 41 | + |
| 42 | + -- Restore configuration |
| 43 | + AppState.putConfig appState cfg |
| 44 | + |
| 45 | + -- Wait for 2 seconds so that retry can happen |
| 46 | + waitFor (2 * sec) "PoolFlushed 2" $ \x -> [ o | o@PoolFlushed <- pure x ] |
| 47 | + waitFor (1 * sec) "SchemaCacheQueriedObs" $ \x -> [ o | o@SchemaCacheQueriedObs{} <- pure x ] |
| 48 | + waitFor (1 * sec) "SchemaCacheLoadedObs" $ \x -> [ o | o@SchemaCacheLoadedObs{} <- pure x ] |
| 49 | + |
| 50 | + where |
| 51 | + sec = 1000000 |
0 commit comments