A small service stores information about which users are in presence rooms. After a recent PR, the results became unstable: some participants may disappear from the state snapshot, and during parallel updates one error can stop processing of the remaining events.
PresenceBoard class accepts events through the record(room_id, user_id) method.user_ids must be stored.snapshot method must return a regular Hash of the form room_id => [user_id, ...].snapshot, the list of users for each room must be sorted in ascending order.board = PresenceBoard.new
board.record("alpha", 3)
board.record("alpha", 1)
board.record("alpha", 2)
p board.snapshot
Notice that the snapshot may contain not all participants of the room.
Then run parallel processing where one thread fails after some successful updates:
board = PresenceBoard.new
inputs = [
["alpha", 1],
["beta", 2],
nil,
["alpha", 3]
]
begin
board.record_all(inputs)
rescue => e
puts e.class
end
p board.snapshot
snapshot always returns a complete snapshot of already recorded users across all rooms.record_all is invalid, the exception should be visible to the caller, but successfully completed writes from other threads must not disappear from the final state.3.3.0Fiber schedulerThread, synchronization via Mutexruby presence_board.rb or via ruby -r ./presence_board.rb -e '...'room_id is a String, user_id is an Integer, and in record_all each valid element is an array of two valuesUse line comments to explain the problem, its impact, the fix and how to test it.