Preliminary streaming mode support (client streaming, server streaming, bidirectional) (#37)
* Tweak runOps param order, inline common op sequences, clean up serverHandleNormalCall
* More ops sequence inlining for clarity, experimenting with Managed
* Checkpoint: preliminary support for all streaming modes; much cleanup/refactoring and api design still needed
* Use mempty for default StatusDetails; tweak bad status matching mechanism
* Preliminary user-facing, server-streaming, low-level api and test
* renaming wibbles
* Preliminary user-facing, client-streaming, low-level api and test
* Move sendMsgs comb to Network.GRPC.LowLevel.Op; misc cleanup/DCR
* Modify bidi streaming to omit request payload
* Add transformers dep
* Preliminary user-facing low-level bidirectional streaming api and test
* Fix missing peek import
* Remove TimeoutSeconds params on streaming mode functions
* Fix serverHandleNormalCall rebase wart
* Fix rebase warts; minor hlint fixes and wibbles
* Post-rebase tweaks to optional payload use in serverRequestCall (i.e., now respects payloadHandling again)
* Cleanup/refactor serverRequestCall
* Fix comment
* Change ServerRWHandler type so that handler does not have to invoke a finalizer
* Change ServerReaderHandler type so that handler does not have to invoke a finalizer
* Simplify serverWriter interface and ServerWriterHandler structure
* Simplify serverRW (get rid of exec param), improve bidi streaming tests
* Use ExceptT in serverRW impl
* Change ServerRWHandler type to pass recv/send operations.
* Renaming
* Define ClientRWHandler, pass recv/send ops
* wibbles
* Use ExceptT in clientRW impl
* Add DataKinded phantom typing to RegisteredMethod; misc cleanup
* Simplify sendMsgs interface; add SingleSend type and related helpers
* Rename SingleSend to SendSingle, use ExceptT to clean up {client,server}Writer and sendMsgs
* More ExceptT cleanup in clientWriter
* Factor out reusable bits of clientWriter
* Shrink ServerReaderHandler
* Delete stale comments
* Use common machinery for all streaming modes; make handler types more consistent
* wibbles
2016-07-06 15:59:38 +02:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2016-06-13 22:51:53 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
Preliminary streaming mode support (client streaming, server streaming, bidirectional) (#37)
* Tweak runOps param order, inline common op sequences, clean up serverHandleNormalCall
* More ops sequence inlining for clarity, experimenting with Managed
* Checkpoint: preliminary support for all streaming modes; much cleanup/refactoring and api design still needed
* Use mempty for default StatusDetails; tweak bad status matching mechanism
* Preliminary user-facing, server-streaming, low-level api and test
* renaming wibbles
* Preliminary user-facing, client-streaming, low-level api and test
* Move sendMsgs comb to Network.GRPC.LowLevel.Op; misc cleanup/DCR
* Modify bidi streaming to omit request payload
* Add transformers dep
* Preliminary user-facing low-level bidirectional streaming api and test
* Fix missing peek import
* Remove TimeoutSeconds params on streaming mode functions
* Fix serverHandleNormalCall rebase wart
* Fix rebase warts; minor hlint fixes and wibbles
* Post-rebase tweaks to optional payload use in serverRequestCall (i.e., now respects payloadHandling again)
* Cleanup/refactor serverRequestCall
* Fix comment
* Change ServerRWHandler type so that handler does not have to invoke a finalizer
* Change ServerReaderHandler type so that handler does not have to invoke a finalizer
* Simplify serverWriter interface and ServerWriterHandler structure
* Simplify serverRW (get rid of exec param), improve bidi streaming tests
* Use ExceptT in serverRW impl
* Change ServerRWHandler type to pass recv/send operations.
* Renaming
* Define ClientRWHandler, pass recv/send ops
* wibbles
* Use ExceptT in clientRW impl
* Add DataKinded phantom typing to RegisteredMethod; misc cleanup
* Simplify sendMsgs interface; add SingleSend type and related helpers
* Rename SingleSend to SendSingle, use ExceptT to clean up {client,server}Writer and sendMsgs
* More ExceptT cleanup in clientWriter
* Factor out reusable bits of clientWriter
* Shrink ServerReaderHandler
* Delete stale comments
* Use common machinery for all streaming modes; make handler types more consistent
* wibbles
2016-07-06 15:59:38 +02:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2016-06-13 22:51:53 +02:00
|
|
|
|
|
|
|
module LowLevelTests.Op where
|
|
|
|
|
|
|
|
import Control.Concurrent (threadDelay)
|
2016-06-23 23:35:11 +02:00
|
|
|
import Data.ByteString (isPrefixOf)
|
2016-06-13 22:51:53 +02:00
|
|
|
import Foreign.Storable (peek)
|
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.HUnit as HU (testCase, (@?=),
|
|
|
|
assertBool)
|
|
|
|
|
|
|
|
import Network.GRPC.LowLevel
|
|
|
|
import Network.GRPC.LowLevel.Call
|
|
|
|
import Network.GRPC.LowLevel.Client
|
|
|
|
import Network.GRPC.LowLevel.Server
|
|
|
|
import Network.GRPC.LowLevel.Op
|
|
|
|
import Network.GRPC.LowLevel.CompletionQueue
|
|
|
|
|
|
|
|
lowLevelOpTests :: TestTree
|
|
|
|
lowLevelOpTests = testGroup "Synchronous unit tests of low-level Op interface"
|
|
|
|
[testCancelWhileHandling
|
|
|
|
,testCancelFromServer]
|
|
|
|
|
|
|
|
testCancelWhileHandling :: TestTree
|
|
|
|
testCancelWhileHandling =
|
|
|
|
testCase "Client/Server - cancel after handler starts does nothing" $
|
|
|
|
runSerialTest $ \grpc ->
|
|
|
|
withClientServerUnaryCall grpc $
|
2016-06-23 23:35:11 +02:00
|
|
|
\(Client{..}, Server{..}, cc@ClientCall{..}, ServerCall{..}) -> do
|
2016-06-13 22:51:53 +02:00
|
|
|
withOpArrayAndCtxts serverEmptyRecvOps $ \(opArray, ctxts) -> do
|
|
|
|
tag <- newTag serverCQ
|
|
|
|
startBatch serverCQ unServerCall opArray 3 tag
|
2016-06-15 19:30:17 +02:00
|
|
|
pluck serverCQ tag (Just 1)
|
2016-06-13 22:51:53 +02:00
|
|
|
let (OpRecvCloseOnServerContext pcancelled) = last ctxts
|
|
|
|
cancelledBefore <- peek pcancelled
|
|
|
|
cancelledBefore @?= 0
|
|
|
|
clientCallCancel cc
|
|
|
|
threadDelay 1000000
|
|
|
|
cancelledAfter <- peek pcancelled
|
|
|
|
cancelledAfter @?= 0
|
|
|
|
return $ Right ()
|
|
|
|
|
|
|
|
testCancelFromServer :: TestTree
|
|
|
|
testCancelFromServer =
|
|
|
|
testCase "Client/Server - client receives server cancellation" $
|
|
|
|
runSerialTest $ \grpc ->
|
|
|
|
withClientServerUnaryCall grpc $
|
2016-06-23 23:35:11 +02:00
|
|
|
\(Client{..}, Server{..}, ClientCall{..}, sc@ServerCall{..}) -> do
|
2016-06-16 17:23:54 +02:00
|
|
|
serverCallCancel sc StatusPermissionDenied "TestStatus"
|
2016-06-15 19:30:17 +02:00
|
|
|
clientRes <- runOps unClientCall clientCQ clientRecvOps
|
2016-06-13 22:51:53 +02:00
|
|
|
case clientRes of
|
|
|
|
Left x -> error $ "Client recv error: " ++ show x
|
|
|
|
Right [_,_,OpRecvStatusOnClientResult _ code details] -> do
|
2016-06-16 17:23:54 +02:00
|
|
|
code @?= StatusPermissionDenied
|
2016-06-13 22:51:53 +02:00
|
|
|
assertBool "Received status details or RST_STREAM error" $
|
|
|
|
details == "TestStatus"
|
|
|
|
||
|
|
|
|
isPrefixOf "Received RST_STREAM" details
|
|
|
|
return $ Right ()
|
2016-06-23 23:35:11 +02:00
|
|
|
wrong -> error $ "Unexpected op results: " ++ show wrong
|
2016-06-13 22:51:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
runSerialTest :: (GRPC -> IO (Either GRPCIOError ())) -> IO ()
|
|
|
|
runSerialTest f =
|
|
|
|
withGRPC f >>= \case Left x -> error $ show x
|
|
|
|
Right () -> return ()
|
|
|
|
|
|
|
|
withClientServerUnaryCall :: GRPC
|
|
|
|
-> ((Client, Server, ClientCall, ServerCall)
|
|
|
|
-> IO (Either GRPCIOError a))
|
|
|
|
-> IO (Either GRPCIOError a)
|
|
|
|
withClientServerUnaryCall grpc f = do
|
|
|
|
withClient grpc clientConf $ \c -> do
|
|
|
|
crm <- clientRegisterMethod c "/foo" Normal
|
|
|
|
withServer grpc serverConf $ \s ->
|
|
|
|
withClientCall c crm 10 $ \cc -> do
|
Preliminary streaming mode support (client streaming, server streaming, bidirectional) (#37)
* Tweak runOps param order, inline common op sequences, clean up serverHandleNormalCall
* More ops sequence inlining for clarity, experimenting with Managed
* Checkpoint: preliminary support for all streaming modes; much cleanup/refactoring and api design still needed
* Use mempty for default StatusDetails; tweak bad status matching mechanism
* Preliminary user-facing, server-streaming, low-level api and test
* renaming wibbles
* Preliminary user-facing, client-streaming, low-level api and test
* Move sendMsgs comb to Network.GRPC.LowLevel.Op; misc cleanup/DCR
* Modify bidi streaming to omit request payload
* Add transformers dep
* Preliminary user-facing low-level bidirectional streaming api and test
* Fix missing peek import
* Remove TimeoutSeconds params on streaming mode functions
* Fix serverHandleNormalCall rebase wart
* Fix rebase warts; minor hlint fixes and wibbles
* Post-rebase tweaks to optional payload use in serverRequestCall (i.e., now respects payloadHandling again)
* Cleanup/refactor serverRequestCall
* Fix comment
* Change ServerRWHandler type so that handler does not have to invoke a finalizer
* Change ServerReaderHandler type so that handler does not have to invoke a finalizer
* Simplify serverWriter interface and ServerWriterHandler structure
* Simplify serverRW (get rid of exec param), improve bidi streaming tests
* Use ExceptT in serverRW impl
* Change ServerRWHandler type to pass recv/send operations.
* Renaming
* Define ClientRWHandler, pass recv/send ops
* wibbles
* Use ExceptT in clientRW impl
* Add DataKinded phantom typing to RegisteredMethod; misc cleanup
* Simplify sendMsgs interface; add SingleSend type and related helpers
* Rename SingleSend to SendSingle, use ExceptT to clean up {client,server}Writer and sendMsgs
* More ExceptT cleanup in clientWriter
* Factor out reusable bits of clientWriter
* Shrink ServerReaderHandler
* Delete stale comments
* Use common machinery for all streaming modes; make handler types more consistent
* wibbles
2016-07-06 15:59:38 +02:00
|
|
|
let srm = head (normalMethods s)
|
2016-06-13 22:51:53 +02:00
|
|
|
-- NOTE: We need to send client ops here or else `withServerCall` hangs,
|
|
|
|
-- because registered methods try to do recv ops immediately when
|
|
|
|
-- created. If later we want to send payloads or metadata, we'll need
|
|
|
|
-- to tweak this.
|
2016-06-23 23:35:11 +02:00
|
|
|
_clientRes <- runOps (unClientCall cc) (clientCQ c) clientEmptySendOps
|
2016-06-22 19:41:14 +02:00
|
|
|
withServerCall s srm $ \sc ->
|
2016-06-13 22:51:53 +02:00
|
|
|
f (c, s, cc, sc)
|
|
|
|
|
2016-06-23 23:35:11 +02:00
|
|
|
serverConf :: ServerConfig
|
2016-06-22 22:07:38 +02:00
|
|
|
serverConf = ServerConfig "localhost" 50051 [("/foo", Normal)] []
|
2016-06-13 22:51:53 +02:00
|
|
|
|
2016-06-23 23:35:11 +02:00
|
|
|
clientConf :: ClientConfig
|
2016-06-22 22:07:38 +02:00
|
|
|
clientConf = ClientConfig "localhost" 50051 []
|
2016-06-13 22:51:53 +02:00
|
|
|
|
2016-06-23 23:35:11 +02:00
|
|
|
clientEmptySendOps :: [Op]
|
2016-06-13 22:51:53 +02:00
|
|
|
clientEmptySendOps = [OpSendInitialMetadata mempty,
|
|
|
|
OpSendMessage "",
|
|
|
|
OpSendCloseFromClient]
|
|
|
|
|
2016-06-23 23:35:11 +02:00
|
|
|
clientRecvOps :: [Op]
|
2016-06-13 22:51:53 +02:00
|
|
|
clientRecvOps = [OpRecvInitialMetadata,
|
|
|
|
OpRecvMessage,
|
|
|
|
OpRecvStatusOnClient]
|
|
|
|
|
2016-06-23 23:35:11 +02:00
|
|
|
serverEmptyRecvOps :: [Op]
|
2016-06-13 22:51:53 +02:00
|
|
|
serverEmptyRecvOps = [OpSendInitialMetadata mempty,
|
|
|
|
OpRecvMessage,
|
|
|
|
OpRecvCloseOnServer]
|