<commit_before> {-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Data.ByteString.Lazy as B
import Data.ByteString.Lazy.Char8 as C8

-- import Test.Hspec
-- import Test.QuickCheck hiding (Result)
-- import Test.QuickCheck.Property (Result, rejected, liftBool)

import System.IO as IO

import Network as NETWORK
import Network.Socket as SOCKET hiding (listen)
import Network.BSD as BSD

import Control.Concurrent
import Control.Concurrent.MVar

import System.Process

import ClientLib
import Lib
import KVProtocol

import Control.Monad.State
import Control.Monad.Trans (liftIO)

import Data.Maybe

import Control.Concurrent.Thread.Delay

data TestState = TestState {
  masterKVHandle :: MasterHandle,
  masterHandle :: ProcessHandle,
  slaveHandles :: [ProcessHandle]
}

main :: IO ()
main = do
  state <- setUpTopology
  runStateT runTests state
  return ()

runTests :: StateT TestState IO ()
runTests = do
  testN 5
  killNodes

setUpTopology :: IO TestState
setUpTopology = do
  m <- spawnCommand "stack exec kvstore-master -- -l -n 1"
  delay 100000
  sl <- spawnCommand "stack exec kvstore-slave -- -l -n 1 -i 0"
  delay 100000
  mh <- registerWithMaster testConfig
  return $ TestState mh m [sl]

testConfig :: Lib.Config
testConfig = do
  let hn = "127.0.0.1"
  let pno = PortNumber 1063
  let clientCfg = []
  let slaveCfg = [("127.0.0.1", PortNumber 1064)]
  Lib.Config hn pno clientCfg slaveCfg Nothing Nothing

killNodes :: StateT TestState IO ()
killNodes = get >>= \s -> liftIO $ do
  terminateProcess $ masterHandle s
  sequence $ Prelude.map terminateProcess $ slaveHandles s
  return ()

testN :: Int -> StateT TestState IO ()
testN 0 = return ()
testN n = get >>= \s -> do
  let key = C8.pack $ "key" ++ show n
  let val = C8.pack $ "val" ++ show n
  liftIO $ putVal (masterKVHandle s) key val
  testN (n-1)<commit_msg>fix some tests<commit_after> {-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Data.ByteString.Lazy as B
import Data.ByteString.Lazy.Char8 as C8

-- import Test.Hspec
-- import Test.QuickCheck hiding (Result)
-- import Test.QuickCheck.Property (Result, rejected, liftBool)

import System.IO as IO

import Network as NETWORK
import Network.Socket as SOCKET hiding (listen)
import Network.BSD as BSD

import Control.Concurrent
import Control.Concurrent.MVar

import System.Process

import ClientLib
import Lib
import KVProtocol

import Control.Monad.State
import Control.Monad.Trans (liftIO)

import Data.Maybe

import Debug.Trace

import Control.Concurrent.Thread.Delay

data TestState = TestState {
  masterKVHandle :: MasterHandle,
  masterHandle :: ProcessHandle,
  slaveHandles :: [ProcessHandle],
  numCompleted :: MVar Int
}

main :: IO ()
main = do
  state <- setUpTopology
  runStateT runTests state
  return ()

runTests :: StateT TestState IO ()
runTests = do
  testN 200
  killNodes

setUpTopology :: IO TestState
setUpTopology = do
  m <- spawnCommand "stack exec kvstore-master -- -l -n 1"
  delay 100000
  sl <- spawnCommand "stack exec kvstore-worker -- -l -n 1 -i 0"
  delay 100000
  sl2 <- spawnCommand "stack exec kvstore-worker -- -l -n 1 -i 0"
  delay 100000
  mh <- registerWithMaster testConfig
  mvar <- newMVar 0
  return $ TestState mh m [sl, sl2] mvar

testConfig :: Lib.Config
testConfig = do
  let hn = "127.0.0.1"
  let pno = PortNumber 1063
  let clientCfg = []
  let slaveCfg = [("127.0.0.1", PortNumber 1064), ("127.0.0.1", PortNumber 1065)]
  Lib.Config hn pno clientCfg slaveCfg Nothing Nothing

killNodes :: StateT TestState IO ()
killNodes = get >>= \s -> liftIO $ do
  terminateProcess $ masterHandle s
  sequence $ Prelude.map terminateProcess $ slaveHandles s
  return ()

testN :: Int -> StateT TestState IO ()
testN n = do
  testNSpawn n
  testNWait n

testNWait :: Int -> StateT TestState IO ()
testNWait n = get >>= \s -> do
  completed <- liftIO $ readMVar $ numCompleted s
  if completed == n then return ()
  else do
    liftIO $ delay 10000
    testNWait n

testNSpawn :: Int -> StateT TestState IO ()
testNSpawn 0 = return ()
testNSpawn n = get >>= \s -> do
  let k = C8.pack $ "key" ++ show n
      v = C8.pack $ "val" ++ show n
  _ <- liftIO $ forkIO $ do
    putVal (masterKVHandle s) k v
    i <- takeMVar (numCompleted s)
    putMVar (numCompleted s) (i + 1)
  testNSpawn (n-1)
{'fraction_non_alphanumeric': 0.10155705321868463, 'fraction_numerical': 0.02626074831512898, 'mean_word_length': 3.4926931106471817, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2676449', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1424, 'n_words': 483}