|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Flatware::Sink::Signal do |
| 4 | + let(:formatter_queue) { Queue.new } |
| 5 | + |
| 6 | + let(:formatter) do |
| 7 | + queue = formatter_queue |
| 8 | + |
| 9 | + Class.new do |
| 10 | + define_method(:message, &queue.method(:push)) |
| 11 | + end.new |
| 12 | + end |
| 13 | + |
| 14 | + let(:signal_blocks) { {} } |
| 15 | + |
| 16 | + let(:on_interrupt) do |
| 17 | + -> {}.tap do |block| |
| 18 | + allow(block).to receive(:call) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + before do |
| 23 | + allow(Process).to receive(:waitall) |
| 24 | + |
| 25 | + allow(Signal).to receive(:trap) do |signal, &block| |
| 26 | + signal_blocks[signal] = block |
| 27 | + end |
| 28 | + |
| 29 | + @subject = described_class.listen(formatter, &on_interrupt).tap do |instance| |
| 30 | + allow(instance).to receive(:abort) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + attr_reader :subject |
| 35 | + |
| 36 | + def send_signal(signal) |
| 37 | + signal_blocks.fetch(signal).call |
| 38 | + end |
| 39 | + |
| 40 | + shared_examples_for 'a signal initiated shutdown' do |expected_message| |
| 41 | + before do |
| 42 | + @messages = 2.times.map do |
| 43 | + Timeout.timeout(1, StandardError, 'formatter did not receive within 1 sec') do |
| 44 | + formatter_queue.pop.message |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + attr_reader :messages |
| 50 | + |
| 51 | + it 'aborts' do |
| 52 | + expect(subject).to have_received(:abort) |
| 53 | + end |
| 54 | + |
| 55 | + it 'tells the formatter to emit the signal message' do |
| 56 | + expect(messages).to match([include(expected_message), 'done.']) |
| 57 | + end |
| 58 | + |
| 59 | + it 'calls on_interrupt' do |
| 60 | + expect(on_interrupt).to have_received(:call) |
| 61 | + end |
| 62 | + |
| 63 | + it 'waits for workers' do |
| 64 | + expect(Process).to have_received(:waitall) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe 'on SIGINT' do |
| 69 | + before do |
| 70 | + send_signal('INT') |
| 71 | + end |
| 72 | + |
| 73 | + it_should_behave_like 'a signal initiated shutdown', 'Interrupted' |
| 74 | + end |
| 75 | + |
| 76 | + describe 'on SIGCLD' do |
| 77 | + context 'when a child failed' do |
| 78 | + before do |
| 79 | + allow(Process).to receive(:wait2).and_return( |
| 80 | + [nil, double(success?: true)], |
| 81 | + [nil, double(success?: false)], |
| 82 | + nil |
| 83 | + ) |
| 84 | + |
| 85 | + send_signal('CLD') |
| 86 | + end |
| 87 | + |
| 88 | + it_should_behave_like 'a signal initiated shutdown', 'A worker died' |
| 89 | + end |
| 90 | + |
| 91 | + context 'when a child has not failed' do |
| 92 | + before do |
| 93 | + allow(Process).to receive(:wait2).and_return nil |
| 94 | + |
| 95 | + send_signal('CLD') |
| 96 | + end |
| 97 | + |
| 98 | + it 'does nothing' do |
| 99 | + expect(on_interrupt).to_not have_received(:call) |
| 100 | + expect(subject).to_not have_received(:abort) |
| 101 | + expect(formatter_queue).to be_empty |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | +end |
0 commit comments