wire-server icon indicating copy to clipboard operation
wire-server copied to clipboard

Reduce duplication of mock https servers in integration tests.

Open fisx opened this issue 6 years ago • 0 comments

There are a number of functions that create https servers for integration tests:

  • https://github.com/wireapp/wire-server/blob/b266f9aae3ee8bf31ac83413b7e7f37feb8aa488/services/brig/test/integration/API/Provider.hs#L1519-L1531
  • https://github.com/wireapp/wire-server/blob/b266f9aae3ee8bf31ac83413b7e7f37feb8aa488/services/galley/test/integration/API/Teams/LegalHold.hs#L805-L825
  • ...? (git grep -Hn runTLS suggests these are the only ones)

The following variant has more sophisticated error reporting, but it has been introduced in spar and then removed in the same PR, so it's gone from the code for now:

-- | Copied from galley's 'withTestService'
--
-- FUTUREWORK: use 'asyncWithBound' (that requires the test case to run in IO, though, or some
-- 'unliftIO' magic.)
--
-- The https server may take a few miliseconds (or, depending on your test setup, longer)
-- before it is available.  To accomodate for this, the test will be run several times if it
-- throws an exception, and only after a few re-tries, the latest exception will be thrown.
withMockIdP
    :: (HasCallStack)
    => (Chan e -> Application)  -- ^ the mock service
    -> (Chan e -> TestSpar a)   -- ^ the test
    -> TestSpar a
withMockIdP createApp go = do
    config <- view teMockIdP
    let tlss = Warp.tlsSettings (cert config) (privateKey config)
    let defs = Warp.defaultSettings { Warp.settingsPort = botPort config }
    buf <- liftIO newChan
    srv <- liftIO . Async.async $
        Warp.runTLS tlss defs $
            createApp buf
    patiently (reportIfServerDied srv >> go buf) `finally` liftIO (Async.cancel srv)
  where
    reportIfServerDied :: Async.Async () -> TestSpar ()
    reportIfServerDied srv = liftIO (Async.poll srv >>= maybe (pure ()) print)

    patiently :: TestSpar a -> TestSpar a
    patiently = recovering policy continue . const
      where
        policy = limitRetries 3 <> exponentialBackoff 100000
        continue = [\_ -> Control.Monad.Catch.Handler (\(SomeException _) -> pure True)]

Tasks:

  • [ ] provide a variant of withMockIdP from some library (bilge? wai-utilities?)
  • [ ] use that implementation instead of the ones listed above.

fisx avatar Aug 02 '19 10:08 fisx