Expose GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH channel arg (#35)

* Expose the max receive message length channel argument + unittest

* Remove unused `MultiWayIf` ext
This commit is contained in:
intractable 2017-10-20 16:39:26 -05:00 committed by GitHub
parent 192bea70e8
commit 3aa835a6f2
4 changed files with 49 additions and 4 deletions

View file

@ -438,6 +438,8 @@ char* translate_arg_key(enum supported_arg_key key){
return GRPC_ARG_PRIMARY_USER_AGENT_STRING; return GRPC_ARG_PRIMARY_USER_AGENT_STRING;
case user_agent_suffix_key: case user_agent_suffix_key:
return GRPC_ARG_SECONDARY_USER_AGENT_STRING; return GRPC_ARG_SECONDARY_USER_AGENT_STRING;
case max_receive_message_length_key:
return GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH;
default: default:
return "unknown_arg_key"; return "unknown_arg_key";
} }

View file

@ -154,7 +154,8 @@ enum supported_arg_key {
compression_algorithm_key = 0, compression_algorithm_key = 0,
compression_level_key, compression_level_key,
user_agent_prefix_key, user_agent_prefix_key,
user_agent_suffix_key user_agent_suffix_key,
max_receive_message_length_key,
}; };
grpc_arg* create_arg_array(size_t n); grpc_arg* create_arg_array(size_t n);

View file

@ -6,6 +6,7 @@ import Control.Exception
import Control.Monad import Control.Monad
import Foreign.Marshal.Alloc (malloc, free) import Foreign.Marshal.Alloc (malloc, free)
import Foreign.Storable import Foreign.Storable
import Numeric.Natural
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include <grpc/status.h> #include <grpc/status.h>
@ -50,6 +51,7 @@ data Arg = CompressionAlgArg CompressionAlgorithm
| CompressionLevelArg CompressionLevel | CompressionLevelArg CompressionLevel
| UserAgentPrefix String | UserAgentPrefix String
| UserAgentSuffix String | UserAgentSuffix String
| MaxReceiveMessageLength Natural
deriving (Show, Eq) deriving (Show, Eq)
{#fun create_string_arg as ^ {`GrpcArg', `Int', `ArgKey', `String'} -> `()'#} {#fun create_string_arg as ^ {`GrpcArg', `Int', `ArgKey', `String'} -> `()'#}
@ -67,6 +69,9 @@ createArg array (UserAgentPrefix prefix) i =
createStringArg array i UserAgentPrefixKey prefix createStringArg array i UserAgentPrefixKey prefix
createArg array (UserAgentSuffix suffix) i = createArg array (UserAgentSuffix suffix) i =
createStringArg array i UserAgentSuffixKey suffix createStringArg array i UserAgentSuffixKey suffix
createArg array (MaxReceiveMessageLength n) i =
createIntArg array i MaxReceiveMessageLengthKey $
fromIntegral (min n (fromIntegral (maxBound :: Int)))
createChannelArgs :: [Arg] -> IO GrpcChannelArgs createChannelArgs :: [Arg] -> IO GrpcChannelArgs
createChannelArgs args = do createChannelArgs args = do

View file

@ -57,6 +57,7 @@ lowLevelTests = testGroup "Unit tests of low-level Haskell library"
, testCustomUserAgent , testCustomUserAgent
, testClientCompression , testClientCompression
, testClientServerCompression , testClientServerCompression
, testClientMaxReceiveMessageLengthChannelArg
, testClientStreaming , testClientStreaming
, testClientStreamingUnregistered , testClientStreamingUnregistered
, testServerStreaming , testServerStreaming
@ -766,6 +767,42 @@ testClientServerCompressionLvl =
return ("hello", dummyMeta, StatusOk, StatusDetails "") return ("hello", dummyMeta, StatusOk, StatusDetails "")
return () return ()
testClientMaxReceiveMessageLengthChannelArg :: TestTree
testClientMaxReceiveMessageLengthChannelArg = do
testGroup "max receive message length channel arg (client channel)"
[ csTest' "payload size < small bound succeeds" shouldSucceed server
, csTest' "payload size > small bound fails" shouldFail server
]
where
-- The server always sends a 4-byte payload
pay = "four"
server = TestServer (ServerConfig "localhost" 50051 ["/foo"] [] [] [] [] Nothing) $ \s -> do
let rm = head (normalMethods s)
void $ serverHandleNormalCall s rm mempty $ \sc -> do
payload sc @?= pay
pure (pay, mempty, StatusOk, StatusDetails "")
clientMax n k = TestClient conf $ \c -> do
rm <- clientRegisterMethodNormal c "/foo"
clientRequest c rm 1 pay mempty >>= k
where
conf = ClientConfig "localhost" 50051 [MaxReceiveMessageLength n] Nothing
-- Expect success when the max recv payload size is set to 4 bytes, and we
-- are sent 4.
shouldSucceed = clientMax 4 $ checkReqRslt $ \NormalRequestResult{..} -> do
rspCode @?= StatusOk
rspBody @?= pay
details @?= ""
-- Expect failure when the max recv payload size is set to 3 bytes, and we
-- are sent 4.
shouldFail = clientMax 3 $ \case
Left (GRPCIOBadStatusCode StatusInvalidArgument _)
-> pure ()
rsp
-> clientFail ("Expected failure response, but got: " ++ show rsp)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Utilities and helpers -- Utilities and helpers