-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for simulation with error injection
- Loading branch information
Showing
4 changed files
with
72 additions
and
25 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,20 +1,13 @@ | ||
module Main (main) where | ||
|
||
import System.IO.Temp (withSystemTempDirectory) | ||
|
||
import Test.Tasty | ||
|
||
import qualified Test.System.FS.Sim.Error | ||
import qualified Test.System.FS.Sim.FsTree | ||
import qualified Test.System.FS.StateMachine | ||
import Test.Tasty | ||
|
||
main :: IO () | ||
main = withSystemTempDirectory "fs-sim-test" $ \tmpDir -> | ||
defaultMain $ | ||
testGroup "Test" [ | ||
testGroup "System" [ | ||
testGroup "FS" [ | ||
Test.System.FS.StateMachine.tests tmpDir | ||
, Test.System.FS.Sim.FsTree.tests | ||
] | ||
] | ||
] | ||
main = defaultMain $ testGroup "fs-sim-test" [ | ||
Test.System.FS.Sim.Error.tests | ||
, Test.System.FS.Sim.FsTree.tests | ||
, Test.System.FS.StateMachine.tests | ||
] |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
module Test.System.FS.Sim.Error (tests) where | ||
|
||
import Control.Concurrent.Class.MonadSTM.Strict | ||
import Data.ByteString | ||
import qualified Data.ByteString as BS | ||
import System.FS.API | ||
import qualified System.FS.API.Strict as Strict | ||
import System.FS.Sim.Error | ||
import qualified System.FS.Sim.MockFS as MockFS | ||
import qualified System.FS.Sim.Stream as Stream | ||
import Test.Tasty | ||
import Test.Tasty.QuickCheck | ||
|
||
tests :: TestTree | ||
tests = testGroup "Test.System.FS.Sim.Error" [ | ||
testProperty "propPutAllStrictPutsAll" $ | ||
forAllShrink sometimesPartialWrites Stream.shrinkStream | ||
propPutAllStrictPutsAll | ||
] | ||
|
||
instance Arbitrary ByteString where | ||
arbitrary = BS.pack <$> arbitrary | ||
shrink = fmap BS.pack . shrink . BS.unpack | ||
|
||
-- | Verify that 'hPutAllStrict' writes all requested bytes in the presence of | ||
-- partial writes. | ||
propPutAllStrictPutsAll :: ErrorStreamPutSome -> ByteString -> Property | ||
propPutAllStrictPutsAll errStream bs = | ||
ioProperty $ do | ||
fsVar <- newTMVarIO MockFS.empty | ||
errVar <- newTVarIO (emptyErrors { hPutSomeE = errStream }) | ||
let hfs = mkSimErrorHasFS fsVar errVar | ||
prop <- withFile hfs (mkFsPath ["file1"]) (ReadWriteMode MustBeNew) $ \h -> do | ||
n' <- Strict.hPutAllStrict hfs h bs | ||
let n = fromIntegral $ BS.length bs | ||
bs' <- hGetSomeAt hfs h n 0 | ||
pure (n === n' .&&. bs === bs') | ||
fcover <- withFile hfs (mkFsPath ["file2"]) (ReadWriteMode MustBeNew) $ \h -> do | ||
n' <- Strict.hPutSome hfs h bs | ||
let n = fromIntegral $ BS.length bs | ||
pure $ cover 0.5 (n /= n') "At least one partial write" | ||
pure $ fcover prop | ||
|
||
sometimesPartialWrites :: Gen ErrorStreamPutSome | ||
sometimesPartialWrites = Stream.genInfinite (Just . Right <$> arbitrary) |
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