Agilefreaks / Aquarium

An AOP library for Ruby
https://rubygems.org/gems/aquarium/
148 stars 24 forks source link

Using Aquarium to spec asyncronous web applications. #1

Open hedgehog opened 14 years ago

hedgehog commented 14 years ago

I am not sure this application can be accomodated but I thought to bring this use case to your attention.

Context: Exploring using Aquarium to write RSpec examples of async_sinatra behavior.

In sinatra/base.rb you'll see something like this:

        define_method "#{verb} #{path}", &block if block_given?
        unbound_method = instance_method("#{verb} #{path}") 
        block =
          if block.arity != 0
            proc { unbound_method.bind(self).call(*@block_params) }
          else
            proc { unbound_method.bind(self).call }
          end

I see this error when I try to replicate the method_tracing_example_spec.rb

`@@_aspect_class_advice_chain_ASSpec_HEAD _slash_foo' is not allowed as a class variable name

The spec helper file:

$stdout.sync=true
require 'eventmachine'
dir = "/usr/src/"
require dir + 'em-spec/lib/em-spec/rspec' # or 'bacon' or 'test'
require dir + 'em-spec/lib/ext/fiber18'
require dir + 'sinatra/lib/sinatra/base'
require dir + 'async_sinatra/lib/sinatra/async'
# require dir + 'async_sinatra/spec/as_server'
require 'aquarium'

include Aquarium::Aspects

module Sinatra
  module Async

    module SpecHelper
#      include ::Sinatra::Async::SpecServer
      include ::EM::SpecHelper

    end
  end
end

Spec::Runner.configure do |config|
  include ::Sinatra::Async::SpecHelper
end

The spec file:

dir = File.expand_path(File.dirname(__FILE__))
require dir + '/as_spec_helper'
module Sinatra
  describe Async, "routes when testing with EM::SpecHelper" do

  default_timeout 400

    class ::ASSpec < ::Sinatra::Base
      register ::Sinatra::Async
      disable :raise_errors, :show_exceptions

      def log message
        @log ||= []
        @log << message
      end
      def logged_messages
        @log
      end

      aget '/foo' do
        body "hello from aget"
      end
    end

    before(:all) do
#      @server=HTTPServer.new(ASSpec)
#      @wr_session=Webrat::Session.new  # @wr_session gets clobbered somewhere in asinatra, possibly em.
    end

    it "should still work as usual" do
      aspect = ::Aquarium::Aspects::Aspect.new :around, :invocations_of => :all_methods,
        :for_type => ::ASSpec, :restricting_methods_to => :exclude_ancestor_methods do |execution_point, obj, *args|
        begin
          obj.log "Entering: #{execution_point.target_type.name}##{execution_point.method_name}: args = #{args.inspect}"
          execution_point.proceed
        ensure
          obj.log "Leaving: #{execution_point.target_type.name}##{execution_point.method_name}: args = #{args.inspect}"
        end
      end
      ::Sinatra.new(::ASSpec)
    end

#    it "should still work as usual" do
#      as_app = asinatra(ASSpec) do
#        visit 'http://0.0.0.0:4000/foo'
#      end
#      #resp.body.should == "hello from aget"
#    end
#
#    it "should not require a call to done when #em is not used" do
#        1.should == 1
#    end
#
#    it "should have timers" do
#      em do
#        start = Time.now
#
#        EM.add_timer(0.5){
#          (Time.now-start).should be_close( 0.5, 0.1 )
#          done
#        }
#      end
#    end
  end
end

deanwampler commented 14 years ago

It looks like there is a space in the method name. Could you confirm that you didn't introduce the space accidentally? If not, I'll see what I can figure out.

hedgehog commented 14 years ago

Yes there is a space. I haven't worked out why but this does seem to be intentional by the Sinatra community - I'm just starting to get to grips with their code.

deanwampler commented 14 years ago

Oh, interesting. I guess I could handle spaces in names, too! I'll add that to my list...