@@ -24,24 +24,25 @@ import Crypto.Random (ChaChaDRG)
2424import Data.ByteString.Char8 (ByteString )
2525import Data.Int (Int64 )
2626import qualified Data.Map.Strict as M
27- import Data.Time.Clock.System (getSystemTime )
27+ import Data.Time.Clock.System (SystemTime ( .. ), getSystemTime )
2828import Simplex.Messaging.Crypto (pattern MaxLenBS )
2929import qualified Simplex.Messaging.Crypto as C
30- import Simplex.Messaging.Protocol (EncDataBytes (.. ), EntityId (.. ), ErrorType (.. ), LinkId , Message (.. ), QueueLinkData , RecipientId , SParty (.. ), noMsgFlags , randomMsgId )
30+ import Simplex.Messaging.Protocol (EncDataBytes (.. ), EntityId (.. ), ErrorType (.. ), LinkId , Message (.. ), NotifierId , QueueLinkData , RecipientId , SParty (.. ), noMsgFlags , randomMsgId )
3131import Simplex.Messaging.Server.MsgStore.STM
3232import Simplex.Messaging.Server.MsgStore.Types
33+ import Simplex.Messaging.Server.NtfStore
3334import Simplex.Messaging.Server.QueueStore
3435import Simplex.Messaging.Server.QueueStore.QueueInfo
3536import Simplex.Messaging.Server.QueueStore.STM (STMQueueStore (.. ))
3637import Simplex.Messaging.Server.QueueStore.Types
3738import Simplex.Messaging.TMap (TMap )
39+ import qualified Simplex.Messaging.TMap as TM
3840import Test.Hspec hiding (fit , it )
3941import Util
4042
4143#if defined(dbServerPostgres)
4244import AgentTests.FunctionalAPITests (runRight )
4345import Control.Concurrent (threadDelay )
44- import Data.Time.Clock.System (SystemTime (.. ))
4546import Database.PostgreSQL.Simple (Only (.. ))
4647import qualified Database.PostgreSQL.Simple as DB
4748import Simplex.Messaging.Agent.Store.Postgres.Common
@@ -63,6 +64,9 @@ msgStoreTests = do
6364 it " tryDelPeekMsg (ACK not from NSE) should reset message counts when queue is empty" $ withMsgStore testPostgresStoreConfig testResetMessageCounts
6465 it " should expire messages across commit batches" $ withMsgStore testPostgresStoreConfig testExpireMessagesInBatches
6566#endif
67+ describe " Notification store" $ do
68+ it " should remove keys of expired notifications" testExpireNtfs
69+ it " should remove keys of delivered notifications" testDeleteEmptyNtfs
6670 where
6771 someMsgStoreTests :: MsgStoreClass s => (HasTestEnv => MsgStoreConfig s ) -> Spec
6872 someMsgStoreTests cfg = do
@@ -281,6 +285,54 @@ testDeleteQueueMaps ms = do
281285 where
282286 expectAuth = either (`shouldBe` AUTH ) (\ _ -> expectationFailure " deleted queue is still found" )
283287
288+ mkMsgNtf :: TVar ChaChaDRG -> Int64 -> IO MsgNtf
289+ mkMsgNtf g ts = do
290+ ntfMsgId <- atomically $ randomMsgId 24 g
291+ ntfNonce <- atomically $ C. randomCbNonce g
292+ pure MsgNtf {ntfMsgId, ntfTs = MkSystemTime ts 0 , ntfNonce, ntfEncMeta = " meta" }
293+
294+ ntfCounts :: NtfStore -> IO [(NotifierId , Int )]
295+ ntfCounts (NtfStore ns) = M. assocs <$> (mapM (fmap length . readTVarIO) =<< readTVarIO ns)
296+
297+ testExpireNtfs :: IO ()
298+ testExpireNtfs = do
299+ g <- C. newRandom
300+ st@ (NtfStore ns) <- NtfStore <$> TM. emptyIO
301+ now <- systemSeconds <$> getSystemTime
302+ let old = now - 100
303+ nId1 = EntityId " notifier 1"
304+ nId2 = EntityId " notifier 2"
305+ nId3 = EntityId " notifier 3"
306+ storeNtf st nId1 =<< mkMsgNtf g old
307+ storeNtf st nId2 =<< mkMsgNtf g old
308+ storeNtf st nId2 =<< mkMsgNtf g now
309+ atomically $ TM. insertM nId3 (newTVar [] ) ns
310+ ntfCounts st `shouldReturn` [(nId1, 1 ), (nId2, 2 ), (nId3, 0 )]
311+ deleteExpiredNtfs st (now - 50 ) `shouldReturn` 2
312+ ntfCounts st `shouldReturn` [(nId2, 1 )]
313+ storeNtf st nId1 =<< mkMsgNtf g now
314+ deleteExpiredNtfs st (now - 50 ) `shouldReturn` 0
315+ ntfCounts st `shouldReturn` [(nId1, 1 ), (nId2, 1 )]
316+
317+ testDeleteEmptyNtfs :: IO ()
318+ testDeleteEmptyNtfs = do
319+ g <- C. newRandom
320+ st@ (NtfStore ns) <- NtfStore <$> TM. emptyIO
321+ now <- systemSeconds <$> getSystemTime
322+ let nId1 = EntityId " notifier 1"
323+ nId2 = EntityId " notifier 2"
324+ nId3 = EntityId " notifier 3"
325+ nId4 = EntityId " notifier 4"
326+ forM_ ([nId1, nId2, nId3, nId4] :: [NotifierId ]) $ \ nId -> storeNtf st nId =<< mkMsgNtf g now
327+ ntfs <- M. assocs <$> readTVarIO ns
328+ let flush nId = forM_ (lookup nId ntfs) $ \ v -> atomically $ writeTVar v []
329+ mapM_ flush ([nId1, nId2, nId4] :: [NotifierId ])
330+ storeNtf st nId2 =<< mkMsgNtf g now
331+ deleteNtfs st nId4 `shouldReturn` 0
332+ storeNtf st nId4 =<< mkMsgNtf g now
333+ deleteEmptyNtfs st ntfs
334+ ntfCounts st `shouldReturn` [(nId2, 1 ), (nId3, 1 ), (nId4, 1 )]
335+
284336testLinkData :: QueueLinkData
285337testLinkData = (EncDataBytes " fixed data" , EncDataBytes " user data" )
286338
0 commit comments