abstractivator 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/abstractivator/fiber_defer.rb +37 -0
 - data/lib/abstractivator/version.rb +1 -1
 - data/spec/lib/abstractivator/fiber_defer_spec.rb +74 -0
 - metadata +4 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 6544ed986ae721bbafe6a7ca12bd302808b64497
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 826dbb6f5c26ef72f77c9117d6bc26c10492ad31
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 69486ac9eb226283b82b1bbdfcd1dc296a73d50079a3da84fc7a601220dc688eabe12b23980ef702aa644da18bc3b8ebb3cd9feae8c8263decdc557514641271
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 7e921ab9be7d6cc07effa8e5b9311494f4d266ee10f8d02f658026a66762b80caec81db52256b8ce496bcdc0e019941aeaa52821080c5da6b452321d6731c3d5
         
     | 
| 
         @@ -0,0 +1,37 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'fiber'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            begin
         
     | 
| 
      
 4 
     | 
    
         
            +
              require 'eventmachine'
         
     | 
| 
      
 5 
     | 
    
         
            +
            rescue => e
         
     | 
| 
      
 6 
     | 
    
         
            +
              raise 'Abstractivator::FiberDefer requires eventmachine but it is unavailable.'
         
     | 
| 
      
 7 
     | 
    
         
            +
            end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            module Abstractivator
         
     | 
| 
      
 10 
     | 
    
         
            +
              module FiberDefer
         
     | 
| 
      
 11 
     | 
    
         
            +
                ROOT_FIBER = Fiber.current
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                def with_fiber_defer(&block)
         
     | 
| 
      
 14 
     | 
    
         
            +
                  raise 'this method requires an eventmachine reactor to be running' unless EM.reactor_running?
         
     | 
| 
      
 15 
     | 
    
         
            +
                  Fiber.new{block.call}.resume if block
         
     | 
| 
      
 16 
     | 
    
         
            +
                end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                def fiber_defer(&action)
         
     | 
| 
      
 19 
     | 
    
         
            +
                  f = Fiber.current
         
     | 
| 
      
 20 
     | 
    
         
            +
                  raise 'fiber_defer must be passed an action to defer (the block)' unless action
         
     | 
| 
      
 21 
     | 
    
         
            +
                  raise 'fiber_defer must be called within a with_fiber_defer block' if f == ROOT_FIBER
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                  safe_action = proc do
         
     | 
| 
      
 24 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 25 
     | 
    
         
            +
                      [action.call, nil]
         
     | 
| 
      
 26 
     | 
    
         
            +
                    rescue Exception => e
         
     | 
| 
      
 27 
     | 
    
         
            +
                      [nil, e]
         
     | 
| 
      
 28 
     | 
    
         
            +
                    end
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                  EM.defer(safe_action, proc { |result, error| f.resume([result, error]) })
         
     | 
| 
      
 32 
     | 
    
         
            +
                  result, error = Fiber.yield
         
     | 
| 
      
 33 
     | 
    
         
            +
                  raise error if error
         
     | 
| 
      
 34 
     | 
    
         
            +
                  result
         
     | 
| 
      
 35 
     | 
    
         
            +
                end
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,74 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'rspec'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'abstractivator/fiber_defer'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'eventmachine'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            describe Abstractivator::FiberDefer do
         
     | 
| 
      
 6 
     | 
    
         
            +
              include Abstractivator::FiberDefer
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
              describe '#with_fiber_defer' do
         
     | 
| 
      
 9 
     | 
    
         
            +
                context 'when an eventmachine reactor is not running' do
         
     | 
| 
      
 10 
     | 
    
         
            +
                  it 'raises an error' do
         
     | 
| 
      
 11 
     | 
    
         
            +
                    expect{with_fiber_defer}.to raise_error /reactor/
         
     | 
| 
      
 12 
     | 
    
         
            +
                  end
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
                context 'when an eventmachine reactor is running' do
         
     | 
