packages feed

hs-hath-1.4.0: test/RPCSpec.hs

module RPCSpec ( rpcSpecs ) where

import qualified Data.ByteString.Char8  as BS
import qualified Data.Text              as T
import           Data.Time.Clock.System ( SystemTime(systemSeconds), getSystemTime )

import           Hath.Config            ( ClientConfig(..), defaultClientConfig )
import           Hath.Error             ( HathError(..) )
import           Hath.FileURI           ( parseFileURI )
import           Hath.RPC               ( RPCServers(..)
                                        , StartFailure(..)
                                        , Stop(..)
                                        , clientStart
                                        , failedRPCServer
                                        , getBlacklist
                                        , loginFailure
                                        , nextGallery
                                        , overload
                                        , pickRPCServer
                                        , runRPCIO
                                        , startFailure
                                        , toldToStop
                                        , worthRetrying
                                        )
import           Hath.ServerTime        ( serverNow, setServerTime )

import           MockRPC

import           Relude

import           Test.Hspec

config :: ClientConfig
config = defaultClientConfig { clientId = "test-client-001", key = "test-secret-key-12345" }

-- | Back to our own clock, so that the other specs sign with it.
useOurClock :: IO ()
useOurClock = setServerTime . systemSeconds =<< getSystemTime

rpcSpecs :: Spec
rpcSpecs = describe "RPC" $ after_ useOurClock $ do
  describe "worthRetrying" $ do
    it "repeats an exchange that broke off or met a server error" $ do
      worthRetrying Nothing `shouldBe` True
      worthRetrying (Just 500) `shouldBe` True
      worthRetrying (Just 403) `shouldBe` True

    it "takes 404 and 410 as final" $ do
      worthRetrying (Just 404) `shouldBe` False
      worthRetrying (Just 410) `shouldBe` False

  describe "a request" $ do
    it "gets through when two attempts fail"
      $ withMockRPC defaultMockRPC { mockOutages = 2 } (runRPCIO config clientStart)
      >>= (`shouldBe` Right ())

    it "fails after three attempts"
      $ withMockRPC defaultMockRPC { mockOutages = 3 } (runRPCIO config clientStart)
      >>= (`shouldBe` Left (TransportError "HTTP 500"))

    it "gives up on a reply of more than 10 MiB" $ do
      queued <- withMockRPC
        defaultMockRPC { mockQueue = const (BS.replicate 10485761 'x') }
        (runRPCIO config (nextGallery Nothing))
      void queued `shouldBe` Left (TransportError "a reply of more than 10 MiB")

  describe "on KEY_EXPIRED" $ do
    it "takes the server's time and signs a request without add again" $ do
      started <- withMockRPC defaultMockRPC { mockClockAhead = 3600 } (runRPCIO config clientStart)
      started `shouldBe` Right ()
      ahead <- (-) <$> serverNow <*> (systemSeconds <$> getSystemTime)
      ahead `shouldSatisfy` \seconds -> abs (seconds - 3600) <= 1

    it "gives up on a request with add, as the official client does"
      $ withMockRPC defaultMockRPC { mockClockAhead = 3600 } (runRPCIO config (getBlacklist 60))
      >>= (`shouldBe` Left (RequestFailure "KEY_EXPIRED"))

  describe "a failed client_start" $ do
    let refused = \case
          Refused _     -> True
          Unannounced _ -> False

    it "ends the client when another client holds the address or the client ID"
      $ forM_ [ "FAIL_OTHER_CLIENT_CONNECTED", "FAIL_CID_IN_USE" ]
      $ \status -> startFailure 443 (RequestFailure status) `shouldSatisfy` refused

    it "leaves it up after a failed connectivity test, naming the port"
      $ startFailure 443 (RequestFailure "FAIL_CONNECT_TEST") `shouldSatisfy` \case
        Unannounced why -> "443" `T.isInfixOf` why
        Refused _       -> False

    it "leaves it up after any other failure"
      $ forM_ [ RequestFailure "FAIL_STARTUP_FLOOD", TransportError "HTTP 500", EmptyResponse ]
      $ \failure -> startFailure 443 failure `shouldSatisfy` (not . refused)

  describe "a failed still_alive" $ do
    it "stops the client on TERM_BAD_NETWORK and FAIL_NOT_LOGGED_IN" $ do
      toldToStop (RequestFailure "TERM_BAD_NETWORK") `shouldBe` Just BadNetwork
      toldToStop (RequestFailure "FAIL_NOT_LOGGED_IN") `shouldBe` Just NotLoggedIn

    it "waits for the next one otherwise"
      $ map toldToStop [ RequestFailure "FAIL", TransportError "HTTP 500", EmptyResponse ]
      `shouldBe` [ Nothing, Nothing, Nothing ]

  describe "a failed login" $ do
    it "waits out the 15 minute block of a startup loop"
      $ fst (loginFailure (RequestFailure "FAIL_STARTUP_LOOP")) `shouldBe` 900

    it "waits 5 s otherwise" $ map
      (fst . loginFailure)
      [ RequestFailure "FAIL_UNSUPPORTED_LOCATION", RequestFailure "FAIL", TransportError "HTTP 500" ]
      `shouldBe` [ 5, 5, 5 ]

  describe "the RPC server" $ do
    let servers = RPCServers [ "192.0.2.1", "192.0.2.2", "192.0.2.3" ] 80 Nothing Nothing

    it "is one of those assigned, kept until it fails" $ do
      let picked = pickRPCServer 1 servers
      rpcCurrent picked `shouldBe` Just "192.0.2.2"
      rpcCurrent (pickRPCServer 0 picked) `shouldBe` Just "192.0.2.2"

    it "is never the one that failed last, while there are others" $ do
      let failed = failedRPCServer "192.0.2.2" (pickRPCServer 1 servers)
      rpcCurrent failed `shouldBe` Nothing
      forM_ [ 0 .. 5 ] $ \n -> rpcCurrent (pickRPCServer n failed) `shouldNotBe` Just "192.0.2.2"

    it "stays the one that failed when it is the only one" $ do
      let only = RPCServers [ "192.0.2.1" ] 80 Nothing (Just "192.0.2.1")
      rpcCurrent (pickRPCServer 0 only) `shouldBe` Just "192.0.2.1"

    it "is none until some are assigned"
      $ rpcCurrent (pickRPCServer 0 (RPCServers [] 80 Nothing Nothing)) `shouldBe` Nothing

  describe "overload" $ it "is sent as act=overload" $ do
    seen <- newIORef []
    sent <- withMockRPC
      defaultMockRPC { mockSeen = atomicModifyIORef'_ seen . (:) . fst }
      (runRPCIO config overload)
    sent `shouldBe` Right ()
    readIORef seen `shouldReturn` [ "overload" ]

  describe "get_blacklist" $ it "gives the file ids, skipping lines that are not one" $ do
    let fileId = "0123456789abcdef0123456789abcdef01234567-1000-800-600-jpg"
    blacklist <- withMockRPC
      defaultMockRPC { mockBlacklist = [ fileId, "", "not-a-file-id" ] }
      (runRPCIO config (getBlacklist 43200))
    blacklist `shouldBe` Right (maybeToList (parseFileURI fileId))