Sha256: ccd16e1f98733385dcd41b9aee112f85b66bfce8530880c67da9073f54950cc5

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require File.expand_path(File.dirname(__FILE__) + "/spec_helper")

describe "Tsafe" do
  it "should be able to spawn threadsafe proxy-objects" do
    arr = Tsafe::Proxy.new(obj: {})

    0.upto(5) do |i|
      arr[i] = i
    end

    Thread.new do
      begin
        arr.each do
          sleep 0.1
        end
      rescue Exception => e # rubocop:disable Lint/RescueException
        puts e.inspect
      end
    end

    5.upto(10) do |i|
      arr[i] = i
      sleep 0.1
    end
  end

  it "should be able to spawn special classes" do
    # Create new synchronized hash.
    arr = Tsafe::MonHash.new

    # Make sure we get the right results.
    arr[1] = 2

    res = arr[1]
    raise "Expected 2 but got '#{res}'." unless res == 2

    # Set some values to test with.
    0.upto(5) do |i|
      arr[i] = i
    end

    # Try to call through each through a thread and then also try to set new values, which normally would crash the hash.
    Thread.new do
      begin
        arr.each do |key, val|
          res = key + val
          sleep 0.1
        end
      rescue Exception => e # rubocop:disable Lint/RescueException
        puts e.inspect
      end
    end

    # This should not crash it, since they should wait for each other.
    5.upto(10) do |i|
      arr[i] = i
      sleep 0.1
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tsafe-0.0.12 spec/tsafe_spec.rb