| 
      
 15 
     | 
    
         
            +
                  it 'calls the block' do
         
     | 
| 
      
 16 
     | 
    
         
            +
                    EM.run do
         
     | 
| 
      
 17 
     | 
    
         
            +
                      expect{|b| with_fiber_defer(&b)}.to yield_control
         
     | 
| 
      
 18 
     | 
    
         
            +
                      EM.stop
         
     | 
| 
      
 19 
     | 
    
         
            +
                    end
         
     | 
| 
      
 20 
     | 
    
         
            +
                  end
         
     | 
| 
      
 21 
     | 
    
         
            +
                  context 'when no block is provided' do
         
     | 
| 
      
 22 
     | 
    
         
            +
                    it 'does nothing' do
         
     | 
| 
      
 23 
     | 
    
         
            +
                      EM.run do
         
     | 
| 
      
 24 
     | 
    
         
            +
                        with_fiber_defer
         
     | 
| 
      
 25 
     | 
    
         
            +
                        EM.stop
         
     | 
| 
      
 26 
     | 
    
         
            +
                      end
         
     | 
| 
      
 27 
     | 
    
         
            +
                    end
         
     | 
| 
      
 28 
     | 
    
         
            +
                  end
         
     | 
| 
      
 29 
     | 
    
         
            +
                end
         
     | 
| 
      
 30 
     | 
    
         
            +
              end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
              describe '#fiber_defer' do
         
     | 
| 
      
 33 
     | 
    
         
            +
                context 'when it is called outside a with_fiber_defer block' do
         
     | 
| 
      
 34 
     | 
    
         
            +
                  it 'raises an error' do
         
     | 
| 
      
 35 
     | 
    
         
            +
                    expect{fiber_defer{}}.to raise_error /with_fiber_defer/
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
                context 'when it is not passed a block' do
         
     | 
| 
      
 39 
     | 
    
         
            +
                  it 'raises an error' do
         
     | 
| 
      
 40 
     | 
    
         
            +
                    expect{fiber_defer}.to raise_error /must be passed an action/
         
     | 
| 
      
 41 
     | 
    
         
            +
                  end
         
     | 
| 
      
 42 
     | 
    
         
            +
                end
         
     | 
| 
      
 43 
     | 
    
         
            +
                it 'executes the block on a background thread' do
         
     | 
| 
      
 44 
     | 
    
         
            +
                  EM.run do
         
     | 
| 
      
 45 
     | 
    
         
            +
                    with_fiber_defer do
         
     | 
| 
      
 46 
     | 
    
         
            +
                      main_thread = Thread.current
         
     | 
| 
      
 47 
     | 
    
         
            +
                      executed = false
         
     | 
| 
      
 48 
     | 
    
         
            +
                      fiber_defer do
         
     | 
| 
      
 49 
     | 
    
         
            +
                        expect(Thread.current).to_not eql main_thread
         
     | 
| 
      
 50 
     | 
    
         
            +
                        executed = true
         
     | 
| 
      
 51 
     | 
    
         
            +
                      end
         
     | 
| 
      
 52 
     | 
    
         
            +
                      expect(executed).to be true
         
     | 
| 
      
 53 
     | 
    
         
            +
                      EM.stop
         
     | 
| 
      
 54 
     | 
    
         
            +
                    end
         
     | 
| 
      
 55 
     | 
    
         
            +
                  end
         
     | 
| 
      
 56 
     | 
    
         
            +
                end
         
     | 
| 
      
 57 
     | 
    
         
            +
                it 'returns the value of its block' do
         
     | 
| 
      
 58 
     | 
    
         
            +
                  EM.run do
         
     | 
| 
      
 59 
     | 
    
         
            +
                    with_fiber_defer do
         
     | 
| 
      
 60 
     | 
    
         
            +
                      expect(fiber_defer{42}).to eql 42
         
     | 
| 
      
 61 
     | 
    
         
            +
                      EM.stop
         
     | 
| 
      
 62 
     | 
    
         
            +
                    end
         
     | 
