Sha256: d16c484433cbd60d440e1b4f8c79e15e1ad459be022f3a694a306970c4529ea8

Contents?: true

Size: 1.79 KB

Versions: 104

Compression:

Stored size: 1.79 KB

Contents

require 'thread_safe/synchronized_delegator.rb'

module ThreadSafe
  describe SynchronizedDelegator do 
    it 'wraps array' do
      array = ::Array.new
      sync_array = described_class.new(array)

      array << 1
      expect(1).to eq sync_array[0]

      sync_array << 2
      expect(2).to eq array[1]
    end

    it 'synchronizes access' do
      t1_continue, t2_continue = false, false

      hash = ::Hash.new do |hash, key|
        t2_continue = true
        unless hash.find { |e| e[1] == key.to_s } # just to do something
          hash[key] = key.to_s
          Thread.pass until t1_continue
        end
      end
      sync_hash = described_class.new(hash)
      sync_hash[1] = 'egy'

      t1 = Thread.new do
        sync_hash[2] = 'dva'
        sync_hash[3] # triggers t2_continue
      end

      t2 = Thread.new do
        Thread.pass until t2_continue
        sync_hash[4] = '42'
      end

      sleep(0.05) # sleep some to allow threads to boot

      until t2.status == 'sleep' do
        Thread.pass
      end

      expect(3).to eq hash.keys.size

      t1_continue = true
      t1.join; t2.join

      expect(4).to eq sync_hash.size
    end

    it 'synchronizes access with block' do
      t1_continue, t2_continue = false, false

      array = ::Array.new
      sync_array = described_class.new(array)

      t1 = Thread.new do
        sync_array << 1
        sync_array.each do
          t2_continue = true
          Thread.pass until t1_continue
        end
      end

      t2 = Thread.new do
        # sleep(0.01)
        Thread.pass until t2_continue
        sync_array << 2
      end

      until t2.status == 'sleep' || t2.status == false
        Thread.pass
      end

      expect(1).to eq array.size

      t1_continue = true
      t1.join; t2.join

      expect([1, 2]).to eq array
    end
  end
end

Version data entries

104 entries across 98 versions & 30 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/spec/thread_safe/synchronized_delegator_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/spec/thread_safe/synchronized_delegator_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/spec/thread_safe/synchronized_delegator_spec.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/thread_safe-0.3.6/spec/thread_safe/synchronized_delegator_spec.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/thread_safe-0.3.6/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/thread_safe-0.3.6/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/thread_safe/synchronized_delegator_spec.rb