lock_method 0.5.1 → 0.5.2
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.
- data/CHANGELOG +6 -0
- data/lib/lock_method/lock.rb +7 -3
- data/lib/lock_method/version.rb +1 -1
- data/lib/lock_method.rb +4 -4
- data/test/helper.rb +9 -2
- data/test/shared_tests.rb +22 -0
- metadata +1 -1
    
        data/CHANGELOG
    CHANGED
    
    
    
        data/lib/lock_method/lock.rb
    CHANGED
    
    | @@ -36,15 +36,19 @@ module LockMethod | |
| 36 36 | 
             
                attr_reader :obj
         | 
| 37 37 | 
             
                attr_reader :method_id
         | 
| 38 38 | 
             
                attr_reader :args
         | 
| 39 | 
            +
                attr_reader :blk
         | 
| 40 | 
            +
                attr_reader :original_method_id
         | 
| 39 41 |  | 
| 40 | 
            -
                def initialize(obj, method_id, options = {})
         | 
| 42 | 
            +
                def initialize(obj, method_id, options = {}, &blk)
         | 
| 41 43 | 
             
                  @mutex = ::Mutex.new
         | 
| 42 44 | 
             
                  @obj = obj
         | 
| 43 45 | 
             
                  @method_id = method_id
         | 
| 46 | 
            +
                  @blk = blk
         | 
| 44 47 | 
             
                  options = options.symbolize_keys
         | 
| 45 48 | 
             
                  @ttl = options[:ttl]
         | 
| 46 49 | 
             
                  @args = options[:args]
         | 
| 47 50 | 
             
                  @spin = options[:spin]
         | 
| 51 | 
            +
                  @original_method_id = options[:original_method_id]
         | 
| 48 52 | 
             
                end
         | 
| 49 53 |  | 
| 50 54 | 
             
                def spin?
         | 
| @@ -99,7 +103,7 @@ module LockMethod | |
| 99 103 | 
             
                  # nothing
         | 
| 100 104 | 
             
                end
         | 
| 101 105 |  | 
| 102 | 
            -
                def call_and_lock | 
| 106 | 
            +
                def call_and_lock
         | 
| 103 107 | 
             
                  while locked? and spin?
         | 
| 104 108 | 
             
                    ::Kernel.sleep 0.5
         | 
| 105 109 | 
             
                  end
         | 
| @@ -108,7 +112,7 @@ module LockMethod | |
| 108 112 | 
             
                  else
         | 
| 109 113 | 
             
                    begin
         | 
| 110 114 | 
             
                      save
         | 
| 111 | 
            -
                      obj.send(* | 
| 115 | 
            +
                      obj.send(*([original_method_id]+args), &blk)
         | 
| 112 116 | 
             
                    ensure
         | 
| 113 117 | 
             
                      delete
         | 
| 114 118 | 
             
                    end
         | 
    
        data/lib/lock_method/version.rb
    CHANGED
    
    
    
        data/lib/lock_method.rb
    CHANGED
    
    | @@ -64,10 +64,10 @@ module LockMethod | |
| 64 64 | 
             
                  end
         | 
| 65 65 | 
             
                  original_method_id = "_unlocked_#{method_id}"
         | 
| 66 66 | 
             
                  alias_method original_method_id, method_id
         | 
| 67 | 
            -
                  define_method method_id do |*args1|
         | 
| 68 | 
            -
                     | 
| 69 | 
            -
                    lock = ::LockMethod::Lock.new self, method_id,  | 
| 70 | 
            -
                    lock.call_and_lock | 
| 67 | 
            +
                  define_method method_id do |*args1, &blk|
         | 
| 68 | 
            +
                    options1 = options.merge(:args => args1, :original_method_id => original_method_id)
         | 
| 69 | 
            +
                    lock = ::LockMethod::Lock.new self, method_id, options1, &blk
         | 
| 70 | 
            +
                    lock.call_and_lock
         | 
| 71 71 | 
             
                  end
         | 
| 72 72 | 
             
                end
         | 
| 73 73 | 
             
              end
         | 
    
        data/test/helper.rb
    CHANGED
    
    
    
        data/test/shared_tests.rb
    CHANGED
    
    | @@ -206,4 +206,26 @@ module SharedTests | |
| 206 206 |  | 
| 207 207 | 
             
                assert_equal 'danke schoen', BlogSpin.get_latest_entries
         | 
| 208 208 | 
             
              end
         | 
| 209 | 
            +
             | 
| 210 | 
            +
              def test_15_block
         | 
| 211 | 
            +
                blocker = Thread.new do
         | 
| 212 | 
            +
                  BlogBlock.get_latest_entries { $stderr.write "i'm in the way!" }
         | 
| 213 | 
            +
                end
         | 
| 214 | 
            +
                
         | 
| 215 | 
            +
                # give it a bit of time to lock
         | 
| 216 | 
            +
                sleep 1
         | 
| 217 | 
            +
                
         | 
| 218 | 
            +
                # the blocker won't have finished
         | 
| 219 | 
            +
                assert_raises(LockMethod::Locked) do
         | 
| 220 | 
            +
                  BlogBlock.get_latest_entries { $stderr.write "don't print me" }
         | 
| 221 | 
            +
                end
         | 
| 222 | 
            +
                
         | 
| 223 | 
            +
                # wait to finish
         | 
| 224 | 
            +
                blocker.join
         | 
| 225 | 
            +
                
         | 
| 226 | 
            +
                # now we're sure
         | 
| 227 | 
            +
                assert_nothing_raised do
         | 
| 228 | 
            +
                  BlogBlock.get_latest_entries { $stderr.write "i'm now allowed" }
         | 
| 229 | 
            +
                end
         | 
| 230 | 
            +
              end
         | 
| 209 231 | 
             
            end
         |