Skip to content

Commit

Permalink
Test that closeIOCtx is idempotent
Browse files Browse the repository at this point in the history
Install io_uring from a package manager in GHA

Small unit tests for simple no-op submissions
  • Loading branch information
jorisdral committed Mar 26, 2024
1 parent 233aba9 commit 2d2288a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ jobs:
cabal: "3.10.2.1"
os: ubuntu-22.04
liburing: "liburing-2.1"
- ghc: "9.6.4"
cabal: "3.10.2.1"
os: ubuntu-22.04
liburing: "system"

steps:
- name: Checkout repository
Expand All @@ -52,8 +56,9 @@ jobs:
cabal-version: ${{ matrix.cabal }}
cabal-update: true

- name: Install liburing (on Linux)
id: setup-liburing
- name: Install (specific) liburing from source
id: setup-liburing-source
if: ${{ ! (matrix.liburing == 'system') }}
run: |
sudo apt-get update
sudo apt-get -y install pkg-config
Expand All @@ -70,6 +75,13 @@ jobs:
sudo rm -rf ./tmp
pkg-config --modversion liburing
- name: Install (newest) liburing from a package manager
id: setup-liburing-cloned
if: ${{ matrix.liburing == 'system' }}
run: |
sudo apt-get update
sudo apt-get -y install pkg-config liburing-dev
- name: Configure the build
run: |
cabal configure --enable-test --enable-benchmark --ghc-options="-Werror" --ghc-options="-fno-ignore-asserts"
Expand Down
2 changes: 1 addition & 1 deletion System/IO/BlockIO/URingFFI.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ foreign import capi unsafe "liburing.h io_uring_sqe_set_data64"
#else
io_uring_sqe_set_data :: Ptr URingSQE -> CULong -> IO ()
io_uring_sqe_set_data p user_data =
do #{poke struct io_uring_cqe, user_data} p user_data
do #{poke struct io_uring_sqe, user_data} p user_data
#endif

foreign import capi unsafe "liburing.h io_uring_prep_read"
Expand Down
35 changes: 34 additions & 1 deletion blockio-uring.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ source-repository head
type: git
location: https://github.com/well-typed/blockio-uring

common warnings
ghc-options:
-Wall -Wcompat -Wincomplete-uni-patterns
-Wincomplete-record-updates -Wpartial-fields -Widentities
-Wredundant-constraints -Wmissing-export-lists
-Wno-unticked-promoted-constructors -Wunused-packages

common wno-x-partial
if impl(ghc >=9.8)
-- No errors for x-partial functions. We might remove this in the future if
-- we decide to refactor code that uses partial functions.
ghc-options: -Wno-x-partial

library
exposed-modules: System.IO.BlockIO
other-modules:
Expand All @@ -45,7 +58,7 @@ library
, primitive ^>=0.9
, unix ^>=2.8

pkgconfig-depends: liburing >= 2.0 && < 2.7
pkgconfig-depends: liburing >=2.0 && <2.7
default-language: Haskell2010
ghc-options: -Wall

Expand All @@ -71,3 +84,23 @@ benchmark bench
System.IO.BlockIO.URingFFI

ghc-options: -Wall -threaded

test-suite test
default-language: Haskell2010
type: exitcode-stdio-1.0
hs-source-dirs: test .
main-is: Main.hs
build-depends:
, array
, base >=4.16 && <4.20
, primitive
, tasty
, tasty-hunit

pkgconfig-depends: liburing
other-modules:
System.IO.BlockIO
System.IO.BlockIO.URing
System.IO.BlockIO.URingFFI

ghc-options: -threaded -fno-ignore-asserts
55 changes: 55 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}

{-# OPTIONS_GHC -Wno-orphans #-}

{- HLINT ignore "Use camelCase" -}

module Main (main) where

import Control.Exception (SomeException, try)
import Data.Word (Word64)
import System.IO.BlockIO
import System.IO.BlockIO.URing as URing
import Test.Tasty
import Test.Tasty.HUnit

main :: IO ()
main = defaultMain tests

tests :: TestTree
tests = testGroup "test" [
testCase "example_simpleNoop 1" $ example_simpleNoop 1
, testCase "example_simpleNoop maxBound" $ example_simpleNoop maxBound
, testCase "example_initClose" example_initClose
, testCase "example_closeIsIdempotent" example_closeIsIdempotent
]

example_simpleNoop :: Word64 -> Assertion
example_simpleNoop n = do
uring <- setupURing (URingParams 1)
prepareNop uring (IOOpId n)
URing.submitIO uring
completion <- awaitIO uring
closeURing uring
IOCompletion (IOOpId n) 0 @=? completion

deriving instance Eq IOCompletion
deriving instance Show IOCompletion

example_initClose :: Assertion
example_initClose = do
ctx <- initIOCtx defaultIOCtxParams
closeIOCtx ctx

example_closeIsIdempotent :: Assertion
example_closeIsIdempotent = do
ctx <- initIOCtx defaultIOCtxParams
closeIOCtx ctx
eith <- try @SomeException (closeIOCtx ctx)
case eith of
Left (e :: SomeException) ->
assertFailure ("Close on a closed context threw an error : " <> show e)
Right () ->
pure ()

0 comments on commit 2d2288a

Please sign in to comment.