Skip to content

Commit af17e94

Browse files
committed
Tidy up tests.
1 parent c642d28 commit af17e94

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

lib/async/job/processor/inline.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ def initialize(delegate, parent: nil)
2323
super(delegate)
2424

2525
@parent = parent || Async::Idler.new
26+
27+
@call_count = 0
28+
@complete_count = 0
2629
end
2730

31+
# @attribute [Integer] The count of completed jobs.
32+
attr_reader :complete_count
33+
2834
# Process a job asynchronously with optional scheduling.
2935
# If the job has a scheduled_at time, the processor will wait until that time before execution.
3036
# @parameter job [Hash] The job data containing execution details.
@@ -36,7 +42,9 @@ def call(job)
3642
sleep(scheduled_at - Time.now)
3743
end
3844

45+
@call_count += 1
3946
@delegate.call(job)
47+
@complete_count += 1
4048
rescue => error
4149
Console.error(self, error)
4250
end
@@ -51,6 +59,13 @@ def start
5159
def stop
5260
@delegate.stop
5361
end
62+
63+
# Returns statistics about the processor's job counts.
64+
#
65+
# - `c`: call count / completed count.
66+
def statistics
67+
{c: [@call_count, @complete_count]}
68+
end
5469
end
5570
end
5671
end

test/async/job/processor/aggregate.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ def call(job)
4646
processor.call(:job1)
4747
processor.call(:job2)
4848

49-
# Give the processor a chance to process jobs
50-
sleep(0.1)
51-
5249
expect(delegate.called_jobs).to be(:include?, :job1)
5350
expect(delegate.called_jobs).to be(:include?, :job2)
5451
end
@@ -58,8 +55,6 @@ def call(job)
5855

5956
error_processor = subject.new(error_delegate)
6057
error_processor.call(:job1)
61-
# Give the processor a chance to process jobs and handle error
62-
sleep(0.1)
6358

6459
# Assert that the error was logged
6560
expect_console.to have_logged(

test/async/job/processor/inline.rb

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,15 @@ def call(job)
4949
job = {id: 1, data: "test"}
5050
processor.call(job)
5151

52-
# Wait a bit for async processing
53-
sleep(0.1)
54-
5552
expect(delegate.called).to be == true
5653
expect(delegate.job).to be == job
5754
end
5855

59-
it "handles scheduled jobs" do
56+
it "handles scheduled jobs" do
6057
scheduled_time = Time.now + 0.05
6158
job = {id: 1, scheduled_at: scheduled_time}
6259

6360
start_time = Time.now
64-
processor.call(job)
65-
66-
# Wait for processing to complete
67-
sleep(0.1)
68-
69-
expect(delegate.called).to be == true
70-
# The job should have been delayed by at least 0.05 seconds
71-
expect(Time.now - start_time).to be >= 0.05
72-
end
73-
74-
it "calls sleep for scheduled jobs" do
75-
scheduled_time = Time.now + 0.1
76-
job = {id: 1, "scheduled_at" => scheduled_time}
77-
78-
# Mock the sleep method to ensure it's called
79-
expect(processor).to receive(:sleep).and_return(nil)
80-
8161
processor.call(job).wait
8262

8363
expect(delegate.called).to be == true
@@ -92,9 +72,6 @@ def call(job)
9272
# Should not raise exception
9373
expect{error_processor.call(job)}.not.to raise_exception
9474

95-
# Wait for async processing
96-
sleep(0.1)
97-
9875
# Assert that the error was logged
9976
expect_console.to have_logged(
10077
severity: be == :error,
@@ -113,4 +90,16 @@ def call(job)
11390
expect(delegate.stopped).to be == true
11491
expect(result).to be == "stopped"
11592
end
93+
94+
with "#statistics" do
95+
it "returns statistics with call and complete counts" do
96+
expect(processor.statistics).to be == {c: [0, 0]}
97+
98+
# Call the processor to increment counts
99+
job = {id: 1, data: "test"}
100+
processor.call(job)
101+
102+
expect(processor.statistics).to be == {c: [1, 1]}
103+
end
104+
end
116105
end

0 commit comments

Comments
 (0)