module Test.Migration.Connections (testConnectionsMigrationToPostgres) where
import API.Brig (getConnection, postConnection, putConnection)
import Control.Monad.Codensity
import Control.Monad.Reader
import GHC.Stack
import SetupHelpers
import Test.Migration.Util (assertMigrationSuccessful, waitForMigration)
import Testlib.Prelude
import Testlib.ResourcePool
testConnectionsMigrationToPostgres :: (HasCallStack) => App ()
testConnectionsMigrationToPostgres :: HasCallStack => App ()
testConnectionsMigrationToPostgres = do
resourcePool <- (Env -> ResourcePool BackendResource)
-> App (ResourcePool BackendResource)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (.resourcePool)
runCodensity (acquireResources 1 resourcePool) $ \[BackendResource
backend] -> do
let domain :: String
domain = BackendResource
backend.berDomain
cassandraPair <-
Codensity App String -> forall b. (String -> App b) -> App b
forall k (m :: k -> *) a.
Codensity m a -> forall (b :: k). (a -> m b) -> m b
runCodensity (HasCallStack =>
BackendResource -> ServiceOverrides -> Codensity App String
BackendResource -> ServiceOverrides -> Codensity App String
startDynamicBackend BackendResource
backend (String -> Bool -> ServiceOverrides
forall {b} {b}. (ToJSON b, ToJSON b) => b -> b -> ServiceOverrides
conf String
"cassandra" Bool
False)) ((String -> App (Value, Value)) -> App (Value, Value))
-> (String -> App (Value, Value)) -> App (Value, Value)
forall a b. (a -> b) -> a -> b
$ \String
_ ->
String -> App (Value, Value)
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App (Value, Value)
connectFreshPair String
domain
dualWritePair <-
runCodensity (startDynamicBackend backend (conf "migration-to-postgresql" False)) $ \String
_ -> do
HasCallStack => (Value, Value) -> App ()
(Value, Value) -> App ()
checkConnection (Value, Value)
cassandraPair
String -> App (Value, Value)
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App (Value, Value)
connectFreshPair String
domain
workerOnPair <-
runCodensity (startDynamicBackend backend (conf "migration-to-postgresql" True)) $ \String
_ -> do
HasCallStack => (Value, Value) -> App ()
(Value, Value) -> App ()
checkConnection (Value, Value)
dualWritePair
pair <- String -> App (Value, Value)
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App (Value, Value)
connectFreshPair String
domain
waitForMigration domain connectionsMigrationFinishedCounterName
assertMigrationSuccessful domain "^wire_connections_migration_failed"
pure pair
runCodensity (startDynamicBackend backend (conf "postgresql" False)) $ \String
_ -> do
HasCallStack => (Value, Value) -> App ()
(Value, Value) -> App ()
checkConnection (Value, Value)
cassandraPair
HasCallStack => (Value, Value) -> App ()
(Value, Value) -> App ()
checkConnection (Value, Value)
dualWritePair
HasCallStack => (Value, Value) -> App ()
(Value, Value) -> App ()
checkConnection (Value, Value)
workerOnPair
pgPair <- String -> App (Value, Value)
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App (Value, Value)
connectFreshPair String
domain
checkConnection pgPair
where
conf :: b -> b -> ServiceOverrides
conf b
db b
runMigration =
ServiceOverrides
forall a. Default a => a
def
{ brigCfg = setField "postgresMigration.connections" db,
backgroundWorkerCfg =
setField "postgresMigration.connections" db
>=> setField "migrateConnections" runMigration
}
connectionsMigrationFinishedCounterName :: String
connectionsMigrationFinishedCounterName :: String
connectionsMigrationFinishedCounterName = String
"^wire_connections_migration_finished"
connectFreshPair :: (HasCallStack, MakesValue domain) => domain -> App (Value, Value)
connectFreshPair :: forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App (Value, Value)
connectFreshPair domain
domain = do
[u1, u2] <- [domain] -> App [Value]
forall domain.
(HasCallStack, MakesValue domain) =>
[domain] -> App [Value]
createUsers [domain
domain, domain
domain]
void $ postConnection u1 u2 >>= assertSuccess
void $ putConnection u2 u1 "accepted" >>= assertSuccess
pure (u1, u2)
checkConnection :: (HasCallStack) => (Value, Value) -> App ()
checkConnection :: HasCallStack => (Value, Value) -> App ()
checkConnection (Value
u1, Value
u2) =
Value -> Value -> App Response
forall userFrom userTo.
(HasCallStack, MakesValue userFrom, MakesValue userTo) =>
userFrom -> userTo -> App Response
getConnection Value
u1 Value
u2 App Response -> (Response -> App ()) -> App ()
forall a.
HasCallStack =>
App Response -> (Response -> App a) -> App a
`bindResponse` \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"status" App Value -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"accepted"