summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2024-07-16 21:18:59 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2024-07-16 21:29:51 +0200
commit710e6121a755959dbf423aa828bd3cc7af4214a2 (patch)
tree9c619066c5c7f2ea61beb69de7a738b16e5bc744
parent35066bdbd2475b632dfb3d824a297cbbaa7b1aa3 (diff)
Handle parallel thread diff in chatrooms
-rw-r--r--src/Erebos/Chatroom.hs11
-rw-r--r--test/chatroom.test64
2 files changed, 68 insertions, 7 deletions
diff --git a/src/Erebos/Chatroom.hs b/src/Erebos/Chatroom.hs
index b2432c5..ae373b6 100644
--- a/src/Erebos/Chatroom.hs
+++ b/src/Erebos/Chatroom.hs
@@ -161,8 +161,8 @@ instance Storable ChatMessageData where
mdLeave <- isJust <$> loadMbEmpty "leave"
return ChatMessageData {..}
-threadToList :: [Stored (Signed ChatMessageData)] -> [ChatMessage]
-threadToList thread = helper S.empty $ thread
+threadToListSince :: [ Stored (Signed ChatMessageData) ] -> [ Stored (Signed ChatMessageData) ] -> [ ChatMessage ]
+threadToListSince since thread = helper (S.fromList since) thread
where
helper :: S.Set (Stored (Signed ChatMessageData)) -> [Stored (Signed ChatMessageData)] -> [ChatMessage]
helper seen msgs
@@ -244,7 +244,7 @@ instance Mergeable ChatroomState where
ChatroomStateData {..} | null rsdMessages -> Nothing
| otherwise -> Just rsdMessages
roomStateSubscribe = fromMaybe False $ findPropertyFirst rsdSubscribe roomStateData
- roomStateMessages = threadToList $ concatMap (rsdMessages . fromStored) roomStateData
+ roomStateMessages = threadToListSince [] $ concatMap (rsdMessages . fromStored) roomStateData
in ChatroomState {..}
toComponents = roomStateData
@@ -342,10 +342,7 @@ chatroomSetSubscribe lookupData subscribe = void $ findAndUpdateChatroomState $
}
getMessagesSinceState :: ChatroomState -> ChatroomState -> [ChatMessage]
-getMessagesSinceState cur old = takeWhile notOld (roomStateMessages cur)
- where
- notOld msg = cmsgData msg `notElem` roomStateMessageData old
- -- TODO: parallel message threads
+getMessagesSinceState cur old = threadToListSince (roomStateMessageData old) (roomStateMessageData cur)
data ChatroomSetChange = AddedChatroom ChatroomState
diff --git a/test/chatroom.test b/test/chatroom.test
index 9b68839..1998290 100644
--- a/test/chatroom.test
+++ b/test/chatroom.test
@@ -280,3 +280,67 @@ test ChatroomSubscribedBeforeStart:
send "chatroom-message-send $room1_p2 message2" to p2
expect /chatroom-message-new $room1_p1 room first_room from Owner2 text message2/ from p1
expect /chatroom-message-new $room1_p2 room first_room from Owner2 text message2/ from p2
+
+
+test ParallelThreads:
+ spawn as p1
+ spawn as p2
+
+ send "create-identity Device1 Owner1" to p1
+ send "create-identity Device2 Owner2" to p2
+
+ for p in [ p1, p2 ]:
+ with p:
+ send "chatroom-watch-local"
+ send "start-server"
+
+ send "chatroom-create first_room" to p1
+ expect /chatroom-create-done ([a-z0-9#]+) first_room.*/ from p1 capture room1_p1
+
+ expect /chatroom-watched-added [a-z0-9#]+ first_room sub true/ from p1
+ expect /chatroom-watched-added ([a-z0-9#]+) first_room sub false/ from p2 capture room1_p2
+
+ with p2:
+ send "chatroom-subscribe $room1_p2"
+ expect /chatroom-watched-updated [a-z0-9#]+ first_room sub true .*/
+
+ for p in [p1, p2]:
+ with p:
+ send "stop-server"
+ for p in [p1, p2]:
+ with p:
+ expect /stop-server-done/
+
+ send "chatroom-message-send $room1_p1 message1A" to p1
+ send "chatroom-message-send $room1_p1 message1B" to p1
+ send "chatroom-message-send $room1_p2 message2A" to p2
+ send "chatroom-message-send $room1_p2 message2B" to p2
+ with p1:
+ expect /chatroom-message-new $room1_p1 room first_room from Owner. text message(..)/ capture msg
+ guard (msg == "1A")
+ with p1:
+ expect /chatroom-message-new $room1_p1 room first_room from Owner. text message(..)/ capture msg
+ guard (msg == "1B")
+ with p2:
+ expect /chatroom-message-new $room1_p2 room first_room from Owner. text message(..)/ capture msg
+ guard (msg == "2A")
+ with p2:
+ expect /chatroom-message-new $room1_p2 room first_room from Owner. text message(..)/ capture msg
+ guard (msg == "2B")
+
+ for p in [p1, p2]:
+ with p:
+ send "start-server"
+
+ with p1:
+ expect /chatroom-message-new $room1_p1 room first_room from Owner. text message(..)/ capture msg
+ guard (msg == "2A")
+ with p1:
+ expect /chatroom-message-new $room1_p1 room first_room from Owner. text message(..)/ capture msg
+ guard (msg == "2B")
+ with p2:
+ expect /chatroom-message-new $room1_p2 room first_room from Owner. text message(..)/ capture msg
+ guard (msg == "1A")
+ with p2:
+ expect /chatroom-message-new $room1_p2 room first_room from Owner. text message(..)/ capture msg
+ guard (msg == "1B")