-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c68422e
commit 94c0d70
Showing
2 changed files
with
53 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Peras.NodeSpec where | ||
|
||
import qualified Data.ByteString as BS | ||
import Foreign (newArray, peekArray) | ||
import Foreign (peekArray) | ||
import Peras.Node (ByteBuffer (..), receiveFfi, sendFfi) | ||
import Test.Hspec (Spec, it, shouldBe, shouldReturn) | ||
import Test.Hspec (Spec, shouldBe, shouldReturn) | ||
import Test.Hspec.QuickCheck (prop) | ||
import Test.QuickCheck (Gen, Property, arbitrary, forAll, listOf) | ||
import Test.QuickCheck.Monadic (assert, monadicIO, run) | ||
|
||
spec :: Spec | ||
spec = | ||
it "can receive sent data" $ do | ||
dataBytes <- newArray (BS.unpack "1234567890") | ||
|
||
let buffer = ByteBuffer 10 dataBytes | ||
sendFfi 123 buffer `shouldReturn` True | ||
|
||
res <- receiveFfi | ||
case res of | ||
Left err -> fail err | ||
Right (buffer', addr) -> do | ||
addr `shouldBe` 123 | ||
bufferSize buffer' `shouldBe` 10 | ||
(BS.pack <$> peekArray 10 (bytes buffer')) `shouldReturn` "1234567890" | ||
prop "can receive sent data through dummy FFI" propDummyFFIReceivesOwnSends | ||
|
||
newtype SomeBytes = SomeBytes {toSend :: BS.ByteString} | ||
deriving newtype (Eq, Show) | ||
|
||
propDummyFFIReceivesOwnSends :: Property | ||
propDummyFFIReceivesOwnSends = | ||
forAll genSomeBytes $ \SomeBytes{toSend} -> monadicIO $ do | ||
received <- run $ do | ||
let len = BS.length toSend | ||
|
||
sendFfi 123 toSend `shouldReturn` True | ||
res <- receiveFfi | ||
|
||
case res of | ||
Left err -> fail err | ||
Right (buffer', addr) -> do | ||
addr `shouldBe` 123 | ||
bufferSize buffer' `shouldBe` fromIntegral len | ||
BS.pack <$> peekArray len (bytes buffer') | ||
|
||
assert $ received == toSend | ||
|
||
genSomeBytes :: Gen SomeBytes | ||
genSomeBytes = SomeBytes . BS.pack <$> listOf arbitrary |