A small service collects totals by shifts from multiple threads and returns a state snapshot for reporting.
ShiftQueue#push accepts a record with the keys :shift, :worker, and :count.ShiftQueue#snapshot returns a Hash where the key is the shift name and the value is the total count for that shift.ShiftQueue#snapshot must not change the object's internal state.snapshot must return the same result if no push occurred between calls.ArgumentError.ShiftQueue.snapshot twice in a row.push.Input example:
queue = ShiftQueue.new
threads = [
Thread.new { queue.push(shift: "day", worker: "ann", count: 2) },
Thread.new { queue.push(shift: "day", worker: "max", count: 3) }
]
threads.each(&:join)
first = queue.snapshot
second = queue.snapshot
"day", the snapshot should contain {"day" => 5}.first and second should match.queue.push(shift: "night", count: 1) is called, ArgumentError should be raised.Files and environment:
3.3.0ruby without additional flagslib/shift_queue.rbtest/shift_queue_test.rbRuby 3.3.0, standard library only; run with ruby; thread scheduling uses Ruby defaults; no external gems
Use line comments to explain the problem, its impact, the fix and how to test it.