-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f52cd27
commit 16fda90
Showing
8 changed files
with
213 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ gem "rake", "~> 13.0" | |
gem "rspec", "~> 3.0" | ||
|
||
gem "rubocop", "~> 1.21" | ||
|
||
gem "async-rspec", "~> 1.17" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
async-conditions_list (0.1.0) | ||
async (~> 2.17) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
ast (2.4.2) | ||
async (2.17.0) | ||
console (~> 1.26) | ||
fiber-annotation | ||
io-event (~> 1.6, >= 1.6.5) | ||
async-rspec (1.17.0) | ||
rspec (~> 3.0) | ||
rspec-files (~> 1.0) | ||
rspec-memory (~> 1.0) | ||
console (1.27.0) | ||
fiber-annotation | ||
fiber-local (~> 1.1) | ||
json | ||
diff-lcs (1.5.1) | ||
fiber-annotation (0.2.0) | ||
fiber-local (1.1.0) | ||
fiber-storage | ||
fiber-storage (1.0.0) | ||
io-event (1.6.5) | ||
json (2.7.2) | ||
language_server-protocol (3.17.0.3) | ||
parallel (1.26.3) | ||
parser (3.3.5.0) | ||
ast (~> 2.4.1) | ||
racc | ||
racc (1.8.1) | ||
rainbow (3.1.1) | ||
rake (13.2.1) | ||
regexp_parser (2.9.2) | ||
rspec (3.13.0) | ||
rspec-core (~> 3.13.0) | ||
rspec-expectations (~> 3.13.0) | ||
rspec-mocks (~> 3.13.0) | ||
rspec-core (3.13.1) | ||
rspec-support (~> 3.13.0) | ||
rspec-expectations (3.13.3) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.13.0) | ||
rspec-files (1.1.3) | ||
rspec (~> 3.0) | ||
rspec-memory (1.0.4) | ||
rspec (~> 3.0) | ||
rspec-mocks (3.13.2) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.13.0) | ||
rspec-support (3.13.1) | ||
rubocop (1.66.1) | ||
json (~> 2.3) | ||
language_server-protocol (>= 3.17.0) | ||
parallel (~> 1.10) | ||
parser (>= 3.3.0.2) | ||
rainbow (>= 2.2.2, < 4.0) | ||
regexp_parser (>= 2.4, < 3.0) | ||
rubocop-ast (>= 1.32.2, < 2.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (>= 2.4.0, < 3.0) | ||
rubocop-ast (1.32.3) | ||
parser (>= 3.3.1.0) | ||
ruby-progressbar (1.13.0) | ||
unicode-display_width (2.6.0) | ||
|
||
PLATFORMS | ||
arm64-darwin-23 | ||
ruby | ||
|
||
DEPENDENCIES | ||
async-conditions_list! | ||
async-rspec (~> 1.17) | ||
rake (~> 13.0) | ||
rspec (~> 3.0) | ||
rubocop (~> 1.21) | ||
|
||
BUNDLED WITH | ||
2.5.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "conditions_list/version" | ||
require "async" | ||
|
||
module Async | ||
module ConditionsList | ||
# The ConditionsList class manages a collection of Async::Condition objects, | ||
# allowing you to wait on multiple conditions and get accumulated result. | ||
# | ||
# == Initialization | ||
# | ||
# To create a new ConditionsList, specify the number of conditions you want to manage: | ||
# | ||
# conditions_list = Async::ConditionsList.new(size) | ||
# | ||
# == Methods | ||
# | ||
# - `wait`: Waits on all conditions in the list. | ||
# | ||
# - `signal(value = nil)`: Signals the current condition being waited on. | ||
# If no condition has been waited on yet, it raises `ConditionsList::Error`. | ||
# | ||
# == Example Usage | ||
# | ||
# require 'async' | ||
# require 'async/conditions_list' | ||
# | ||
# conditions_list = Async::ConditionsList.new(3) | ||
# | ||
# Sync do |task| | ||
# task.async do | ||
# conditions_list.wait | ||
# puts "All conditions have been met!" | ||
# end | ||
# | ||
# # Simulate asynchronous events | ||
# task.sleep(1) | ||
# conditions_list.signal | ||
# task.sleep(1) | ||
# conditions_list.signal | ||
# task.sleep(1) | ||
# conditions_list.signal | ||
# end | ||
# | ||
# == Error Handling | ||
# | ||
# - Raises `ConditionsList::Error` if `signal` is called before any condition has been waited on. | ||
# | ||
# == Notes | ||
# | ||
# - Ensure that the `wait` method is called before `signal` to avoid errors. | ||
# - This class is intended to be used within the `Async` scheduler. | ||
# | ||
class ConditionsList | ||
class Error < StandardError; end | ||
# Your code goes here... | ||
|
||
def initialize(size) | ||
@list = Array.new(size) { Async::Condition.new } | ||
@current_condition = nil | ||
end | ||
|
||
def wait | ||
@list.map do |condition| | ||
@current_condition = condition | ||
condition.wait | ||
end | ||
end | ||
|
||
def signal(value = nil) | ||
raise Error, "No condition has been waited on yet." unless @current_condition | ||
|
||
@current_condition.signal(value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Async | ||
module ConditionsList | ||
class ConditionsList | ||
VERSION = "0.1.0" | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require "async" | ||
require "async/conditions_list" | ||
require "async/rspec" | ||
|
||
RSpec.describe Async::ConditionsList do | ||
it "has a version number" do | ||
expect(Async::ConditionsList::VERSION).not_to be nil | ||
include_context Async::RSpec::Reactor | ||
|
||
let(:size) { 3 } | ||
let(:conditions_list) { Async::ConditionsList.new(size) } | ||
|
||
it "waits on all conditions in the list and returns composed result" do | ||
reactor.async do |task| | ||
wait_task = task.async { conditions_list.wait } | ||
|
||
size.times { |i| conditions_list.signal(i) } | ||
|
||
result = wait_task.wait | ||
|
||
expect(result).to eq([0, 1, 2]) | ||
end | ||
end | ||
|
||
it "does something useful" do | ||
expect(false).to eq(true) | ||
describe "#signal" do | ||
context "when no condition has been waited on" do | ||
it "raises an Error" do | ||
expect do | ||
conditions_list.signal | ||
end.to raise_error(Async::ConditionsList::Error, "No condition has been waited on yet.") | ||
end | ||
end | ||
end | ||
end |