fs-sim-0.3.1.0: Simulated file systems
Safe HaskellSafe-Inferred
LanguageHaskell2010

System.FS.Sim.Error

Description

HasFS instance wrapping SimFS that generates errors, suitable for testing error handling.

Synopsis

Simulate Errors monad

simErrorHasFS :: forall m. (MonadSTM m, MonadThrow m, PrimMonad m) => StrictTMVar m MockFS -> StrictTVar m Errors -> HasFS m HandleMock #

Introduce possibility of errors

simErrorHasFS' :: (MonadSTM m, MonadThrow m, PrimMonad m) => MockFS -> Errors -> m (HasFS m HandleMock) #

Alternative to simErrorHasFS that creates TVars internally.

runSimErrorFS :: (MonadSTM m, MonadThrow m, PrimMonad m) => MockFS -> Errors -> (StrictTVar m Errors -> HasFS m HandleMock -> m a) -> m (a, MockFS) #

Runs a computation provided an Errors and an initial MockFS, producing a result and the final state of the filesystem.

withErrors :: (MonadSTM m, MonadThrow m) => StrictTVar m Errors -> Errors -> m a -> m a #

Execute the next action using the given Errors. After the action is finished, the previous Errors are restored.

Streams

type ErrorStream = Stream FsErrorType #

An ErrorStream is a possibly infinite Stream of (Maybe) FsErrorTypes.

Nothing indicates that there is no error.

Each time the ErrorStream is used (see runErrorStream), the first element (Nothing in case the list is empty) is taken from the list and an ErrorStream with the remainder of the list is returned. The first element represents whether an error should be returned or not.

An FsError consists of a number of fields: fsErrorType, a fsErrorPath, etc. Only the first fields is interesting. Therefore, we only generate the FsErrorType. The FsErrorType will be used to construct the actual FsError.

type ErrorStreamGetSome = Stream (Either FsErrorType Partial) #

ErrorStream for reading bytes from a file: an error or a partial get.

type ErrorStreamPutSome = Stream (Either (FsErrorType, Maybe PutCorruption) Partial) #

ErrorStream for writing bytes to a file: an error and possibly some corruption, or a partial write.

Generating partial reads/writes

newtype Partial #

A Partial p, where p > 0, is a number representing how many fewer bytes should be read or written than requested.

Constructors

Partial Word64 

Instances

Instances details
Arbitrary Partial # 
Instance details

Defined in System.FS.Sim.Error

Show Partial # 
Instance details

Defined in System.FS.Sim.Error

partialiseByteCount :: Partial -> ByteCount -> ByteCount #

Given a requested number of bytes to read/write, compute a partial number of bytes to read/write.

We subtract p from the number of requested bytes. If that would result in 0 requested bytes or less, we request 1 byte. If the number of requested bytes was already 0, we can't simulate a partial read so we return 0 again.

partialiseByteString :: Partial -> ByteString -> ByteString #

Given a bytestring that is requested to be written to disk, use partialiseByteCount to compute a partial bytestring.

Blob

newtype Blob #

Constructors

MkBlob 

Fields

Instances

Instances details
Arbitrary Blob # 
Instance details

Defined in System.FS.Sim.Error

Methods

arbitrary :: Gen Blob #

shrink :: Blob -> [Blob] #

IsString Blob # 
Instance details

Defined in System.FS.Sim.Error

Methods

fromString :: String -> Blob #

Show Blob # 
Instance details

Defined in System.FS.Sim.Error

Methods

showsPrec :: Int -> Blob -> ShowS #

show :: Blob -> String #

showList :: [Blob] -> ShowS #

Generating corruption for hPutSome

data PutCorruption #

Model possible corruptions that could happen to a hPutSome call.

Constructors

SubstituteWithJunk Blob

The blob to write is substituted with corrupt junk

PartialWrite Partial

Only perform the write partially

Instances

Instances details
Arbitrary PutCorruption # 
Instance details

Defined in System.FS.Sim.Error

Show PutCorruption # 
Instance details

Defined in System.FS.Sim.Error

corruptByteString :: ByteString -> PutCorruption -> ByteString #

Apply the PutCorruption to the ByteString.

If the bytestring is substituted by corrupt junk, then the output bytestring might be larger than the input bytestring.

Error streams for HasFS

data Errors #

Error streams for the methods of the HasFS type class.

An ErrorStream is provided for each method of the HasFS type class. This ErrorStream will be used to generate potential errors that will be thrown by the corresponding method.

For hPutSome, an ErrorStreamWithCorruption is provided to simulate corruption.

An Errors is used in conjunction with SimErrorFS, which is a layer on top of SimFS that simulates methods throwing FsErrors.

Instances

Instances details
Arbitrary Errors # 
Instance details

Defined in System.FS.Sim.Error

Show Errors # 
Instance details

Defined in System.FS.Sim.Error

allNull :: Errors -> Bool #

Return True if all streams are empty (null).

genErrors #

Arguments

:: Bool

True -> generate partial writes

-> Bool

True -> generate SubstituteWithJunk corruptions

-> Gen Errors 

Generator for Errors that allows some things to be disabled.

This is needed by the VolatileDB state machine tests, which try to predict what should happen based on the Errors, which is too complex sometimes.

simpleErrors :: ErrorStream -> Errors #

Use the given ErrorStream for each field/method. No corruption of hPutSome.