Skip to content

Commit

Permalink
Added test for the missing ::Async::Reactor.run.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jul 26, 2023
1 parent 0720377 commit c2bc038
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 37 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AllCops:
SuggestExtensions: false
TargetRubyVersion: 2.6
NewCops: enable

Metrics:
Enabled: false
Expand Down
18 changes: 7 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-03-02 20:56:34 UTC using RuboCop version 1.47.0.
# on 2023-07-26 19:57:49 UTC using RuboCop version 1.47.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -77,16 +77,6 @@ Style/GlobalStdStream:
- 'spec/database_adapters/activerecord/activerecord.rb'
- 'tasks/db.rake'

# Offense count: 18
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, EnforcedShorthandSyntax, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
# SupportedShorthandSyntax: always, never, either, consistent
Style/HashSyntax:
Exclude:
- 'lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb'
- 'lib/slack-ruby-bot-server/models/team/methods.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/HashTransformKeys:
Expand All @@ -99,6 +89,12 @@ Style/HashTransformValues:
Exclude:
- 'lib/slack-ruby-bot-server/api/helpers/error_helpers.rb'

# Offense count: 2
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/MapToHash:
Exclude:
- 'spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle, Autocorrect.
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

case ENV['DATABASE_ADAPTER']
case ENV.fetch('DATABASE_ADAPTER', nil)
when 'mongoid' then
gem 'kaminari-mongoid'
gem 'mongoid', ENV['MONGOID_VERSION'] || '~> 7.3.0'
Expand All @@ -14,7 +14,7 @@ when 'activerecord' then
when nil
warn "Missing ENV['DATABASE_ADAPTER']."
else
warn "Invalid ENV['DATABASE_ADAPTER']: #{ENV['DATABASE_ADAPTER']}."
warn "Invalid ENV['DATABASE_ADAPTER']: #{ENV.fetch('DATABASE_ADAPTER', nil)}."
end

