memcache 0.4.2.0 → 0.4.2.1
raw patch · 12 files changed
+504/−63 lines, 12 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- CHANGELOG.md +41/−0
- Database/Memcache/Client.hs +40/−2
- Database/Memcache/Cluster.hs +7/−1
- Database/Memcache/ElastiCacheClient.hs +5/−0
- Database/Memcache/Errors.hs +6/−1
- Database/Memcache/Server.hs +66/−2
- Database/Memcache/Socket.hs +25/−0
- Database/Memcache/Types.hs +3/−1
- README.md +18/−3
- memcache.cabal +6/−3
- test/MockServer.hs +22/−3
- test/Spec.hs +265/−47
CHANGELOG.md view
@@ -1,3 +1,44 @@+# 0.4.2.1 - November 18th, 2026++No API changes from 0.4.2.0.++- `set`, `add`, `replace`, `cas`, `append` and `prepend` reject a value+ larger than 1GiB, memcached's largest possible item size, with+ `OpError ErrValueTooLarge` before sending anything. A value of about 2GiB+ or more (longer than 2147483645 bytes) previously made memcached run the+ value's bytes as commands, so storing untrusted data could run commands on+ the server, and a value over 1GiB could time out, be sent again and mark+ the server dead. Such a value no longer reaches the server, so it no+ longer makes memcached delete the key's existing value, as memcached does+ whenever it rejects a store as too large; a value within 1GiB but over the+ server's own limit (`-I`, 1MiB by default) still does. Every operation+ that sends a value now evaluates it first, before its timeout starts: an+ exception it throws is no longer retried and no longer marks the server+ dead, and time spent computing it no longer counts towards+ `optsServerTimeout`. The request encoders in `Database.Memcache.Socket`+ still encode a value of any length; don't send one longer than 1GiB.+- memcached 1.6.10 or later is now required (previously documented as+ 1.6.0). memcached before 1.6.10 rejects the meta set command this library+ stores values with, without reading the value that follows it, and then+ runs the value's bytes as commands, so storing untrusted data could run+ commands on the server. Each new connection now asks the server for its+ version before it is used: until that check passes, the only requests sent+ are the authentication exchange, when credentials are set, and `version`.+ A server older than 1.6.10, or any other reply that is neither an error+ nor a readable version, is refused with `ProtocolError (UnknownOp ...)`,+ whose message quotes the reply; the connection is closed, and the refusal+ is not retried and doesn't mark the server dead. An error reply, such as+ `CLIENT_ERROR unauthenticated`, throws what it throws for any request, and+ so does a reply that can't be parsed, such as an empty line or an overlong+ one, which counts against the server. This covers ElastiCache discovery,+ which now fails for a cluster running a memcached older than 1.6.10.+ Through a proxy, the check sees only the proxy's version, so every+ memcached behind it must be 1.6.10 or later too. Each new connection that+ an operation opens takes one more round trip, which counts towards+ `optsServerTimeout`. Connections made directly with+ `Database.Memcache.Socket.newConnection` are not checked; send+ `storeRequest` and `modifyRequest` only to 1.6.10 or later.+ # 0.4.2.0 - October 20th, 2026 No API changes from 0.4.0.0. This is the first Hackage release since 0.4.0.0,
Database/Memcache/Client.hs view
@@ -14,8 +14,17 @@ connect to a single Memcached server or a cluster of servers; clusters use consistent hashing to route requests to the appropriate server. -This requires memcached 1.6.0 or newer, the first release with the meta-commands; older servers reject every operation with @ERROR@.+This requires memcached 1.6.10 or newer. Older servers reject the meta set+command this client stores values with, without reading the value that+follows it, and then run the value's bytes as commands. So each new connection+asks the server for its version before it is used: until that check passes,+the only requests sent are the authentication exchange, when credentials are+set, and @version@. Unless the server reports 1.6.10 or later, the connection+is closed and v'ProtocolError' 'UnknownOp' is thrown, quoting the server's+reply; an error reply, such as @CLIENT_ERROR unauthenticated@, throws what it+throws for any request. A refused version isn't retried, and doesn't mark the+server as failed. Through a proxy, the check sees only the proxy's version, so+every memcached behind it must be 1.6.10 or later too. Authentication uses Memcached's experimental text-protocol auth-file mechanism (@-Y authfile@), introduced in memcached 1.5.15. Enabling @-Y@ also disables@@ -136,6 +145,11 @@ -- | Create a client for the supplied servers. An empty list uses the default -- local server specification.+--+-- Nothing is sent until an operation needs a connection to a server. Each new+-- connection then checks the server's version, and refuses a server older than+-- memcached 1.6.10: until the check passes, the only requests sent are the+-- authentication exchange, when credentials are set, and @version@. newClient :: [ServerSpec] -> Options -> IO Client newClient scs = newCluster $ if null scs then [def] else scs @@ -198,6 +212,11 @@ -- | Store a new (or overwrite existing) key-value pair, returning its @Version@ -- identifier. Use an expiration value of @0@ to store forever.+--+-- A value larger than 1 GiB, the largest item memcached can store, is+-- rejected with 'OpError' 'ErrValueTooLarge' before anything is sent, as it+-- is by every operation that sends a value. memcached itself rejects a value+-- too large for its item size limit (its @-I@ option) with the same status. set :: Cluster -> Key -> Value -> Flags -> Expiration -> IO Version set c k v f e = store c k v f e 0 Set "SET" @@ -211,6 +230,7 @@ -- with 'set'. cas :: Cluster -> Key -> Value -> Flags -> Expiration -> Version -> IO (Maybe Version) cas c k v f e ver = do+ checkValue v response <- keyedOp c k (\wire -> storeRequest Set wire v f e ver) status <- responseStatus response case status of@@ -224,6 +244,7 @@ -- expiration value of @0@ to store forever. add :: Cluster -> Key -> Value -> Flags -> Expiration -> IO (Maybe Version) add c k v f e = do+ checkValue v response <- keyedOp c k (\wire -> storeRequest Add wire v f e 0) status <- responseStatus response case status of@@ -239,6 +260,7 @@ -- server, otherwise 'Nothing' is returned. A version of @0@ skips the check. replace :: Cluster -> Key -> Value -> Flags -> Expiration -> Version -> IO (Maybe Version) replace c k v f e ver = do+ checkValue v response <- keyedOp c k (\wire -> storeRequest Replace wire v f e ver) status <- responseStatus response case status of@@ -250,12 +272,27 @@ store :: Cluster -> Key -> Value -> Flags -> Expiration -> Version -> StoreMode -> String -> IO Version store c k v f e ver mode operation = do+ checkValue v response <- keyedOp c k (\wire -> storeRequest mode wire v f e ver) status <- responseStatus response case status of NoError -> return $ responseCas response _ -> failResponse response operation +-- | The largest value memcached can store: 1 GiB, its+-- @ITEM_SIZE_MAX_UPPER_LIMIT@, which caps its @-I@ item size option.+maxValueLength :: Int+maxValueLength = 1073741824++-- | Throw 'OpError' 'ErrValueTooLarge', the status memcached returns for a+-- value too large to store, if the value is longer than @maxValueLength@.+-- Every operation that sends a value calls this first, so that nothing is+-- sent: memcached mishandles a data length over 2147483645 bytes and then+-- runs the rest of the value's bytes as commands. This evaluates the value+-- before a connection is taken.+checkValue :: Value -> IO ()+checkValue v = when (B.length v > maxValueLength) $ throwStatus ErrValueTooLarge+ -- | Increment a numeric value stored against a key, returning the incremented -- value and the @Version@ identifier of the key-value pair. Throws -- 'OpError' 'ErrValueNonNumeric' if the stored value isn't a number.@@ -315,6 +352,7 @@ modify :: Cluster -> Key -> Value -> Version -> StoreMode -> String -> IO (Maybe Version) modify c k v ver mode operation = do+ checkValue v response <- keyedOp c k (\wire -> modifyRequest mode wire v ver) status <- responseStatus response case status of
Database/Memcache/Cluster.hs view
@@ -71,6 +71,10 @@ -- | How long to wait for an operation to complete before considering -- it failed. --+ -- When an attempt at an operation has to open a new connection, this+ -- also covers opening it: connecting, authenticating when there are+ -- credentials, and asking the server for its version.+ -- -- Default is 750ms. optsServerTimeout :: Milli, -- | Figure out which server to talk to for a given key.@@ -310,7 +314,9 @@ -- | A rejected command or a reported operation status is an answer, not a -- server failure: retrying it will fail identically and must not count towards--- marking the server dead.+-- marking the server dead. That includes the 'UnknownOp' a new connection+-- throws when it refuses the server's version: a retry would find the same+-- version. clientFault :: SomeException -> Bool clientFault err = case fromException err of Just (ProtocolError BadCommand{}) -> True
Database/Memcache/ElastiCacheClient.hs view
@@ -79,6 +79,11 @@ -- -- This function discovers all the nodes in a cluster and sets up a new thread to run autodiscovery on an interval. --+-- Discovery connects to the configuration endpoint like any other server, so+-- it fails for a cluster running a memcached older than 1.6.10, having sent+-- only the authentication exchange, when the endpoint has credentials, and+-- @version@ (see "Database.Memcache.Client").+-- -- N.B. The interval is in the same units as 'threadDelay'. newClient :: Options -> Int -> ConfigurationEndpoint -> IO Client newClient options interval cfgEndpoint = bracket acquireCfgClient releaseCfgClient resolveCluster'
Database/Memcache/Errors.hs view
@@ -54,7 +54,12 @@ data ProtocolError -- | Received an unknown response line. = UnknownPkt { protocolError :: String }- -- | Unknown Memcached operation.+ -- | Unknown Memcached operation. Also thrown when a new connection asks+ -- the server for its version and the reply is a version below 1.6.10, or+ -- any other reply that is neither an error nor a readable version; the+ -- message quotes the reply. The connection is then closed, having sent+ -- only the authentication exchange, when credentials are set, and+ -- @version@. | UnknownOp { protocolError :: String } -- | Unknown Memcached status field value. | UnknownStatus { protocolError :: String }
Database/Memcache/Server.hs view
@@ -1,5 +1,6 @@-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE CPP #-}+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE CPP #-}+{-# LANGUAGE OverloadedStrings #-} {-| Module : Database.Memcache.Server@@ -12,6 +13,13 @@ Handles the connections between a Memcached client and a single server. +Each new connection is authenticated, when its 'ServerSpec' has credentials,+and then asks the server for its version before it is used. Unless the server+reports memcached 1.6.10 or later, the connection is closed, having sent+nothing but the authentication exchange, when there are credentials, and+@version@, and v'ProtocolError' 'UnknownOp' is thrown (or, for an error reply,+what that reply throws for any request).+ Memcached expected errors (part of the protocol) are returned in the Response; unexpected errors, such as network failures, are thrown as exceptions. The server's 'failed' timestamp is used by cluster retry handling.@@ -25,8 +33,12 @@ ) where import Database.Memcache.Auth+import Database.Memcache.Errors import Database.Memcache.Socket +import Control.Monad (void, when)+import qualified Data.ByteString.Char8 as C+import Data.Char (isDigit) import Data.Default import Data.Hashable import Data.IORef@@ -149,8 +161,60 @@ S.setSocketOption s S.NoDelay 1 buffered <- newConnection s authenticate buffered ssAuth+ checkVersion buffered return buffered )++-- | The oldest memcached this client works with. From 1.6.10, the meta set+-- command that every store sends takes the data length as the token after+-- the key. Older servers reject the command without reading the data that+-- follows it, and then run the stored value's bytes as commands.+minimumVersion :: (Integer, Integer, Integer)+minimumVersion = (1, 6, 10)++-- | Ask the server for its version, and throw unless it is at least+-- @minimumVersion@. Every new connection does this, after authenticating when+-- there are credentials, before it is used for anything else; @connectSocket@+-- closes the connection when this throws.+--+-- An error reply, such as @CLIENT_ERROR unauthenticated@ from a server in+-- auth-file mode when no credentials are set, throws what it throws as the+-- reply to any request. Any other reply that isn't @VERSION@ and a version of+-- at least @minimumVersion@ throws v'ProtocolError' 'UnknownOp', quoting the+-- reply and naming the minimum. Retries treat that as a client fault, like a+-- rejected command: the server's version won't change on a retry, so the+-- operation fails at once and the server isn't marked as failed.+checkVersion :: Connection -> IO ()+checkVersion connection = do+ sendRaw connection versionRequest+ response <- recvResponse connection+ when (responseCode response `elem` ["ERROR", "CLIENT_ERROR", "SERVER_ERROR"]) $+ void (responseStatus response)+ case responseVersion response of+ Just v | v >= minimumVersion -> return ()+ _ -> throwIO $ ProtocolError UnknownOp+ { protocolError = "memcached " <> showVersion minimumVersion+ <> " or later is required, but the server answered version with "+ <> show (C.unwords (responseCode response : responseTokens response))+ }+ where+ showVersion (major, minor, patch) =+ show major <> "." <> show minor <> "." <> show patch++-- | The version in a @VERSION@ reply, as its first three numbers: major,+-- minor and patch, separated by dots. Anything after the third number, such+-- as a @-rc1@ suffix or a word after the version, is ignored.+responseVersion :: Response -> Maybe (Integer, Integer, Integer)+responseVersion (Response "VERSION" (token:_) _) = do+ (major, afterMajor) <- number token+ (minor, afterMinor) <- number =<< C.stripPrefix "." afterMajor+ (patch, _) <- number =<< C.stripPrefix "." afterMinor+ return (major, minor, patch)+ where+ number bytes = case C.span isDigit bytes of+ (digits, remainder) | not (C.null digits) -> Just (read (C.unpack digits), remainder)+ _ -> Nothing+responseVersion _ = Nothing releaseSocket :: Connection -> IO () releaseSocket = closeConnection
Database/Memcache/Socket.hs view
@@ -67,6 +67,11 @@ } deriving (Eq, Show) -- | Create a connection wrapper around a socket.+--+-- This doesn't check the server's version. Connections that+-- "Database.Memcache.Server" makes are checked, and refuse a server older than+-- memcached 1.6.10; a connection made here directly is not, so check the+-- server's version before sending it 'storeRequest' or 'modifyRequest'. newConnection :: N.Socket -> IO Connection newConnection s = Connection s <$> newIORef B.empty @@ -341,6 +346,16 @@ <> bytes " c" <> bytes crlf -- | Encode a storage request.+--+-- Send this only to memcached 1.6.10 or later. Older servers reject it without+-- reading the value, and then run the value's bytes as commands. Connections+-- that "Database.Memcache.Server" makes check the server's version, but a+-- connection made with 'newConnection' directly does not.+--+-- Nor send a value longer than 1 GiB, the largest item memcached can store:+-- memcached mishandles a data length over 2147483645 bytes, and then runs the+-- rest of the value's bytes as commands. The operations in+-- "Database.Memcache.Client" refuse such a value; this encoder doesn't. storeRequest :: StoreMode -> WireKey -> Value -> Flags -> Expiration -> Version -> ByteString storeRequest mode key value flags expiry version = render (B.length (wireKey key) + B.length value) $ bytes cmdMetaSet <> bytes " " <> bytes (wireKey key) <> bytes " " <> intDec (B.length value)@@ -361,6 +376,16 @@ <> bytes crlf -- | Encode an append or prepend request.+--+-- Send this only to memcached 1.6.10 or later. Older servers reject it without+-- reading the value, and then run the value's bytes as commands. Connections+-- that "Database.Memcache.Server" makes check the server's version, but a+-- connection made with 'newConnection' directly does not.+--+-- Nor send a value longer than 1 GiB, the largest item memcached can store:+-- memcached mishandles a data length over 2147483645 bytes, and then runs the+-- rest of the value's bytes as commands. The operations in+-- "Database.Memcache.Client" refuse such a value; this encoder doesn't. modifyRequest :: StoreMode -> WireKey -> Value -> Version -> ByteString modifyRequest mode key value version = render (B.length (wireKey key) + B.length value) $ bytes cmdMetaSet <> bytes " " <> bytes (wireKey key) <> bytes " " <> intDec (B.length value)
Database/Memcache/Types.hs view
@@ -49,7 +49,9 @@ | ErrKeyNotFound -- | The key already exists. | ErrKeyExists- -- | The value exceeds the server limit.+ -- | The value exceeds the server limit. The client also throws this,+ -- without sending anything, for a value larger than 1 GiB, the largest+ -- item memcached can store. | ErrValueTooLarge -- | The item was not stored. | ErrItemNotStored
README.md view
@@ -15,8 +15,20 @@ connecting to a cluster, consistent hashing is used for routing requests to the appropriate server. -**This library requires memcached 1.6.0 or newer**, the first release with the-meta commands. Against older servers every operation is rejected with `ERROR`.+**This library requires memcached 1.6.10 or newer.** Older servers reject the+meta set command this library stores values with, without reading the value+that follows it, and then run the value's bytes as commands. So each new+connection asks the server for its version before it is used: until that check+passes, the only requests sent are the authentication exchange, when+credentials are set, and `version`. Unless the server reports 1.6.10 or later,+the connection is closed and the operation fails with+`ProtocolError (UnknownOp message)`, where the message quotes the server's+reply, or, if the server answered with an error such as+`CLIENT_ERROR unauthenticated`, with the error that reply gives any request. A+refused version isn't retried, and doesn't mark the server as failed. The+check sees only the server the client connects to: through a proxy, such as+memcached's own `-o proxy_config` mode, it sees the proxy's version, so every+memcached behind a proxy must be 1.6.10 or newer too. Authentication uses a memcached auth file with `-Y authfile`, an experimental ASCII-protocol auth mode added in memcached 1.5.15; the old binary protocol@@ -117,6 +129,9 @@ mc <- EC.newClient M.def (60 * 1000000) endpoint ``` +Discovery connects to the configuration endpoint like any other server, so it+fails for a cluster running a memcached older than 1.6.10.+ ## Licensing This library is BSD-licensed.@@ -188,7 +203,7 @@ - `spec` holds the unit and property tests, which run against an in-process mock server and need nothing installed. - `integration` starts its own `memcached` servers on free local ports and- tests against them, so it needs memcached 1.6 or newer on your `PATH`.+ tests against them, so it needs memcached 1.6.10 or newer on your `PATH`. Without it, the suite reports a single pending test instead of failing. memcached refuses to run as root without `-u`, so run the suite as an ordinary user.
memcache.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: memcache-version: 0.4.2.0+version: 0.4.2.1 synopsis: A memcached client library. description: A client library for a Memcached cluster. Memcached is an in-memory key-value@@ -8,8 +8,11 @@ group of Memcached servers and perform out-of-band caching for things like SQL results, rendered pages, or third-party APIs. - It uses the Memcached Meta Text protocol and requires memcached 1.6.0 or- newer. Username/password authentication is supported through memcached's+ It uses the Memcached Meta Text protocol and requires memcached 1.6.10 or+ newer: each new connection checks the server's version, and refuses an+ older server. Until the check passes, the only requests sent are the+ authentication exchange, when credentials are set, and @version@.+ Username/password authentication is supported through memcached's @-Y authfile@ mode; the binary protocol and SASL authentication are no longer supported. It supports connecting to a single, or a cluster of Memcached servers. When connecting to a cluster, consistent hashing is used for routing
test/MockServer.hs view
@@ -5,7 +5,8 @@ -- Each mock listens on its own ephemeral port on 127.0.0.1, so tests can run -- several at once and never clash with a real memcached on the default port. module MockServer (- MockResponse(..), MockServer(..), withMockServer, mockServerSpec+ MockResponse(..), MockServer(..), withMockServer, withMockMemcached,+ mockServerSpec ) where import Control.Concurrent@@ -49,7 +50,18 @@ -- in order, one per request, across however many connections the client -- makes; once they run out it closes the connection and stops listening. withMockServer :: [MockResponse] -> (MockServer -> IO a) -> IO a-withMockServer responses action = do+withMockServer = runMockServer Nothing++-- | Like 'withMockServer', but the mock reports itself as memcached of the+-- given version, such as @"1.6.24"@. The client asks for the server's version+-- on every new connection before sending anything else, so the mock answers+-- the first request on each connection with @VERSION@ and that version,+-- without using up a response. That request is still recorded.+withMockMemcached :: B.ByteString -> [MockResponse] -> (MockServer -> IO a) -> IO a+withMockMemcached version = runMockServer (Just ("VERSION " <> version <> "\r\n"))++runMockServer :: Maybe B.ByteString -> [MockResponse] -> (MockServer -> IO a) -> IO a+runMockServer greeting responses action = do ready <- newEmptyMVar received <- newIORef [] bracket@@ -79,9 +91,16 @@ pending <- readIORef remaining unless (null pending) $ do (client, _) <- N.accept sock- handle ignore (clientHandler client remaining received)+ handle ignore (greet client received >> clientHandler client remaining received) `finally` N.close client acceptLoop sock remaining received++ -- Answer the version check that starts each connection, if the mock+ -- reports a version.+ greet client received = forM_ greeting $ \reply -> do+ request <- recvReq client+ modifyIORef' received (request :)+ N.sendAll client reply ignore :: SomeException -> IO () ignore = const (return ())
test/Spec.hs view
@@ -16,11 +16,15 @@ import qualified Data.ByteString as B import qualified Data.ByteString.Base64 as Base64 import qualified Data.ByteString.Char8 as C+import Data.ByteString.Unsafe (unsafePackMallocCStringLen) import qualified Network.Socket as N import qualified Network.Socket.ByteString as N import Control.Concurrent (forkIO, threadDelay) import Control.Exception (bracket)+import Control.Monad (forM_, void) import Data.Either (isLeft)+import Data.List (isInfixOf)+import Foreign.Marshal.Alloc (mallocBytes) import System.Timeout (timeout) main :: IO ()@@ -278,7 +282,7 @@ withServerAndClient [MR "VA 5 f1 c11 kone\r\nfirst\r\n", Noop, MR "MN\r\n"] $ \server client -> do Client.getMany client ["one", "two"] `shouldReturn` [("one", ("first", 1, 11))] mockRequests server `shouldReturn`- ["mg one v f c k q\r\n", "mg two v f c k q\r\n", "mn\r\n"]+ ["version\r\n", "mg one v f c k q\r\n", "mg two v f c k q\r\n", "mn\r\n"] it "maps encoded keys back to the caller's key" $ do let encoded = Base64.encode "a b"@@ -294,7 +298,7 @@ it "gets a hit" $ withServerAndClient [MR "VA 3 f2 c7\r\nfoo\r\n"] $ \server client -> do Client.get client "key" `shouldReturn` Just ("foo", 2, 7)- mockRequests server `shouldReturn` ["mg key v f c\r\n"]+ mockRequests server `shouldReturn` ["version\r\n", "mg key v f c\r\n"] it "returns Nothing for a miss" $ withClient [MR "EN\r\n"] $ \client ->@@ -312,7 +316,8 @@ withServerAndClient [MR "VA 3 f2 c7\r\nfoo\r\n", MR "EN\r\n"] $ \server client -> do Client.gat client "key" 60 `shouldReturn` Just ("foo", 2, 7) Client.gat client "key" 60 `shouldReturn` Nothing- mockRequests server `shouldReturn` ["mg key v f c T60\r\n", "mg key v f c T60\r\n"]+ mockRequests server `shouldReturn`+ ["version\r\n", "mg key v f c T60\r\n", "mg key v f c T60\r\n"] it "touches a value" $ withClient [MR "HD c3\r\n", MR "EN\r\n"] $ \client -> do@@ -322,7 +327,7 @@ it "stores and returns the server CAS" $ withServerAndClient [MR "HD c9\r\n"] $ \server client -> do Client.set client "key" "foo" 2 30 `shouldReturn` 9- mockRequests server `shouldReturn` ["ms key 3 F2 T30 MS c\r\nfoo\r\n"]+ mockRequests server `shouldReturn` ["version\r\n", "ms key 3 F2 T30 MS c\r\nfoo\r\n"] it "reports a set the server did not store" $ withClient [MR "NS\r\n"] $ \client ->@@ -345,8 +350,8 @@ Client.cas client "key" "v" 0 0 2 `shouldReturn` Just 3 Client.cas client "key" "v" 0 0 2 `shouldReturn` Nothing Client.cas client "key" "v" 0 0 2 `shouldReturn` Nothing- requests <- mockRequests server- requests `shouldSatisfy` all (== "ms key 1 F0 T0 C2 MS c\r\nv\r\n")+ mockRequests server `shouldReturn`+ "version\r\n" : replicate 3 "ms key 1 F0 T0 C2 MS c\r\nv\r\n" it "deletes a present key" $ withClient [MR "HD\r\n"] $ \client ->@@ -362,8 +367,8 @@ Client.get client "key" `shouldThrow` isBadCommand it "reads version" $- withClient [MR "VERSION 1.6.0\r\n"] $ \client ->- Client.version client `shouldReturn` "1.6.0"+ withClient [MR ("VERSION " <> supportedVersion <> "\r\n")] $ \client ->+ Client.version client `shouldReturn` supportedVersion it "collects stats until END, keeping values that contain spaces" $ withClient [MR "STAT curr_items 2\r\nSTAT rusage_user 0.1 0.2\r\nEND\r\n"] $ \client -> do@@ -374,7 +379,7 @@ withServerAndClient [MR "END\r\n"] $ \server client -> do result <- Client.stats client (Just "cachedump 1 100") map snd result `shouldBe` [[]]- mockRequests server `shouldReturn` ["stats cachedump 1 100\r\n"]+ mockRequests server `shouldReturn` ["version\r\n", "stats cachedump 1 100\r\n"] it "keeps non-STAT stats lines" $ withClient [MR "ITEM key [3 b; 0 s]\r\nEND\r\n"] $ \client -> do@@ -388,7 +393,7 @@ it "returns the new counter value and CAS" $ withServerAndClient [MR "VA 1 c2\r\n0\r\n"] $ \server client -> do Client.decrement client "counter" 10 1 60 0 `shouldReturn` Just (0, 2)- mockRequests server `shouldReturn` ["ma counter D1 MD v c N60 J10\r\n"]+ mockRequests server `shouldReturn` ["version\r\n", "ma counter D1 MD v c N60 J10\r\n"] it "reports arithmetic on a non-numeric value" $ withClient [MR "CLIENT_ERROR cannot increment or decrement non-numeric value\r\n"] $ \client ->@@ -401,12 +406,12 @@ it "prepends to an existing key" $ withServerAndClient [MR "HD c4\r\n"] $ \server client -> do Client.prepend client "key" "pre" 0 `shouldReturn` Just 4- mockRequests server `shouldReturn` ["ms key 3 MP c\r\npre\r\n"]+ mockRequests server `shouldReturn` ["version\r\n", "ms key 3 MP c\r\npre\r\n"] it "flushes" $ withServerAndClient [MR "OK\r\n"] $ \server client -> do Client.flush client (Just 10) `shouldReturn` ()- mockRequests server `shouldReturn` ["flush_all 10\r\n"]+ mockRequests server `shouldReturn` ["version\r\n", "flush_all 10\r\n"] it "reports an oversized value as a status, not a protocol failure" $ withClient [MR "SERVER_ERROR object too large for cache\r\n"] $ \client ->@@ -424,9 +429,46 @@ it "sends quit" $ withServerAndClient [Noop] $ \server client -> do Client.quit client- requests <- waitForRequests server 1- requests `shouldBe` ["quit\r\n"]+ requests <- waitForRequests server 2+ requests `shouldBe` ["version\r\n", "quit\r\n"] + -- The value is one byte over the 1 GiB limit. In the test of each+ -- operation, the client's server is a port that is bound but not listening,+ -- so a client that tried to send the value would throw the refused+ -- connection instead: the check has to come before the client connects.+ describe "oversized values" $ beforeAll overLimitValue $ do+ it "rejects an over-limit value from set without sending anything" $ \value ->+ withRefusingClient $ \client ->+ Client.set client "key" value 0 0 `shouldThrow` (== OpError ErrValueTooLarge)++ it "rejects an over-limit value from add without sending anything" $ \value ->+ withRefusingClient $ \client ->+ Client.add client "key" value 0 0 `shouldThrow` (== OpError ErrValueTooLarge)++ it "rejects an over-limit value from replace without sending anything" $ \value ->+ withRefusingClient $ \client ->+ Client.replace client "key" value 0 0 0 `shouldThrow` (== OpError ErrValueTooLarge)++ it "rejects an over-limit value from cas without sending anything" $ \value ->+ withRefusingClient $ \client ->+ Client.cas client "key" value 0 0 1 `shouldThrow` (== OpError ErrValueTooLarge)++ it "rejects an over-limit value from append without sending anything" $ \value ->+ withRefusingClient $ \client ->+ Client.append client "key" value 0 `shouldThrow` (== OpError ErrValueTooLarge)++ it "rejects an over-limit value from prepend without sending anything" $ \value ->+ withRefusingClient $ \client ->+ Client.prepend client "key" value 0 `shouldThrow` (== OpError ErrValueTooLarge)++ -- This client routes every key to no server, so a value that it lets+ -- through fails with NoServersReady and is never sent. It still rejects+ -- the over-limit value, which shows that it checks values first.+ it "does not reject a value of exactly the limit" $ \value -> do+ client <- Client.newClient [] Client.def { Client.optsGetServerForKey = \_ _ -> return Nothing }+ Client.set client "key" value 0 0 `shouldThrow` (== OpError ErrValueTooLarge)+ Client.set client "key" (B.take valueLimit value) 0 0 `shouldThrow` (== ClientError NoServersReady)+ describe "retries and failover" $ do let fast = Client.def { Client.optsServerTimeout = 300@@ -437,7 +479,9 @@ it "retries after the server drops the connection" $ withServerAndClientOpts fast [CloseConnection, MR "VA 3 f2 c7\r\nfoo\r\n"] $ \server client -> do Client.get client "key" `shouldReturn` Just ("foo", 2, 7)- length <$> mockRequests server `shouldReturn` 2+ -- The retry's new connection checks the version again.+ mockRequests server `shouldReturn`+ ["version\r\n", "mg key v f c\r\n", "version\r\n", "mg key v f c\r\n"] it "retries after an operation times out" $ withServerAndClientOpts fast [DelayMS 350 Noop, MR "VA 3 f2 c7\r\nfoo\r\n"] $ \_ client ->@@ -447,11 +491,18 @@ withServerAndClientOpts fast [CloseConnection, CloseConnection, MR "VA 3 f2 c7\r\nfoo\r\n"] $ \server client -> do -- The default of two retries means two attempts in total. Client.get client "key" `shouldThrow` isUnexpectedEOF- length <$> mockRequests server `shouldReturn` 2+ mockRequests server `shouldReturn`+ ["version\r\n", "mg key v f c\r\n", "version\r\n", "mg key v f c\r\n"] Client.get client "key" `shouldThrow` (== ClientError NoServersReady) threadDelay 400000 Client.get client "key" `shouldReturn` Just ("foo", 2, 7) + it "checks the version again on the connection a retry opens" $+ withMockServer [supported, CloseConnection, MR "VERSION 1.6.9\r\n", MR "VA 3 f2 c7\r\nfoo\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] fast+ Client.get client "key" `shouldThrow` refused "VERSION 1.6.9"+ mockRequests server `shouldReturn` ["version\r\n", "mg key v f c\r\n", "version\r\n"]+ describe "authenticate" $ do it "accepts credentials once the server confirms auth-file mode" $ bracket (N.socketPair N.AF_UNIX N.Stream 0) closePair $ \(left, right) -> do@@ -492,13 +543,104 @@ [" ", "\t", "\r", "\n"] it "authenticates each new client connection" $- withMockServer [MR "CLIENT_ERROR unauthenticated\r\n", MR "STORED\r\n", MR "VA 3 f2 c7\r\nfoo\r\n"] $ \server -> do- let spec = (mockServerSpec server) { ssAuth = Auth.Auth "user" "pass" }- client <- Client.newClient [spec] Client.def+ withMockServer (authExchange ++ [supported, MR "VA 3 f2 c7\r\nfoo\r\n"]) $ \server -> do+ client <- Client.newClient [withCredentials server] Client.def Client.get client "key" `shouldReturn` Just ("foo", 2, 7)- mockRequests server `shouldReturn`- ["version\r\n", "set auth 0 0 9\r\nuser pass\r\n", "mg key v f c\r\n"]+ mockRequests server `shouldReturn` authRequests ++ ["version\r\n", "mg key v f c\r\n"] + -- Older servers don't read the data block of the meta set this client+ -- sends, so they run the value's bytes as commands. The client must not+ -- send them anything but its version check (and, with credentials, the+ -- authentication exchange before it). Each mock has a reply left over for+ -- a request that should never come, so that one would be recorded.+ describe "server version check" $ do+ forM_ oldVersions $ \v ->+ it ("refuses memcached " <> C.unpack v <> ", sending it nothing but version") $+ withMockServer [MR ("VERSION " <> v <> "\r\n"), MR "HD c1\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] Client.def+ Client.set client "key" "value" 0 0 `shouldThrow` refused ("VERSION " <> v)+ mockRequests server `shouldReturn` ["version\r\n"]++ -- Every operation that sends a value, not only set, must be refused+ -- before the value reaches the server.+ forM_ valueOps $ \(name, op) ->+ it ("refuses memcached 1.6.9 for " <> name <> ", sending it nothing but version") $+ withMockServer [MR "VERSION 1.6.9\r\n", MR "HD c1\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] Client.def+ op client `shouldThrow` refused "VERSION 1.6.9"+ mockRequests server `shouldReturn` ["version\r\n"]++ forM_ newVersions $ \v ->+ it ("accepts memcached " <> C.unpack v) $+ withMockServer [MR ("VERSION " <> v <> "\r\n"), MR "HD c9\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] Client.def+ Client.set client "key" "value" 0 0 `shouldReturn` 9+ mockRequests server `shouldReturn` ["version\r\n", "ms key 5 F0 T0 MS c\r\nvalue\r\n"]++ forM_ unreadableReplies $ \reply ->+ it ("refuses a server that answers version with " <> show reply) $+ withMockServer [MR (reply <> "\r\n"), MR "HD c1\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] Client.def+ Client.set client "key" "value" 0 0 `shouldThrow` refused reply+ mockRequests server `shouldReturn` ["version\r\n"]++ -- Such as a server in auth-file mode, when the client has no credentials.+ it "throws a CLIENT_ERROR reply to version as it would for any request" $+ withMockServer [MR "CLIENT_ERROR unauthenticated\r\n", MR "HD c1\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] Client.def+ Client.set client "key" "value" 0 0+ `shouldThrow` (== ProtocolError (E.BadCommand "unauthenticated"))+ mockRequests server `shouldReturn` ["version\r\n"]++ it "throws an ERROR reply to version as it would for any request" $+ withMockServer [MR "ERROR\r\n", MR "HD c1\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] Client.def+ Client.set client "key" "value" 0 0 `shouldThrow` (== ProtocolError (UnknownOp "ERROR"))+ mockRequests server `shouldReturn` ["version\r\n"]++ -- A server failure is retried, as it would be for any request, but each+ -- attempt still sends nothing but version.+ it "throws a SERVER_ERROR reply to version as it would for any request" $+ withMockServer [MR "SERVER_ERROR busy\r\n", MR "SERVER_ERROR busy\r\n", MR "HD c1\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] Client.def { Client.optsFailRetryDelay = 10 }+ Client.set client "key" "value" 0 0 `shouldThrow` (== ProtocolError (E.ServerError "busy"))+ mockRequests server `shouldReturn` ["version\r\n", "version\r\n"]++ -- The server's version won't change on a retry: a refusal is a client+ -- fault, so the operation fails at once and the server stays alive.+ it "neither retries a refused server nor marks it dead" $+ withMockServer [MR "VERSION 1.6.9\r\n", MR "VERSION 1.6.9\r\n", MR "HD c1\r\n", MR "HD c1\r\n"] $ \server -> do+ client <- Client.newClient [mockServerSpec server] Client.def+ Client.set client "key" "value" 0 0 `shouldThrow` refused "VERSION 1.6.9"+ mockRequests server `shouldReturn` ["version\r\n"]+ -- A server marked dead would fail with NoServersReady without being+ -- asked again.+ Client.get client "key" `shouldThrow` refused "VERSION 1.6.9"+ mockRequests server `shouldReturn` ["version\r\n", "version\r\n"]++ describe "with credentials" $ do+ forM_ oldVersions $ \v ->+ it ("asks again after authenticating, and refuses memcached " <> C.unpack v) $+ withMockServer (authExchange ++ [MR ("VERSION " <> v <> "\r\n"), MR "HD c1\r\n"]) $ \server -> do+ client <- Client.newClient [withCredentials server] Client.def+ Client.set client "key" "value" 0 0 `shouldThrow` refused ("VERSION " <> v)+ mockRequests server `shouldReturn` authRequests ++ ["version\r\n"]++ forM_ newVersions $ \v ->+ it ("asks again after authenticating, and accepts memcached " <> C.unpack v) $+ withMockServer (authExchange ++ [MR ("VERSION " <> v <> "\r\n"), MR "HD c9\r\n"]) $ \server -> do+ client <- Client.newClient [withCredentials server] Client.def+ Client.set client "key" "value" 0 0 `shouldReturn` 9+ mockRequests server `shouldReturn`+ authRequests ++ ["version\r\n", "ms key 5 F0 T0 MS c\r\nvalue\r\n"]++ forM_ unreadableReplies $ \reply ->+ it ("refuses a server that answers version with " <> show reply <> " after authenticating") $+ withMockServer (authExchange ++ [MR (reply <> "\r\n"), MR "HD c1\r\n"]) $ \server -> do+ client <- Client.newClient [withCredentials server] Client.def+ Client.set client "key" "value" 0 0 `shouldThrow` refused reply+ mockRequests server `shouldReturn` authRequests ++ ["version\r\n"]+ describe "server URIs" $ do it "parses host and port" $ parseServerSpec "memcached://cache.example:1234" `shouldBe`@@ -526,8 +668,8 @@ describe "clusters" $ do it "fails over to another server once one is marked dead" $- withMockServer [MR "VA 1 f0 c1\r\nA\r\n", CloseConnection, CloseConnection] $ \a ->- withMockServer [MR "VA 1 f0 c1\r\nB\r\n", CloseConnection, CloseConnection] $ \b -> do+ withMockMemcached supportedVersion [MR "VA 1 f0 c1\r\nA\r\n", CloseConnection, CloseConnection] $ \a ->+ withMockMemcached supportedVersion [MR "VA 1 f0 c1\r\nB\r\n", CloseConnection, CloseConnection] $ \b -> do client <- Client.newClient [mockServerSpec a, mockServerSpec b] fastFailover first <- Client.get client "key" -- The server that answered now drops two connections in a row, which@@ -538,7 +680,7 @@ map (fmap fst3) [first, second] `shouldSatisfy` (`elem` [[Just "A", Just "B"], [Just "B", Just "A"]]) it "throws NoServersReady once every server is dead" $- withMockServer [CloseConnection, CloseConnection] $ \a -> do+ withMockMemcached supportedVersion [CloseConnection, CloseConnection] $ \a -> do client <- Client.newClient [mockServerSpec a] fastFailover Client.get client "key" `shouldThrow` isUnexpectedEOF Client.get client "key" `shouldThrow` (== ClientError NoServersReady)@@ -546,49 +688,56 @@ Client.version client `shouldThrow` (== ClientError NoServersReady) it "flushes every server" $- withMockServer [MR "OK\r\n"] $ \a ->- withMockServer [MR "OK\r\n"] $ \b -> do+ withMockMemcached supportedVersion [MR "OK\r\n"] $ \a ->+ withMockMemcached supportedVersion [MR "OK\r\n"] $ \b -> do client <- Client.newClient [mockServerSpec a, mockServerSpec b] Client.def Client.flush client Nothing- mockRequests a `shouldReturn` ["flush_all\r\n"]- mockRequests b `shouldReturn` ["flush_all\r\n"]+ mockRequests a `shouldReturn` ["version\r\n", "flush_all\r\n"]+ mockRequests b `shouldReturn` ["version\r\n", "flush_all\r\n"] it "collects stats from every server" $- withMockServer [MR "STAT pid 1\r\nEND\r\n"] $ \a ->- withMockServer [MR "STAT pid 2\r\nEND\r\n"] $ \b -> do+ withMockMemcached supportedVersion [MR "STAT pid 1\r\nEND\r\n"] $ \a ->+ withMockMemcached supportedVersion [MR "STAT pid 2\r\nEND\r\n"] $ \b -> do client <- Client.newClient [mockServerSpec a, mockServerSpec b] Client.def results <- Client.stats client Nothing concatMap snd results `shouldMatchList` [("pid", "1"), ("pid", "2")] it "asks a single server for the version" $- withMockServer [MR "VERSION 1.6.1\r\n"] $ \a ->- withMockServer [MR "VERSION 1.6.1\r\n"] $ \b -> do+ withMockMemcached supportedVersion [supported] $ \a ->+ withMockMemcached supportedVersion [supported] $ \b -> do client <- Client.newClient [mockServerSpec a, mockServerSpec b] Client.def- Client.version client `shouldReturn` "1.6.1"- requests <- (++) <$> mockRequests a <*> mockRequests b- requests `shouldBe` ["version\r\n"]+ Client.version client `shouldReturn` supportedVersion+ -- The connection's own version check, then the operation.+ requests <- sequence [mockRequests a, mockRequests b]+ requests `shouldMatchList` [["version\r\n", "version\r\n"], []] describe "ElastiCache discovery" $ do it "rejects an endpoint that is not a configuration endpoint" $ isLeft (EC.parseConfigurationEndpoint "memcached://cache.example:11211") `shouldBe` True it "discovers nodes with the config command" $- withMockServer [MR "VA 3 f0 c1\r\nfoo\r\n"] $ \node -> do+ withMockMemcached supportedVersion [MR "VA 3 f0 c1\r\nfoo\r\n"] $ \node -> do let config = "CONFIG cluster 0 " <> nodeList node <> "END\r\n"- withMockServer [MR "VERSION 1.6.12\r\n", MR config, Noop] $ \cfg -> do- client <- EC.newClient Client.def hour (endpoint cfg)+ withMockMemcached "1.6.12" [MR "VERSION 1.6.12\r\n", MR config, Noop] $ \cfg -> do+ client <- inTime (EC.newClient Client.def hour (endpoint cfg)) Client.get client "key" `shouldReturn` Just ("foo", 0, 1)- waitForRequests cfg 3 `shouldReturn`- ["version\r\n", "config get cluster\r\n", "quit\r\n"]+ -- The connection's own version check, then discovery's.+ waitForRequests cfg 4 `shouldReturn`+ ["version\r\n", "version\r\n", "config get cluster\r\n", "quit\r\n"] - it "discovers nodes from the cluster key on engines before 1.4.14" $+ -- Engines before 1.4.14 keep the node list in a key, which discovery+ -- used to read with a plain get. Like any server older than memcached+ -- 1.6.10, they are now refused before discovery sends anything.+ it "refuses a configuration endpoint older than memcached 1.6.10" $ withMockServer [MR "VA 3 f0 c1\r\nfoo\r\n"] $ \node -> do let value = "VALUE AmazonElastiCache:cluster 0 " <> nodeList node <> "END\r\n" withMockServer [MR "VERSION 1.4.5\r\n", MR value, Noop] $ \cfg -> do- client <- EC.newClient Client.def hour (endpoint cfg)- Client.get client "key" `shouldReturn` Just ("foo", 0, 1)- waitForRequests cfg 3 `shouldReturn`- ["version\r\n", "get AmazonElastiCache:cluster\r\n", "quit\r\n"]+ inTime (EC.newClient Client.def hour (endpoint cfg)) `shouldThrow` refused "VERSION 1.4.5"+ -- Discovery's connection is refused after asking for the version.+ -- Quitting its client then opens another, which the mock answers+ -- with the node list; that is no version either, so it is refused+ -- too.+ mockRequests cfg `shouldReturn` ["version\r\n", "version\r\n"] where recvResponseFrom bytes =@@ -623,7 +772,38 @@ allStats [(_, [ ("curr_items", "2"), ("rusage_user", "0.1 0.2") ])] = True allStats _ = False expectedAuthRequests = "version\r\nset auth 0 0 9\r\nuser pass\r\n"+ -- A server in auth-file mode rejects the client's first version request,+ -- then accepts its credentials.+ authExchange = [MR "CLIENT_ERROR unauthenticated\r\n", MR "STORED\r\n"]+ authRequests = ["version\r\n", "set auth 0 0 9\r\nuser pass\r\n"]+ withCredentials server = (mockServerSpec server) { ssAuth = Auth.Auth "user" "pass" }+ supported = MR ("VERSION " <> supportedVersion <> "\r\n")+ -- Versions as memcached reports them, with Ubuntu's suffix and a+ -- release candidate's; 1.10.0 and 1.6.9 catch comparing them as text.+ oldVersions =+ ["1.6.9", "1.5.22", "1.4.25", "1.6.0", "1.4.25 Ubuntu", "1.6.9-rc1", "0.10.10"]+ newVersions =+ ["1.6.10", "1.6.14", "1.6.24", "1.6.100", "1.7.0", "1.10.0", "2.0.0", "1.6.10-rc1", "1.6.24 Ubuntu"]+ unreadableReplies =+ [ "VERSION", "VERSION 1.6", "VERSION 1.7", "VERSION 1.6.x", "VERSION v1.6.24"+ , "VERSION 1..6.24", "VERSION 1.6.+10", "VERSION -1.6.24", "HD", "END", "HD 1.6.24" ]+ -- The operations that send a value, each storing one that would run as+ -- a command on a server older than 1.6.10.+ valueOps :: [(String, Client.Client -> IO ())]+ valueOps =+ [ ("set", \c -> void $ Client.set c "key" injection 0 0)+ , ("add", \c -> void $ Client.add c "key" injection 0 0)+ , ("replace", \c -> void $ Client.replace c "key" injection 0 0 0)+ , ("cas", \c -> void $ Client.cas c "key" injection 0 0 1)+ , ("append", \c -> void $ Client.append c "key" injection 0)+ , ("prepend", \c -> void $ Client.prepend c "key" injection 0)+ ]+ injection :: B.ByteString+ injection = "set injected 0 0 3\r\nbad" fastFailover = Client.def { Client.optsFailRetryDelay = 10, Client.optsDeadRetryDelay = 60000 }+ -- Discovery has no timeout of its own, so a client that sent the mock+ -- something it has no reply for would wait for one forever.+ inTime action = timeout 2000000 action >>= maybe (fail "timed out") return fst3 (value, _, _) = value hour = 3600 * 1000000 -- The endpoint check only looks for ".cfg" somewhere in the URI, so put it@@ -635,7 +815,8 @@ let body = "1\n127.0.0.1|127.0.0.1|" <> C.pack (show (mockPort node)) <> "\n" in C.pack (show (B.length body)) <> "\r\n" <> body <> "\r\n" --- | Run an action with a client connected to a fresh mock server.+-- | Run an action with a client connected to a fresh mock server, which+-- reports 'supportedVersion' to each connection's version check. withClient :: [MockResponse] -> (Client.Client -> IO a) -> IO a withClient responses action = withServerAndClient responses (const action) @@ -644,9 +825,46 @@ withServerAndClientOpts :: Client.Options -> [MockResponse] -> (MockServer -> Client.Client -> IO a) -> IO a-withServerAndClientOpts opts responses action = withMockServer responses $ \server -> do+withServerAndClientOpts opts responses action = withMockMemcached supportedVersion responses $ \server -> do client <- Client.newClient [mockServerSpec server] opts action server client++-- | The version that mock servers report unless a test says otherwise: one+-- the client accepts.+supportedVersion :: B.ByteString+supportedVersion = "1.6.24"++-- | Whether an exception is the client refusing a server that answered its+-- version check with the given reply: 'UnknownOp', with a message that quotes+-- the reply and names the minimum version.+refused :: B.ByteString -> MemcacheError -> Bool+refused reply (ProtocolError (UnknownOp message)) =+ show reply `isInfixOf` message && "1.6.10 or later" `isInfixOf` message+refused _ _ = False++-- | Run an action with a client whose server is a port on 127.0.0.1 that is+-- bound but not listening, so every attempt to connect to it is refused.+withRefusingClient :: (Client.Client -> IO a) -> IO a+withRefusingClient action =+ bracket (N.socket N.AF_INET N.Stream N.defaultProtocol) N.close $ \sock -> do+ N.bind sock (N.SockAddrInet 0 (N.tupleToHostAddress (127, 0, 0, 1)))+ port <- N.socketPort sock+ client <- Client.newClient [ServerSpec "127.0.0.1" (show port) Auth.NoAuth] Client.def+ action client++-- | The largest value memcached can store, 1 GiB.+valueLimit :: Int+valueLimit = 1024 * 1024 * 1024++-- | A value one byte longer than 'valueLimit'. It is malloc'd outside the GHC+-- heap and never written, and the client reads only its length, so it takes+-- next to no memory, unless malloc fills it, as glibc's does when+-- MALLOC_PERTURB_ is set. It is freed once nothing refers to it.+overLimitValue :: IO B.ByteString+overLimitValue = do+ let len = valueLimit + 1+ ptr <- mallocBytes len+ unsafePackMallocCStringLen (ptr, len) -- | Wait (up to a second) for the mock to have received at least @n@ requests. waitForRequests :: MockServer -> Int -> IO [B.ByteString]