hWaitForInput-accurate-socket test
authorTom Sydney Kerckhove <syd@cs-syd.eu>
Fri, 21 Dec 2018 10:35:32 +0000 (12:35 +0200)
committerMarge Bot <ben+marge-bot@smart-cactus.org>
Sun, 24 Feb 2019 02:25:41 +0000 (21:25 -0500)
libraries/base/tests/all.T
libraries/base/tests/hWaitForInput-accurate-pipe.hs [new file with mode: 0644]
libraries/base/tests/hWaitForInput-accurate-pipe.stdout [new file with mode: 0644]

index 25e851b..dc16246 100644 (file)
@@ -203,6 +203,7 @@ test('T8089',
 test('hWaitForInput-accurate-socket', reqlib('unix'), compile_and_run, [''])
 test('T8684', expect_broken(8684), compile_and_run, [''])
 test('hWaitForInput-accurate-stdin', normal, compile_and_run, [''])
+test('hWaitForInput-accurate-pipe', reqlib('unix'), compile_and_run, [''])
 test('T9826',normal, compile_and_run,[''])
 test('T9848',
         [ collect_stats('bytes allocated')
diff --git a/libraries/base/tests/hWaitForInput-accurate-pipe.hs b/libraries/base/tests/hWaitForInput-accurate-pipe.hs
new file mode 100644 (file)
index 0000000..409c60c
--- /dev/null
@@ -0,0 +1,23 @@
+import Control.Concurrent
+import Control.Monad
+import GHC.Clock
+import System.IO
+import System.Posix.IO
+import System.Timeout
+
+main :: IO ()
+main = do
+    (readPipe, _) <- createPipe
+    readPipeHandle <- fdToHandle readPipe
+    let nanoSecondsPerSecond = 1000 * 1000 * 1000
+    let milliSecondsPerSecond = 1000
+    let timeToSpend = 1
+    let timeToSpendNano = timeToSpend * nanoSecondsPerSecond
+    let timeToSpendMilli = timeToSpend * milliSecondsPerSecond
+    start <- getMonotonicTimeNSec
+    b <- hWaitForInput readPipeHandle timeToSpendMilli
+    end <- getMonotonicTimeNSec
+    let timeSpentNano = fromIntegral $ end - start
+    let delta = timeSpentNano - timeToSpendNano
+    -- We can never wait for a shorter amount of time than specified
+    putStrLn $ "delta >= 0: " ++ show (delta > 0)
diff --git a/libraries/base/tests/hWaitForInput-accurate-pipe.stdout b/libraries/base/tests/hWaitForInput-accurate-pipe.stdout
new file mode 100644 (file)
index 0000000..f1e939c
--- /dev/null
@@ -0,0 +1 @@
+delta >= 0: True