gemspec
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'rspec/core'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb'].exclude(%r{ext\/(?!#{ENV['DATABASE_ADAPTER']})})
spec.pattern = FileList['spec/**/*_spec.rb'].exclude(%r{ext\/(?!#{ENV.fetch('DATABASE_ADAPTER', nil)})})
end

require 'rubocop/rake_task'
Expand Down
4 changes: 2 additions & 2 deletions lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class TeamsEndpoint < Grape::API
raise 'Missing SLACK_CLIENT_ID or SLACK_CLIENT_SECRET.' unless ENV.key?('SLACK_CLIENT_ID') && ENV.key?('SLACK_CLIENT_SECRET')

options = {
client_id: ENV['SLACK_CLIENT_ID'],
client_secret: ENV['SLACK_CLIENT_SECRET'],
client_id: ENV.fetch('SLACK_CLIENT_ID', nil),
client_secret: ENV.fetch('SLACK_CLIENT_SECRET', nil),
code: params[:code]
}

Expand Down
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot-server/api/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rack/cors'
require 'rack-rewrite'
require 'rack-server-pages'
require 'otr-activerecord' if SlackRubyBotServer::Config.activerecord? && !defined?(::Rails)
require 'otr-activerecord' if SlackRubyBotServer::Config.activerecord? && !defined?(Rails)

module SlackRubyBotServer
module Api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def self.init!
end
end

::Boolean = Grape::API::Boolean
Boolean = Grape::API::Boolean
5 changes: 3 additions & 2 deletions lib/slack-ruby-bot-server/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def start_from_database!
start_intervals!
end

def start_intervals!
::Async::Reactor.run do
def start_intervals!(&_block)
::Async::Reactor.run do |task|
@intervals.each_pair do |period, calls_with_options|
calls_with_options.each do |call_with_options|
call, options = *call_with_options
Expand All @@ -88,6 +88,7 @@ def start_intervals!
end
end
end
yield task if block_given?
end
end

Expand Down
1 change: 1 addition & 0 deletions slack-ruby-bot-server.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
spec.add_dependency 'rack-rewrite'
spec.add_dependency 'rack-server-pages'
spec.add_dependency 'slack-ruby-client'
spec.metadata['rubygems_mfa_required'] = 'true'
end
4 changes: 2 additions & 2 deletions spec/database_adapters/activerecord/activerecord.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
yml = ERB.new(File.read(File.expand_path('config/postgresql.yml', __dir__))).result
db_config = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
::YAML.safe_load(yml, aliases: true)[ENV['RACK_ENV']]
YAML.safe_load(yml, aliases: true)[ENV.fetch('RACK_ENV', nil)]
else
::YAML.safe_load(yml, [], [], true)[ENV['RACK_ENV']]
YAML.safe_load(yml, [], [], true)[ENV.fetch('RACK_ENV', nil)]
end
ActiveRecord::Tasks::DatabaseTasks.create(db_config)
ActiveRecord::Base.establish_connection(db_config)
Expand Down
2 changes: 1 addition & 1 deletion spec/database_adapters/mongoid/mongoid.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Mongo::Logger.logger.level = Logger::INFO
Mongoid.load!(File.expand_path('config/mongoid.yml', __dir__), ENV['RACK_ENV'])
Mongoid.load!(File.expand_path('config/mongoid.yml', __dir__), ENV.fetch('RACK_ENV', nil))
17 changes: 5 additions & 12 deletions spec/slack-ruby-bot-server/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@
let(:instance) { SlackRubyBotServer::Service.instance }
context 'without timers' do
it 'noop' do
Async::Reactor.run do |task|
instance.start_intervals!
task.stop
end
instance.start_intervals!(&:stop)
expect(instance.instance_variable_get(:@intervals).keys).to eq []
end
end
Expand Down Expand Up @@ -94,8 +91,7 @@
end
end
it 'sets up timers' do
Async::Reactor.run do |task|
instance.start_intervals!
instance.start_intervals! do |task|
task.sleep 3
task.stop
end
Expand All @@ -115,8 +111,7 @@
end
end
it 'does not abort all timers on failure of the first one' do
Async::Reactor.run do |task|
instance.start_intervals!
instance.start_intervals! do |task|
task.sleep 3
task.stop
end
Expand All @@ -132,8 +127,7 @@
end
end
it 'runs the timer once within 3 seconds' do
Async::Reactor.run do |task|
instance.start_intervals!
instance.start_intervals! do |task|
task.sleep 3
task.stop
end
Expand All @@ -148,8 +142,7 @@
end
end
it 'runs the timer exactly twice within 3 seconds' do
Async::Reactor.run do |task|
instance.start_intervals!
instance.start_intervals! do |task|
task.sleep 3
task.stop
end
Expand Down
4 changes: 2 additions & 2 deletions spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
models_ids.concat(response.map { |instance| instance._links.self._url.gsub("http://example.org/api/#{model_ps}/", '') })
break unless response._links[:next]

next_cursor = Hash[CGI.parse(URI.parse(response._links.next._url).query).map { |a| [a[0], a[1][0]] }]
next_cursor = CGI.parse(URI.parse(response._links.next._url).query).map { |a| [a[0], a[1][0]] }.to_h
end
expect(models_ids.uniq.count).to eq model.all.count
end
Expand All @@ -48,7 +48,7 @@
models_ids.concat(response.map { |instance| instance._links.self._url.gsub("http://example.org/api/#{model_ps}/", '') })
break unless response._links[:next]

next_cursor = Hash[CGI.parse(URI.parse(response._links.next._url).query).map { |a| [a[0], a[1][0]] }]
next_cursor = CGI.parse(URI.parse(response._links.next._url).query).map { |a| [a[0], a[1][0]] }.to_h
end
expect(models_ids.uniq.count).to eq model.all.count - 3
end
Expand Down

0 comments on commit c2bc038

Please sign in to comment.