|
3 | 3 | # Released under the MIT License. |
4 | 4 | # Copyright, 2024, by Samuel Williams. |
5 | 5 |
|
6 | | -require "async" |
7 | | - |
| 6 | +require "async/job/processor/aggregate" |
8 | 7 | require "sus/fixtures/async/reactor_context" |
| 8 | +require "sus/fixtures/console/captured_logger" |
9 | 9 |
|
10 | | -require "async/job/buffer" |
11 | | -require "async/job/processor/aggregate" |
| 10 | +describe Async::Job::Processor::Aggregate do |
| 11 | + include Sus::Fixtures::Async::ReactorContext |
| 12 | + include_context Sus::Fixtures::Console::CapturedLogger |
12 | 13 |
|
13 | | -class SlowQueue |
14 | | - def initialize(delegate = nil) |
15 | | - @condition = Async::Condition.new |
16 | | - @delegate = delegate |
| 14 | + let(:delegate) do |
| 15 | + Class.new do |
| 16 | + attr_reader :called_jobs, :started, :stopped |
| 17 | + def initialize |
| 18 | + @called_jobs = [] |
| 19 | + @started = false |
| 20 | + @stopped = false |
| 21 | + end |
| 22 | + def call(job) |
| 23 | + @called_jobs << job |
| 24 | + end |
| 25 | + def start |
| 26 | + @started = true |
| 27 | + "started" |
| 28 | + end |
| 29 | + def stop |
| 30 | + @stopped = true |
| 31 | + "stopped" |
| 32 | + end |
| 33 | + end.new |
17 | 34 | end |
18 | | - |
19 | | - attr :condition |
20 | | - |
21 | | - def call(job) |
22 | | - @condition.wait |
23 | | - @delegate&.call(job) |
| 35 | + |
| 36 | + let(:processor) {subject.new(delegate)} |
| 37 | + |
| 38 | + it "processes jobs in batches" do |
| 39 | + processor.call(:job1) |
| 40 | + processor.call(:job2) |
| 41 | + |
| 42 | + # Give the processor a chance to process jobs |
| 43 | + sleep(0.1) |
| 44 | + |
| 45 | + expect(delegate.called_jobs).to be(:include?, :job1) |
| 46 | + expect(delegate.called_jobs).to be(:include?, :job2) |
24 | 47 | end |
25 | | - |
26 | | - def start |
27 | | - @delegate&.start |
| 48 | + |
| 49 | + it "handles errors in flush" do |
| 50 | + error_delegate = Class.new do |
| 51 | + def call(job) |
| 52 | + raise "Test error" |
| 53 | + end |
| 54 | + end.new |
| 55 | + |
| 56 | + error_processor = subject.new(error_delegate) |
| 57 | + error_processor.call(:job1) |
| 58 | + # Give the processor a chance to process jobs and handle error |
| 59 | + sleep(0.1) |
| 60 | + |
| 61 | + # Assert that the error was logged |
| 62 | + expect_console.to have_logged( |
| 63 | + severity: be == :error, |
| 64 | + message: be(:include?, "Could not flush") |
| 65 | + ) |
28 | 66 | end |
29 | | - |
30 | | - def stop |
31 | | - @delegate&.stop |
| 67 | + |
| 68 | + it "delegates start to super and start!" do |
| 69 | + # This will call super (Generic#start) and then start! |
| 70 | + # We can't easily check super, but we can check that start! returns true |
| 71 | + result = processor.send(:start!) |
| 72 | + expect(result).to be == true |
32 | 73 | end |
33 | | -end |
34 | 74 |
|
35 | | -describe Async::Job::Processor::Aggregate do |
36 | | - include Sus::Fixtures::Async::ReactorContext |
37 | | - |
38 | | - let(:buffer) {Async::Job::Buffer.new} |
39 | | - let(:server) {subject.new(buffer)} |
40 | | - |
41 | | - let(:job) {{"data" => "test job"}} |
42 | | - |
43 | | - it "can schedule a job" do |
44 | | - server.call(job) |
45 | | - |
46 | | - expect(buffer.pop).to be == job |
| 75 | + it "delegates start to delegate" do |
| 76 | + result = processor.start |
| 77 | + expect(delegate.started).to be == true |
| 78 | + expect(result).to be == true |
47 | 79 | end |
48 | | - |
49 | | - with "slow queue" do |
50 | | - let(:queue) {SlowQueue.new(buffer)} |
51 | | - let(:server) {subject.new(queue)} |
52 | | - |
53 | | - it "flushes jobs on shutdown" do |
54 | | - server.call(job) |
55 | | - server.stop |
56 | | - |
57 | | - expect(buffer).to be(:empty?) |
58 | | - |
59 | | - # Allow job processing to continue: |
60 | | - queue.condition.signal |
61 | | - |
62 | | - expect(buffer.pop).to be == job |
63 | | - end |
| 80 | + |
| 81 | + it "delegates stop to delegate" do |
| 82 | + result = processor.stop |
| 83 | + expect(delegate.stopped).to be == true |
| 84 | + expect(result).to be == "stopped" |
64 | 85 | end |
65 | 86 | end |
0 commit comments