Skip to content

Commit

Permalink
[WIP] instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jgraichen committed Mar 3, 2014
1 parent 9e490b5 commit 4005f7b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 15 deletions.
4 changes: 1 addition & 3 deletions Gemfile
Expand Up @@ -4,9 +4,7 @@ source 'https://rubygems.org'
#
gem 'webmock', '~> 1.7'
gem 'rake'
gem 'rspec', '>= 3.0.0.beta2'
gem 'rspec-its'
gem 'rspec-collection_matchers'
gem 'rspec'
gem 'coveralls'
gem 'json', '~> 1.8.1'

Expand Down
1 change: 1 addition & 0 deletions lib/acfs.rb
Expand Up @@ -3,6 +3,7 @@
require 'active_support/core_ext/class'
require 'active_support/core_ext/string'
require 'active_support/core_ext/module'
require 'active_support/notifications'

module Acfs
extend ActiveSupport::Autoload
Expand Down
4 changes: 4 additions & 0 deletions lib/acfs/operation.rb
Expand Up @@ -49,6 +49,10 @@ def method
def request
request = ::Acfs::Request.new url, method: method, params: params, data: data
request.on_complete do |response|
::ActiveSupport::Notifications.instrument 'acfs.operation.complete',
operation: self,
response: response

handle_failure response unless response.success?
callback.call response.data
end
Expand Down
2 changes: 2 additions & 0 deletions lib/acfs/request/callbacks.rb
Expand Up @@ -38,6 +38,8 @@ def callbacks
# @return [ Acfs::Request ] The request itself.
#
def complete!(response)
::ActiveSupport::Notifications.instrument 'acfs.request.complete', request: self

call_callback response, 0
self
end
Expand Down
28 changes: 18 additions & 10 deletions lib/acfs/runner.rb
Expand Up @@ -17,13 +17,17 @@ def initialize(adapter)
# and parallel operations will be queued.
#
def process(op)
op.synchronous? ? run(op) : enqueue(op)
::ActiveSupport::Notifications.instrument 'acfs.runner.process', operation: op do
op.synchronous? ? run(op) : enqueue(op)
end
end

# Run operation right now skipping queue.
#
def run(op)
op_request(op) { |req| adapter.run req }
::ActiveSupport::Notifications.instrument 'acfs.runner.run', operation: op do
op_request(op) { |req| adapter.run req }
end
end

# List of current queued operations.
Expand All @@ -35,10 +39,12 @@ def queue
# Enqueue operation to be run later.
#
def enqueue(op)
if running?
op_request(op) { |req| adapter.queue req }
else
queue << op
::ActiveSupport::Notifications.instrument 'acfs.runner.enqueue', operation: op do
if running?
op_request(op) { |req| adapter.queue req }
else
queue << op
end
end
end

Expand All @@ -51,11 +57,13 @@ def running?
# Start processing queued operations.
#
def start
enqueue_operations
::ActiveSupport::Notifications.instrument 'acfs.runner.start' do
enqueue_operations

@running = true
adapter.start
@running = false
@running = true
adapter.start
@running = false
end
end

def clear
Expand Down
79 changes: 79 additions & 0 deletions spec/acfs/runner_spec.rb
@@ -0,0 +1,79 @@
require 'spec_helper'

class NullAdapter < Acfs::Adapter::Base

# Start processing queued requests.
#
def start
end

# Abort running and queued requests.
#
def abort
end

# Run request right now skipping queue.
#
def run(_)
end

# Enqueue request to be run later.
#
def queue(_)
end
end

class NotificationCollector
def call(*args)
events << ActiveSupport::Notifications::Event.new(*args)
end

def events
@events ||= []
end
end

describe ::Acfs::Runner do
let(:adapter) { ::NullAdapter.new }
let(:runner) { ::Acfs::Runner.new adapter }
let(:collector) { NotificationCollector.new }

after do
::ActiveSupport::Notifications.notifier = \
::ActiveSupport::Notifications::Fanout.new
end

describe '#instrumentation' do
before do
::ActiveSupport::Notifications.subscribe /^acfs\.runner/, collector
end

describe '#start' do
it 'should trigger event' do
runner.start
expect(collector.events).to have(1).items
end
end

describe '#process' do
it 'should trigger event' do
runner.process ::Acfs::Operation.new MyUser, :read, params: {id: 0}
expect(collector.events).to have(2).items
end
end

describe '#run' do
it 'should trigger event' do
runner.run ::Acfs::Operation.new MyUser, :read, params: {id: 0}
expect(collector.events).to have(1).items
end
end

describe '#enqueue' do
it 'should trigger event' do
runner.run ::Acfs::Operation.new MyUser, :read, params: {id: 0}
expect(collector.events).to have(1).items
end
end
end
end
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
@@ -1,6 +1,4 @@
require 'rspec'
require 'rspec/its'
require 'rspec/collection_matchers'
require 'webmock/rspec'

if ENV['CI'] || (defined?(:RUBY_ENGINE) && RUBY_ENGINE != 'rbx')
Expand Down

0 comments on commit 4005f7b

Please sign in to comment.