summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2026-09-07 21:39:17 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2026-09-09 22:09:21 +0200
commit50e3f4e0cc313828aa6682ebd270e2abbfef422a (patch)
treeb682c84136dd2c54fab5be98227e8cd7359a1029 /main
parentded08b19077fb8b36ff3e3aba30cd3b1fda1e87b (diff)
Test: commands working with custom shared state
Diffstat (limited to 'main')
-rw-r--r--main/Test.hs24
-rw-r--r--main/Test/State.hs24
2 files changed, 48 insertions, 0 deletions
diff --git a/main/Test.hs b/main/Test.hs
index 1e1546c..bb3bcba 100644
--- a/main/Test.hs
+++ b/main/Test.hs
@@ -28,6 +28,8 @@ import Data.Text.IO qualified as T
import Data.Typeable
import Data.UUID.Types qualified as U
+import GHC.TypeLits
+
import Network.Socket
import System.IO
@@ -57,6 +59,7 @@ import Erebos.Storage.Merge
import Erebos.Sync
import Test.Service
+import Test.State
data TestState = TestState
@@ -344,6 +347,8 @@ commands =
, ( "local-state-wait", cmdLocalStateWait )
, ( "shared-state-get", cmdSharedStateGet )
, ( "shared-state-wait", cmdSharedStateWait )
+ , ( "shared-state-val-get", cmdSharedStateValGet )
+ , ( "shared-state-val-set", cmdSharedStateValSet )
, ( "watch-local-identity", cmdWatchLocalIdentity )
, ( "watch-shared-identity", cmdWatchSharedIdentity )
, ( "update-local-identity", cmdUpdateLocalIdentity )
@@ -876,6 +881,25 @@ cmdSharedStateGet = do
cmdSharedStateWait :: Command
cmdSharedStateWait = localStateWaitHelper "shared-state-wait" (lsShared . headObject)
+cmdSharedStateValGet :: Command
+cmdSharedStateValGet = do
+ [ stid ] <- asks tiParams
+ h <- getOrLoadHead
+ case someSymbolVal $ T.unpack stid of
+ SomeSymbol (_ :: Proxy tid) -> do
+ let value = lookupSharedValueH @(CustomSharedState tid) h
+ cmdOut $ unwords $ "shared-state-val-get" : T.unpack stid : map (BC.unpack . showRef . storedRef) (customStateComponents value)
+
+cmdSharedStateValSet :: Command
+cmdSharedStateValSet = do
+ stid : trefs <- asks tiParams
+ st <- asks tiStorage
+ Just refs <- liftIO $ fmap sequence $ mapM (readRef st . encodeUtf8) trefs
+ case someSymbolVal $ T.unpack stid of
+ SomeSymbol (_ :: Proxy tid) -> do
+ updateLocalState_ $ updateSharedState_ $ \_ -> do
+ return $ CustomSharedState @tid $ map wrappedLoad refs
+
cmdWatchLocalIdentity :: Command
cmdWatchLocalIdentity = do
h <- getOrLoadHead
diff --git a/main/Test/State.hs b/main/Test/State.hs
new file mode 100644
index 0000000..3ef1558
--- /dev/null
+++ b/main/Test/State.hs
@@ -0,0 +1,24 @@
+module Test.State (
+ CustomSharedState(..),
+) where
+
+import Data.Proxy
+
+import GHC.TypeLits
+
+import Erebos.Object
+import Erebos.State
+import Erebos.Storage.Merge
+
+
+data CustomSharedState (tid :: Symbol) = CustomSharedState
+ { customStateComponents :: StoredTips Object
+ }
+
+instance Mergeable (CustomSharedState tid) where
+ type Component (CustomSharedState tid) = Object
+ toComponents = customStateComponents
+ mergeSorted = CustomSharedState
+
+instance KnownSymbol tid => SharedType (CustomSharedState tid) where
+ sharedTypeID _ = mkSharedTypeID (symbolVal @tid Proxy)