Skip to content

Commit

Permalink
Fix: ssParser
Browse files Browse the repository at this point in the history
  • Loading branch information
fisx committed Dec 23, 2024
1 parent b237893 commit ef90411
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions integration/test/Testlib/ModService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ where

import Control.Concurrent
import Control.Concurrent.Async
import Control.Exception (assert)
import qualified Control.Exception as E
import Control.Monad.Catch (catch, displayException, throwM)
import Control.Monad.Codensity
Expand All @@ -19,6 +20,7 @@ import Control.Monad.Reader
import Control.Retry (fibonacciBackoff, limitRetriesByCumulativeDelay, retrying)
import Data.Aeson hiding ((.=))
import qualified Data.Attoparsec.Text as Parser
import Data.Char (isSpace)
import Data.Default
import Data.Foldable
import Data.Function
Expand Down Expand Up @@ -309,10 +311,21 @@ ensureFederatorPortIsFree resource = do
Left e -> assertFailure $ "Failed while parsing ss output with error: " <> e

parseSS :: Text -> Either String (Maybe (String, ProcessID))
parseSS input =
if Text.null input
then pure Nothing
else Just <$> Parser.parseOnly (ssParser <* Parser.endOfInput) input
parseSS = assert (and tests) prs
where
tests =
[ parseSS "LISTEN 0 4096 127.0.0.1:8082 0.0.0.0:* users:((\"brig\",pid=51468,fd=79))"
== Right (Just ("brig", 51468)),
parseSS "LISTEN 0 4096 127.0.0.1:8082 0.0.0.0:* users:((\"brig\",pid=51468,fd=79)) \t"
== Right (Just ("brig", 51468)),
parseSS "LISTEN 0 4096 127.0.0.1:8082 0.0.0.0:* users:((\"brig\",pid=51468,fd=79)) \n"
== Right (Just ("brig", 51468))
]

prs input =
if Text.null input
then pure Nothing
else Just <$> Parser.parseOnly ssParser input

-- Example input:
-- LISTEN 0 4096 127.0.0.1:8082 0.0.0.0:* users:(("brig",pid=51468,fd=79))
Expand All @@ -327,12 +340,11 @@ ssParser = do
name <- quoted
_ <- Parser.char ','
p <- pid
_ <- Parser.many1 noNewLine
pure (name, p)
where
spaces = void $ Parser.many' Parser.space
noSpace = Parser.satisfy (/= ' ')
noSpaces = Parser.many1 noSpace
noSpaces = Parser.takeWhile1 (not . isSpace)
noDoubleQuote = Parser.takeWhile1 (/= '"')
token p = do
spaces
res <- p
Expand All @@ -344,13 +356,12 @@ ssParser = do
quoted = do
token $ do
_ <- Parser.char '"'
tok <- noSpaces
tok <- Text.unpack <$> noDoubleQuote
_ <- Parser.char '"'
pure tok
pid = do
ignoreStrToken "pid="
Parser.decimal
noNewLine = Parser.satisfy (/= '\n')

ensureBackendReachable :: (HasCallStack) => String -> App ()
ensureBackendReachable domain = do
Expand Down

0 comments on commit ef90411

Please sign in to comment.