Skip to content

Commit

Permalink
Merge pull request #202 from well-typed/temp
Browse files Browse the repository at this point in the history
Refactor in preparation of synthesized errors
  • Loading branch information
edsko authored Jul 25, 2024
2 parents 2fbb70e + 9381c6c commit 23b2f35
Show file tree
Hide file tree
Showing 52 changed files with 2,088 additions and 1,772 deletions.
17 changes: 14 additions & 3 deletions grapesy.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,21 @@ library
Network.GRPC.Common
Network.GRPC.Common.Binary
Network.GRPC.Common.Compression
Network.GRPC.Common.Headers
Network.GRPC.Common.HTTP2Settings
Network.GRPC.Common.JSON
Network.GRPC.Common.NextElem
Network.GRPC.Common.Protobuf
Network.GRPC.Common.StreamElem
Network.GRPC.Common.StreamType
Network.GRPC.Common.HTTP2Settings
Network.GRPC.Server
Network.GRPC.Server.Binary
Network.GRPC.Server.Protobuf
Network.GRPC.Server.Run
Network.GRPC.Server.StreamType
Network.GRPC.Server.StreamType.Binary
Network.GRPC.Spec
Network.GRPC.Spec.Serialization
other-modules:
Network.GRPC.Client.Call
Network.GRPC.Client.Connection
Expand All @@ -132,7 +134,6 @@ library
Network.GRPC.Server.RequestHandler
Network.GRPC.Server.RequestHandler.API
Network.GRPC.Server.Session
Network.GRPC.Spec.Base64
Network.GRPC.Spec.Call
Network.GRPC.Spec.Compression
Network.GRPC.Spec.CustomMetadata.Map
Expand All @@ -144,7 +145,7 @@ library
Network.GRPC.Spec.Headers.PseudoHeaders
Network.GRPC.Spec.Headers.Request
Network.GRPC.Spec.Headers.Response
Network.GRPC.Spec.LengthPrefixed
Network.GRPC.Spec.MessageMeta
Network.GRPC.Spec.OrcaLoadReport
Network.GRPC.Spec.PercentEncoding
Network.GRPC.Spec.RPC
Expand All @@ -153,6 +154,16 @@ library
Network.GRPC.Spec.RPC.Raw
Network.GRPC.Spec.RPC.StreamType
Network.GRPC.Spec.RPC.Unknown
Network.GRPC.Spec.Serialization.Base64
Network.GRPC.Spec.Serialization.CustomMetadata
Network.GRPC.Spec.Serialization.Headers.Common
Network.GRPC.Spec.Serialization.Headers.PseudoHeaders
Network.GRPC.Spec.Serialization.Headers.Request
Network.GRPC.Spec.Serialization.Headers.Response
Network.GRPC.Spec.Serialization.LengthPrefixed
Network.GRPC.Spec.Serialization.Status
Network.GRPC.Spec.Serialization.Timeout
Network.GRPC.Spec.Serialization.TraceContext
Network.GRPC.Spec.Status
Network.GRPC.Spec.Timeout
Network.GRPC.Spec.TraceContext
Expand Down
7 changes: 3 additions & 4 deletions interop/Interop/Client/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Compression qualified as Compr
import Network.GRPC.Common.Protobuf
import Network.GRPC.Spec

