New herbarium labels disappear while a print batch is running
Level 3 · node · Concurrency · Asynchronous snapshot completion removes later queued entries
Scenario
A fictional herbarium workshop queues label names in memory and delivers them in batches to a printer. New labels can be registered while printing is underway. Only one print operation is designed to run at a time, but after a successful print, newly registered labels that have not been sent sometimes disappear from the queue. Trace the state across the entry point, asynchronous print coordinator and queue.
Requirements
createLabelDesk(send) creates an independent queue. enqueue(label) registers a string label and pending() returns an independent array of unacknowledged labels in order. flush() sends a snapshot of labels pending at call time to send(batch) and returns a Promise resolving to the batch size on success. For an empty queue it returns 0 without calling send. Other flush() calls made during printing share the current operation's result and do not automatically send another batch. Remove only the successful batch; preserve labels added meanwhile until the next flush(). If send fails, propagate the same exception and retain the original batch and later labels in order for retry on the next call. Equal strings are distinct registrations. send is a local function that does not mutate its input array; failure means the batch was not processed.
Acceptance criteria
- Empty and ordinary flushes have correct call counts, batch order and return counts.
- Labels added during printing survive the earlier batch's success and are sent in order on the next flush.
- On print failure preserve the original exception and retain the original batch and later labels for retry.
- Overlapping flush calls share one operation's result, preserve duplicate names as separate registrations and return independent arrays on reads.