| 
      
 63 
     | 
    
         
            +
                  end
         
     | 
| 
      
 64 
     | 
    
         
            +
                end
         
     | 
| 
      
 65 
     | 
    
         
            +
                it 'raises an error raised by its block' do
         
     | 
| 
      
 66 
     | 
    
         
            +
                  EM.run do
         
     | 
| 
      
 67 
     | 
    
         
            +
                    with_fiber_defer do
         
     | 
| 
      
 68 
     | 
    
         
            +
                      expect{fiber_defer{raise 'oops'}}.to raise_error 'oops'
         
     | 
| 
      
 69 
     | 
    
         
            +
                      EM.stop
         
     | 
| 
      
 70 
     | 
    
         
            +
                    end
         
     | 
| 
      
 71 
     | 
    
         
            +
                  end
         
     | 
| 
      
 72 
     | 
    
         
            +
                end
         
     | 
| 
      
 73 
     | 
    
         
            +
              end
         
     | 
| 
      
 74 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: abstractivator
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.2.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Peter Winton
         
     | 
| 
         @@ -101,6 +101,7 @@ files: 
     | 
|
| 
       101 
101 
     | 
    
         
             
            - lib/abstractivator/enum.rb
         
     | 
| 
       102 
102 
     | 
    
         
             
            - lib/abstractivator/enumerable_ext.rb
         
     | 
| 
       103 
103 
     | 
    
         
             
            - lib/abstractivator/event.rb
         
     | 
| 
      
 104 
     | 
    
         
            +
            - lib/abstractivator/fiber_defer.rb
         
     | 
| 
       104 
105 
     | 
    
         
             
            - lib/abstractivator/proc_ext.rb
         
     | 
| 
       105 
106 
     | 
    
         
             
            - lib/abstractivator/schedule.rb
         
     | 
| 
       106 
107 
     | 
    
         
             
            - lib/abstractivator/schedule/schedule.rb
         
     | 
| 
         @@ -118,6 +119,7 @@ files: 
     | 
|
| 
       118 
119 
     | 
    
         
             
            - spec/lib/abstractivator/enum_spec.rb
         
     | 
| 
       119 
120 
     | 
    
         
             
            - spec/lib/abstractivator/enumerable_ext_spec.rb
         
     | 
| 
       120 
121 
     | 
    
         
             
            - spec/lib/abstractivator/event_spec.rb
         
     | 
| 
      
 122 
     | 
    
         
            +
            - spec/lib/abstractivator/fiber_defer_spec.rb
         
     | 
| 
       121 
123 
     | 
    
         
             
            - spec/lib/abstractivator/proc_ext_spec.rb
         
     | 
| 
       122 
124 
     | 
    
         
             
            - spec/lib/abstractivator/schedule/em_util.rb
         
     | 
| 
       123 
125 
     | 
    
         
             
            - spec/lib/abstractivator/schedule/schedule_runner_spec.rb
         
     | 
| 
         @@ -157,6 +159,7 @@ test_files: 
     | 
|
| 
       157 
159 
     | 
    
         
             
            - spec/lib/abstractivator/enum_spec.rb
         
     | 
| 
       158 
160 
     | 
    
         
             
            - spec/lib/abstractivator/enumerable_ext_spec.rb
         
     | 
| 
       159 
161 
     | 
    
         
             
            - spec/lib/abstractivator/event_spec.rb
         
     | 
| 
      
 162 
     | 
    
         
            +
            - spec/lib/abstractivator/fiber_defer_spec.rb
         
     | 
| 
       160 
163 
     | 
    
         
             
            - spec/lib/abstractivator/proc_ext_spec.rb
         
     | 
| 
       161 
164 
     | 
    
         
             
            - spec/lib/abstractivator/schedule/em_util.rb
         
     | 
| 
       162 
165 
     | 
    
         
             
            - spec/lib/abstractivator/schedule/schedule_runner_spec.rb
         
     |