Skip to content

Convert tests to sus. #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion async.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "benchmark-ips"
spec.add_development_dependency "bundler"
spec.add_development_dependency "covered", "~> 0.10"
spec.add_development_dependency "rspec", "~> 3.6"
spec.add_development_dependency "sus", "~> 0.15"
spec.add_development_dependency "sus-fixtures-async"
end
5 changes: 5 additions & 0 deletions config/sus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

require 'covered/sus'
include Covered::Sus

ENV['CONSOLE_LEVEL'] ||= 'fatal'
40 changes: 21 additions & 19 deletions spec/async/condition_examples.rb → fixtures/a_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,66 @@

require 'async/variable'

RSpec.shared_examples Async::Condition do
ACondition = Sus::Shared("a condition") do
let(:condition) {subject.new}

it 'can signal waiting task' do
state = nil

reactor.async do
state = :waiting
subject.wait
condition.wait
state = :resumed
end

expect(state).to be == :waiting

subject.signal
condition.signal

reactor.yield

expect(state).to be == :resumed
end

it 'should be able to signal stopped task' do
expect(subject.empty?).to be_truthy
expect(condition).to be(:empty?)

task = reactor.async do
subject.wait
condition.wait
end

expect(subject.empty?).to be_falsey
expect(condition.empty?).not.to be(:empty?)

task.stop

subject.signal
condition.signal
end

it 'resumes tasks in order' do
order = []

5.times do |i|
task = reactor.async do
subject.wait
condition.wait
order << i
end
end

subject.signal
condition.signal

reactor.yield

expect(order).to be == [0, 1, 2, 3, 4]
end

context "with timeout" do
let!(:ready) {Async::Variable.new(subject)}
let!(:waiting) {Async::Variable.new(described_class.new)}
with "timeout" do
let(:ready) {Async::Variable.new(condition)}
let(:waiting) {Async::Variable.new(subject.new)}

before do
def before
@state = nil
end

let!(:task) do
reactor.async do |task|

@task = reactor.async do |task|
task.with_timeout(0.1) do
begin
@state = :waiting
Expand All @@ -77,10 +77,12 @@
end
end
end

super
end

it 'can timeout while waiting' do
task.wait
@task.wait

expect(@state).to be == :timeout
end
Expand All @@ -89,7 +91,7 @@
waiting.wait
ready.resolve

task.wait
@task.wait

expect(@state).to be == :signalled
end
Expand Down
17 changes: 17 additions & 0 deletions fixtures/chainable_async.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2022, by Samuel Williams.

ChainableAsync = Sus::Shared("chainable async") do
let(:parent) {Object.new}
let(:chainable) {subject.new(parent: parent)}

it 'should chain async to parent' do
expect(parent).to receive(:async).and_return(nil)

chainable.async do
# Nothing.
end
end
end
44 changes: 44 additions & 0 deletions fixtures/timer_quantum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.

class TimerQuantum
def self.resolve
self.new.to_f
end

def to_f
precision
end

private

def precision
@precision ||= self.measure_host_precision
end

def measure_host_precision(repeats: 100, duration: 0.01)
# Measure the precision sleep using the monotonic clock:
start_time = self.now
repeats.times do
sleep(duration)
end
end_time = self.now

actual_duration = end_time - start_time
expected_duration = repeats * duration

if actual_duration < expected_duration
warn "Invalid precision measurement: #{actual_duration} < #{expected_duration}"
return 0.1
end

# This computes the overhead of sleep, called `repeats` times:
return actual_duration - expected_duration
end

def now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end

Q = TIMER_QUANTUM = TimerQuantum.resolve
2 changes: 0 additions & 2 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

gemspec

# gem "event", path: "../event"
# gem "async-rspec", path: "../async-rspec"
# gem "io-event", git: "https://github.com/socketry/io-event.git"

group :maintenance, optional: true do
Expand Down
16 changes: 0 additions & 16 deletions spec/async/chainable_async_examples.rb

This file was deleted.

54 changes: 0 additions & 54 deletions spec/async/clock_spec.rb

This file was deleted.

37 changes: 0 additions & 37 deletions spec/async/variable_spec.rb

This file was deleted.

23 changes: 0 additions & 23 deletions spec/spec_helper.rb

This file was deleted.

22 changes: 0 additions & 22 deletions spec/tempfile_spec.rb

This file was deleted.

Loading