Skip to content

Commit cd7af5f

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

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

lib/async/job/processor/inline.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require_relative "generic"
99

1010
require "async/idler"
11+
require "async/service/formatting"
1112

1213
module Async
1314
module Job
@@ -23,8 +24,14 @@ def initialize(delegate, parent: nil)
2324
super(delegate)
2425

2526
@parent = parent || Async::Idler.new
27+
28+
@call_count = 0
29+
@complete_count = 0
2630
end
2731

32+
# @attribute [Integer] The count of completed jobs.
33+
attr_reader :complete_count
34+
2835
# Process a job asynchronously with optional scheduling.
2936
# If the job has a scheduled_at time, the processor will wait until that time before execution.
3037
# @parameter job [Hash] The job data containing execution details.
@@ -36,7 +43,9 @@ def call(job)
3643
sleep(scheduled_at - Time.now)
3744
end
3845

46+
@call_count += 1
3947
@delegate.call(job)
48+
@complete_count += 1
4049
rescue => error
4150
Console.error(self, error)
4251
end
@@ -51,6 +60,13 @@ def start
5160
def stop
5261
@delegate.stop
5362
end
63+
64+
# Returns statistics about the processor's job counts.
65+
#
66+
# - `c`: call count / completed count.
67+
def statistics
68+
{c: [@call_count, @complete_count]}
69+
end
5470
end
5571
end
5672
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)