2016-05-24 23:27:15 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2016-05-26 00:41:37 +02:00
|
|
|
module UnsafeTests (unsafeTests) where
|
2016-05-24 23:27:15 +02:00
|
|
|
|
2016-07-11 20:20:13 +02:00
|
|
|
import Control.Concurrent (threadDelay)
|
2016-05-26 00:41:37 +02:00
|
|
|
import Control.Concurrent.Async
|
|
|
|
import Control.Exception (bracket_)
|
|
|
|
import Control.Monad
|
|
|
|
import qualified Data.ByteString as B
|
|
|
|
import Foreign.Marshal.Alloc
|
|
|
|
import Foreign.Ptr
|
|
|
|
import Foreign.Storable
|
2016-05-24 23:27:15 +02:00
|
|
|
import Network.GRPC.Unsafe
|
|
|
|
import Network.GRPC.Unsafe.ByteBuffer
|
2016-05-26 00:41:37 +02:00
|
|
|
import Network.GRPC.Unsafe.Constants
|
2016-05-24 23:27:15 +02:00
|
|
|
import Network.GRPC.Unsafe.Metadata
|
2016-05-26 00:41:37 +02:00
|
|
|
import Network.GRPC.Unsafe.Op
|
2016-05-24 23:27:15 +02:00
|
|
|
import Network.GRPC.Unsafe.Slice
|
|
|
|
import Network.GRPC.Unsafe.Time
|
2016-06-22 22:07:38 +02:00
|
|
|
import Network.GRPC.Unsafe.ChannelArgs
|
2016-07-11 20:20:13 +02:00
|
|
|
import System.Clock
|
2016-05-24 23:27:15 +02:00
|
|
|
import Test.Tasty
|
2016-07-11 20:20:13 +02:00
|
|
|
import Test.Tasty.HUnit as HU (testCase, (@?=),
|
|
|
|
assertBool)
|
2016-05-24 23:27:15 +02:00
|
|
|
|
|
|
|
unsafeTests :: TestTree
|
2016-05-25 19:04:48 +02:00
|
|
|
unsafeTests = testGroup "Unit tests for unsafe C bindings"
|
2016-05-24 23:27:15 +02:00
|
|
|
[ roundtripSlice "Hello, world!"
|
|
|
|
, roundtripByteBuffer "Hwaet! We gardena in geardagum..."
|
2016-06-22 22:07:38 +02:00
|
|
|
, roundtripSlice largeByteString
|
|
|
|
, roundtripByteBuffer largeByteString
|
2016-07-11 20:20:13 +02:00
|
|
|
, roundtripTimeSpec (TimeSpec 123 123)
|
2016-05-24 23:27:15 +02:00
|
|
|
, testMetadata
|
|
|
|
, testNow
|
|
|
|
, testCreateDestroyMetadata
|
|
|
|
, testCreateDestroyMetadataKeyVals
|
|
|
|
, testCreateDestroyDeadline
|
2016-06-22 22:07:38 +02:00
|
|
|
, testCreateDestroyChannelArgs
|
2016-05-24 23:27:15 +02:00
|
|
|
]
|
|
|
|
|
2016-06-22 22:07:38 +02:00
|
|
|
largeByteString :: B.ByteString
|
|
|
|
largeByteString = B.pack $ take (32*1024*1024) $ cycle [97..99]
|
|
|
|
|
2016-05-24 23:27:15 +02:00
|
|
|
roundtripSlice :: B.ByteString -> TestTree
|
2016-05-25 19:04:48 +02:00
|
|
|
roundtripSlice bs = testCase "ByteString slice roundtrip" $ do
|
2016-05-24 23:27:15 +02:00
|
|
|
slice <- byteStringToSlice bs
|
|
|
|
unslice <- sliceToByteString slice
|
|
|
|
bs HU.@?= unslice
|
|
|
|
freeSlice slice
|
|
|
|
|
|
|
|
roundtripByteBuffer :: B.ByteString -> TestTree
|
2016-05-25 19:04:48 +02:00
|
|
|
roundtripByteBuffer bs = testCase "ByteBuffer roundtrip" $ do
|
2016-05-24 23:27:15 +02:00
|
|
|
slice <- byteStringToSlice bs
|
|
|
|
buffer <- grpcRawByteBufferCreate slice 1
|
|
|
|
reader <- byteBufferReaderCreate buffer
|
|
|
|
readSlice <- grpcByteBufferReaderReadall reader
|
|
|
|
bs' <- sliceToByteString readSlice
|
|
|
|
bs' HU.@?= bs
|
|
|
|
-- clean up
|
|
|
|
freeSlice slice
|
|
|
|
byteBufferReaderDestroy reader
|
|
|
|
grpcByteBufferDestroy buffer
|
|
|
|
freeSlice readSlice
|
|
|
|
|
2016-07-11 20:20:13 +02:00
|
|
|
roundtripTimeSpec :: TimeSpec -> TestTree
|
|
|
|
roundtripTimeSpec t = testCase "CTimeSpec roundtrip" $ do
|
|
|
|
p <- malloc
|
|
|
|
let c = CTimeSpec t
|
|
|
|
poke p c
|
|
|
|
c' <- peek p
|
|
|
|
c' @?= c
|
|
|
|
free p
|
|
|
|
|
2016-05-24 23:27:15 +02:00
|
|
|
testMetadata :: TestTree
|
2016-05-25 19:04:48 +02:00
|
|
|
testMetadata = testCase "Metadata setter/getter roundtrip" $ do
|
2016-05-24 23:27:15 +02:00
|
|
|
m <- metadataAlloc 3
|
|
|
|
setMetadataKeyVal "hello" "world" m 0
|
|
|
|
setMetadataKeyVal "foo" "bar" m 1
|
|
|
|
setMetadataKeyVal "Haskell" "Curry" m 2
|
|
|
|
k0 <- getMetadataKey m 0
|
|
|
|
v0 <- getMetadataVal m 0
|
|
|
|
k1 <- getMetadataKey m 1
|
|
|
|
v1 <- getMetadataVal m 1
|
|
|
|
k2 <- getMetadataKey m 2
|
|
|
|
v2 <- getMetadataVal m 2
|
|
|
|
k0 HU.@?= "hello"
|
|
|
|
v0 HU.@?= "world"
|
|
|
|
k1 HU.@?= "foo"
|
|
|
|
v1 HU.@?= "bar"
|
|
|
|
k2 HU.@?= "Haskell"
|
|
|
|
v2 HU.@?= "Curry"
|
|
|
|
metadataFree m
|
|
|
|
|
|
|
|
currTimeMillis :: ClockType -> IO Int
|
|
|
|
currTimeMillis t = do
|
|
|
|
gprT <- gprNow t
|
|
|
|
tMillis <- gprTimeToMillis gprT
|
|
|
|
timespecDestroy gprT
|
|
|
|
return tMillis
|
|
|
|
|
|
|
|
testNow :: TestTree
|
2016-05-25 19:04:48 +02:00
|
|
|
testNow = testCase "Create/destroy various clock types" $ do
|
2016-05-24 23:27:15 +02:00
|
|
|
_ <- currTimeMillis GprClockMonotonic
|
|
|
|
_ <- currTimeMillis GprClockRealtime
|
|
|
|
_ <- currTimeMillis GprClockPrecise
|
|
|
|
return ()
|
|
|
|
|
|
|
|
testCreateDestroyMetadata :: TestTree
|
2016-05-25 19:04:48 +02:00
|
|
|
testCreateDestroyMetadata = testCase "Create/destroy metadataArrayPtr" $ do
|
2016-05-26 00:41:37 +02:00
|
|
|
grpc $ withMetadataArrayPtr $ const $ return ()
|
2016-05-24 23:27:15 +02:00
|
|
|
|
|
|
|
testCreateDestroyMetadataKeyVals :: TestTree
|
2016-05-25 19:04:48 +02:00
|
|
|
testCreateDestroyMetadataKeyVals = testCase "Create/destroy metadata key/values" $ do
|
2016-05-26 00:41:37 +02:00
|
|
|
grpc $ withMetadataKeyValPtr 10 $ const $ return ()
|
2016-05-24 23:27:15 +02:00
|
|
|
|
|
|
|
testCreateDestroyDeadline :: TestTree
|
2016-05-25 19:04:48 +02:00
|
|
|
testCreateDestroyDeadline = testCase "Create/destroy deadline" $ do
|
2016-05-26 00:41:37 +02:00
|
|
|
grpc $ withDeadlineSeconds 10 $ const $ return ()
|
|
|
|
|
2016-06-22 22:07:38 +02:00
|
|
|
testCreateDestroyChannelArgs :: TestTree
|
|
|
|
testCreateDestroyChannelArgs = testCase "Create/destroy channel args" $
|
|
|
|
grpc $ withChannelArgs [CompressionAlgArg GrpcCompressDeflate] $
|
|
|
|
const $ return ()
|
|
|
|
|
2016-05-26 00:41:37 +02:00
|
|
|
assertCqEventComplete :: Event -> IO ()
|
|
|
|
assertCqEventComplete e = do
|
|
|
|
eventCompletionType e HU.@?= OpComplete
|
|
|
|
eventSuccess e HU.@?= True
|
|
|
|
|
|
|
|
grpc :: IO a -> IO ()
|
|
|
|
grpc = bracket_ grpcInit grpcShutdown . void
|
2016-07-11 20:20:13 +02:00
|
|
|
|
|
|
|
threadDelaySecs :: Int -> IO ()
|
|
|
|
threadDelaySecs = threadDelay . (* 10^(6::Int))
|