diff --git a/changelog.d/5-internal/perhaps-deflake-search-tests b/changelog.d/5-internal/perhaps-deflake-search-tests new file mode 100644 index 0000000000..fa50f821a9 --- /dev/null +++ b/changelog.d/5-internal/perhaps-deflake-search-tests @@ -0,0 +1 @@ +Attempt to deflake a flaky integration test about search diff --git a/services/brig/test/integration/API/Search.hs b/services/brig/test/integration/API/Search.hs index 084edb76bc..9bea8efb8b 100644 --- a/services/brig/test/integration/API/Search.hs +++ b/services/brig/test/integration/API/Search.hs @@ -263,15 +263,15 @@ testOrderName brig = do nameMatch <- userQualifiedId <$> createUser' True searchedWord brig namePrefixMatch <- userQualifiedId <$> createUser' True (searchedWord <> "suffix") brig refreshIndex brig - results <- searchResults <$> executeSearch brig searcher searchedWord - let resultUIds = map contactQualifiedId results let expectedOrder = [nameMatch, namePrefixMatch] - let dbg = "results: " <> show results <> "\nsearchedWord: " <> cs searchedWord + -- possibly there is some delay in refreshing of the index writes being + -- visible for subsequent reads, so we try this a few times. + results <- aFewTimes 12 (toIds <$> executeSearch brig searcher searchedWord) (== expectedOrder) liftIO $ assertEqual - ("Expected order: name match, name prefix match.\n\nSince this test fails sporadically for unknown reasons here is some debug info:\n" <> dbg) + "If this test fails again, maybe we should consider deleting it and living with some more uncertainty" expectedOrder - resultUIds + results testOrderHandle :: TestConstraints m => Brig -> m () testOrderHandle brig = do @@ -282,15 +282,18 @@ testOrderHandle brig = do handlePrefixMatch <- userQualifiedId <$> createUser' True "handle prefix match" brig void $ putHandle brig (qUnqualified handlePrefixMatch) (searchedWord <> "suffix") refreshIndex brig - results <- searchResults <$> executeSearch brig searcher searchedWord - let resultUIds = map contactQualifiedId results let expectedOrder = [handleMatch, handlePrefixMatch] - let dbg = "results: " <> show results <> "\nsearchedWord: " <> cs searchedWord + -- possibly there is some delay in refreshing of the index writes being + -- visible for subsequent reads, so we try this a few times. + results <- aFewTimes 12 (toIds <$> executeSearch brig searcher searchedWord) (== expectedOrder) liftIO $ assertEqual - ("Expected order: handle match, handle prefix match.\n\nSince this test fails sporadically for unknown reasons here here is some debug info:\n" <> dbg) + "If this test fails again, maybe we should consider deleting it and living with some more uncertainty" expectedOrder - resultUIds + results + +toIds :: SearchResult Contact -> [Qualified UserId] +toIds = map contactQualifiedId . searchResults testSearchTeamMemberAsNonMemberDisplayName :: TestConstraints m => Brig -> m () testSearchTeamMemberAsNonMemberDisplayName brig = do diff --git a/services/brig/test/integration/API/UserPendingActivation.hs b/services/brig/test/integration/API/UserPendingActivation.hs index 4aa3dbf775..c228de38bb 100644 --- a/services/brig/test/integration/API/UserPendingActivation.hs +++ b/services/brig/test/integration/API/UserPendingActivation.hs @@ -35,7 +35,6 @@ import Control.Exception (assert) import Control.Lens ((^.), (^?)) import Control.Monad.Catch (MonadCatch) import Control.Monad.Random -import Control.Retry (exponentialBackoff, limitRetries, retrying) import Data.Aeson hiding (json) import qualified Data.Aeson as Aeson import Data.Aeson.Lens (key, _String) @@ -363,20 +362,3 @@ acceptWithName name email code = "password" Aeson..= defPassword, "team_code" Aeson..= code ] - --- | Run a probe several times, until a "good" value materializes or until patience runs out -aFewTimes :: - (HasCallStack, MonadIO m) => - -- | Number of retries. Exponentially: 11 ~ total of 2 secs delay, 12 ~ 4 secs delay, ... - Int -> - m a -> - (a -> Bool) -> - m a -aFewTimes - retries - action - good = do - retrying - (exponentialBackoff 1000 <> limitRetries retries) - (\_ -> pure . not . good) - (\_ -> action) diff --git a/services/brig/test/integration/Util.hs b/services/brig/test/integration/Util.hs index d33c4d61c3..1b8787b11b 100644 --- a/services/brig/test/integration/Util.hs +++ b/services/brig/test/integration/Util.hs @@ -815,6 +815,23 @@ recoverN n m = (constantDelay 1000000 <> limitRetries n) (const m) +-- | Run a probe several times, until a "good" value materializes or until patience runs out +aFewTimes :: + (HasCallStack, MonadIO m) => + -- | Number of retries. Exponentially: 11 ~ total of 2 secs delay, 12 ~ 4 secs delay, ... + Int -> + m a -> + (a -> Bool) -> + m a +aFewTimes + retries + action + good = do + retrying + (exponentialBackoff 1000 <> limitRetries retries) + (\_ -> pure . not . good) + (\_ -> action) + -- | This allows you to run requests against a brig instantiated using the given options. -- Note that ONLY 'brig' calls should occur within the provided action, calls to other -- services will fail.