import Interop.Util.Exceptions
import Interop.Util.Messages
Expand Down Expand Up @@ -110,13 +109,13 @@ verifyStreamingOutputs :: forall rpc.
HasCallStack
=> Call rpc
-> (ProperTrailers' -> IO ()) -- ^ Verify trailers
-> [(InboundEnvelope, Output rpc) -> IO ()] -- ^ Verifier per expected output
-> [(InboundMeta, Output rpc) -> IO ()] -- ^ Verifier per expected output
-> IO ()
verifyStreamingOutputs call verifyTrailers = go
where
go :: [(InboundEnvelope, Output rpc) -> IO ()] -> IO ()
go :: [(InboundMeta, Output rpc) -> IO ()] -> IO ()
go verifiers = do
mResp <- recvOutputWithEnvelope call
mResp <- recvOutputWithMeta call
case (mResp, verifiers) of
(NoMoreElems trailers, []) -> verifyTrailers trailers
(StreamElem{}, []) -> assertFailure "Too many outputs"
Expand Down
13 changes: 6 additions & 7 deletions interop/Interop/Client/TestCase/ClientCompressedStreaming.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Interop.Client.TestCase.ClientCompressedStreaming (runTest) where
import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf
import Network.GRPC.Spec

import Interop.Client.Common
import Interop.Client.Connect
Expand All @@ -20,21 +19,21 @@ runTest cmdline =
checkServerSupportsCompressedRequest conn

withRPC conn def (Proxy @StreamingInputCall) $ \call -> do
sendInputWithEnvelope call $ StreamElem compressed
sendInputWithEnvelope call $ FinalElem uncompressed NoMetadata
sendInputWithMeta call $ StreamElem compressed
sendInputWithMeta call $ FinalElem uncompressed NoMetadata

(resp, _metadata) <- recvFinalOutput call
assertEqual 73086 $ resp ^. #aggregatedPayloadSize
where
-- Expect compressed, and /is/ compressed
compressed :: (OutboundEnvelope, Proto StreamingInputCallRequest)
compressed :: (OutboundMeta, Proto StreamingInputCallRequest)
compressed = (
def { outboundEnableCompression = True }
, mkStreamingInputCallRequest True 27182
)

-- Expect uncompressed, and /is/ uncompressed
uncompressed :: (OutboundEnvelope, Proto StreamingInputCallRequest)
uncompressed :: (OutboundMeta, Proto StreamingInputCallRequest)
uncompressed = (
def { outboundEnableCompression = False }
, mkStreamingInputCallRequest False 45904
Expand All @@ -51,11 +50,11 @@ runTest cmdline =
checkServerSupportsCompressedRequest :: Connection -> IO ()
checkServerSupportsCompressedRequest conn =
withRPC conn def (Proxy @StreamingInputCall) $ \call -> do
sendInputWithEnvelope call $ FinalElem featureProbe NoMetadata
sendInputWithMeta call $ FinalElem featureProbe NoMetadata
expectInvalidArgument $ recvFinalOutput call
where
-- Expect compressed, but is /not/ actually compressed
featureProbe :: (OutboundEnvelope, Proto StreamingInputCallRequest)
featureProbe :: (OutboundMeta, Proto StreamingInputCallRequest)
featureProbe = (
def { outboundEnableCompression = False }
, mkStreamingInputCallRequest True 27182
Expand Down
14 changes: 7 additions & 7 deletions interop/Interop/Client/TestCase/ClientCompressedUnary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Interop.Client.TestCase.ClientCompressedUnary (runTest) where

import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Spec
import Network.GRPC.Common.Protobuf

import Interop.Client.Common
import Interop.Client.Connect
Expand All @@ -21,25 +21,25 @@ runTest cmdline =

-- 2. Call UnaryCall with the compressed message
withRPC conn def (Proxy @UnaryCall) $ \call -> do
sendInputWithEnvelope call $ FinalElem compressed NoMetadata
sendInputWithMeta call $ FinalElem compressed NoMetadata
(resp, _metadata) <- recvFinalOutput call
verifySimpleResponse resp

-- 3. Call UnaryCall with the uncompressed message
withRPC conn def (Proxy @UnaryCall) $ \call -> do
sendInputWithEnvelope call $ FinalElem uncompressed NoMetadata
sendInputWithMeta call $ FinalElem uncompressed NoMetadata
(resp, _metadata) <- recvFinalOutput call
verifySimpleResponse resp
where
-- Expect compressed, and /is/ compressed
compressed :: (OutboundEnvelope, Proto SimpleRequest)
compressed :: (OutboundMeta, Proto SimpleRequest)
compressed = (
def { outboundEnableCompression = True }
, mkSimpleRequest True
)

-- Expect uncompressed, and /is/ uncompressed
uncompressed :: (OutboundEnvelope, Proto SimpleRequest)
uncompressed :: (OutboundMeta, Proto SimpleRequest)
uncompressed = (
def { outboundEnableCompression = False }
, mkSimpleRequest False
Expand All @@ -51,11 +51,11 @@ runTest cmdline =
checkServerSupportsCompressedRequest :: Connection -> IO ()
checkServerSupportsCompressedRequest conn =
withRPC conn def (Proxy @UnaryCall) $ \call -> do
sendInputWithEnvelope call $ FinalElem featureProbe NoMetadata
sendInputWithMeta call $ FinalElem featureProbe NoMetadata
expectInvalidArgument $ recvFinalOutput call
where
-- Expect compressed, but is /not/ actually compressed
featureProbe :: (OutboundEnvelope, Proto SimpleRequest)
featureProbe :: (OutboundMeta, Proto SimpleRequest)
featureProbe = (
def { outboundEnableCompression = False }
, mkSimpleRequest True
Expand Down
9 changes: 4 additions & 5 deletions interop/Interop/Client/TestCase/EmptyUnary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf
import Network.GRPC.Common.StreamElem qualified as StreamElem
import Network.GRPC.Spec

import Interop.Client.Connect
import Interop.Cmdline
Expand All @@ -20,18 +19,18 @@ runTest cmdline =
withConnection def (testServer cmdline) $ \conn ->
withRPC conn def (Proxy @EmptyCall) $ \call -> do
sendFinalInput call empty
streamElem :: StreamElem ProperTrailers' (InboundEnvelope, Proto Empty)
<- recvOutputWithEnvelope call
streamElem :: StreamElem ProperTrailers' (InboundMeta, Proto Empty)
<- recvOutputWithMeta call

-- The test description asks us to also verify the size of the /outgoing/
-- message if possible. This information is not readily available in
-- @grapesy@, but we will test it implicitly when running the @grapesy@
-- interop client against the @grapesy@ interop server.

case StreamElem.value streamElem of
Just (envelope, resp) -> do
Just (meta, resp) -> do
assertEqual empty $ resp
assertEqual 0 $ inboundUncompressedSize envelope
assertEqual 0 $ inboundUncompressedSize meta
Nothing ->
assertFailure "Expected response"
where
Expand Down
5 changes: 2 additions & 3 deletions interop/Interop/Client/TestCase/ServerCompressedStreaming.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Data.Maybe (isJust)

import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Spec

import Interop.Client.Common
import Interop.Client.Connect
Expand All @@ -20,8 +19,8 @@ runTest cmdline = do
withRPC conn def (Proxy @StreamingOutputCall) $ \call -> do
sendFinalInput call $ mkStreamingOutputCallRequest expected Nothing
verifyStreamingOutputs call (\_ -> return ()) $ [
\(envelope, resp) -> do
assertEqual compressed $ isJust (inboundCompressedSize envelope)
\(meta, resp) -> do
assertEqual compressed $ isJust (inboundCompressedSize meta)
verifyStreamingOutputCallResponse sz resp
| (compressed, sz) <- expected
]
Expand Down
17 changes: 8 additions & 9 deletions interop/Interop/Client/TestCase/ServerCompressedUnary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf
import Network.GRPC.Common.StreamElem qualified as StreamElem
import Network.GRPC.Spec

import Interop.Client.Common
import Interop.Client.Connect
Expand All @@ -21,18 +20,18 @@ runTest :: Cmdline -> IO ()
runTest cmdline =
withConnection def (testServer cmdline) $ \conn -> do
withRPC conn def (Proxy @UnaryCall) $ \call -> do
sendInputWithEnvelope call $ FinalElem (request True) NoMetadata
resp <- recvOutputWithEnvelope call
sendInputWithMeta call $ FinalElem (request True) NoMetadata
resp <- recvOutputWithMeta call
verifyResponse True (StreamElem.value resp)

withRPC conn def (Proxy @UnaryCall) $ \call -> do
sendInputWithEnvelope call $ FinalElem (request False) NoMetadata
resp <- recvOutputWithEnvelope call
sendInputWithMeta call $ FinalElem (request False) NoMetadata
resp <- recvOutputWithMeta call
verifyResponse False (StreamElem.value resp)
where
-- To keep the test simple, we disable /outbound/ compression
-- (this test is testing /inbound/ compression)
request :: Bool -> (OutboundEnvelope, Proto SimpleRequest)
request :: Bool -> (OutboundMeta, Proto SimpleRequest)
request expectCompressed = (
def { outboundEnableCompression = False }
, mkSimpleRequest False
Expand All @@ -41,10 +40,10 @@ runTest cmdline =

verifyResponse ::
HasCallStack
=> Bool -> Maybe (InboundEnvelope, Proto SimpleResponse) -> IO ()
=> Bool -> Maybe (InboundMeta, Proto SimpleResponse) -> IO ()
verifyResponse _expectCompressed Nothing =
assertFailure "Expected response"
verifyResponse expectCompressed (Just (envelope, resp)) = do
assertEqual expectCompressed $ isJust (inboundCompressedSize envelope)
verifyResponse expectCompressed (Just (meta, resp)) = do
assertEqual expectCompressed $ isJust (inboundCompressedSize meta)
verifySimpleResponse resp

2 changes: 1 addition & 1 deletion interop/Interop/Client/TestCase/ServerStreaming.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ runTest cmdline =
withRPC conn def (Proxy @StreamingOutputCall) $ \call -> do
sendFinalInput call $ mkStreamingOutputCallRequest expected Nothing
verifyStreamingOutputs call (\_ -> return ()) $ [
\(_envelope, resp) -> verifyStreamingOutputCallResponse sz resp
\(_meta, resp) -> verifyStreamingOutputCallResponse sz resp
| (_compressed, sz) <- expected
]
where
Expand Down
4 changes: 2 additions & 2 deletions interop/Interop/Client/TestCase/SpecialStatusMessage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Data.Text (Text)
import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf
import Network.GRPC.Spec
import Network.GRPC.Spec.Serialization (buildGrpcStatus)

import Interop.Client.Connect
import Interop.Cmdline
Expand All @@ -30,7 +30,7 @@ runTest cmdline = do
echoStatus :: Proto EchoStatus
echoStatus =
defMessage
& #code .~ fromIntegral (fromGrpcStatus $ GrpcError GrpcUnknown)
& #code .~ fromIntegral (buildGrpcStatus $ GrpcError GrpcUnknown)
& #message .~ statusMessage

statusMessage :: Text
Expand Down
4 changes: 2 additions & 2 deletions interop/Interop/Client/TestCase/StatusCodeAndMessage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Data.Text (Text)
import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf
import Network.GRPC.Spec
import Network.GRPC.Spec.Serialization (buildGrpcStatus)

import Interop.Client.Connect
import Interop.Cmdline
Expand Down Expand Up @@ -39,7 +39,7 @@ runTest cmdline = do
echoStatus :: Proto EchoStatus
echoStatus =
defMessage
& #code .~ fromIntegral (fromGrpcStatus $ GrpcError GrpcUnknown)
& #code .~ fromIntegral (buildGrpcStatus $ GrpcError GrpcUnknown)
& #message .~ statusMessage

statusMessage :: Text
Expand Down
10 changes: 5 additions & 5 deletions interop/Interop/Server/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Control.Exception
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf
import Network.GRPC.Server
import Network.GRPC.Spec
import Network.GRPC.Spec.Serialization (parseGrpcStatus)

import Interop.Util.Exceptions

Expand Down Expand Up @@ -54,7 +54,7 @@ constructResponseMetadata call = do
-- See <https://github.com/grpc/grpc/blob/master/doc/interop-test-descriptions.md#status_code_and_message>
echoStatus :: Proto EchoStatus -> IO ()
echoStatus status =
case toGrpcStatus code of
case parseGrpcStatus code of
Just GrpcOk ->
return ()
Just (GrpcError err) ->
Expand All @@ -69,9 +69,9 @@ echoStatus status =
code :: Word
code = fromIntegral $ status ^. #code

checkInboundCompression :: Bool -> InboundEnvelope -> IO ()
checkInboundCompression expectCompressed envelope =
case (expectCompressed, inboundCompressedSize envelope) of
checkInboundCompression :: Bool -> InboundMeta -> IO ()
checkInboundCompression expectCompressed meta =
case (expectCompressed, inboundCompressedSize meta) of
(True, Just _) ->
return ()
(False, Nothing) ->
Expand Down
9 changes: 4 additions & 5 deletions interop/Interop/Server/TestService/StreamingInputCall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Data.ByteString qualified as BS.Strict
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf
import Network.GRPC.Server
import Network.GRPC.Spec

import Interop.Server.Common

Expand All @@ -29,15 +28,15 @@ handle call = do
-- Returns the sum of all request payload bodies received.
loop :: Int -> IO Int
loop !acc = do
streamElem <- recvInputWithEnvelope call
streamElem <- recvInputWithMeta call
case streamElem of
StreamElem r -> handleRequest r >>= \sz -> loop (acc + sz)
FinalElem r _ -> handleRequest r >>= \sz -> return $ acc + sz
NoMoreElems _ -> return acc

handleRequest :: (InboundEnvelope, Proto StreamingInputCallRequest) -> IO Int
handleRequest (envelope, request) = do
checkInboundCompression expectCompressed envelope
handleRequest :: (InboundMeta, Proto StreamingInputCallRequest) -> IO Int
handleRequest (meta, request) = do
checkInboundCompression expectCompressed meta
return $ BS.Strict.length (request ^. #payload ^. #body)
where
expectCompressed :: Bool
Expand Down
Loading

0 comments on commit 23b2f35

Please sign in to comment.