Sha256: e96447b0bf619f9f67c9db7d1d5b1df51be76d47f8c1fb1177a5ababeeb7b7b1

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

shared_examples :concurrent_create do
  # Each thread attempts to create
  def create_thread(name)
    Thread.new do
      s = new_store
      begin
        (0...1000).map do |i|
          s.create(i.to_s, name, expires: false).tap do
            Thread.pass if rand(100) >= 99
          end
        end
      ensure
        s.close
      end
    end
  end

  it 'have atomic create across multiple threads', isolate: true do
    names = %w{a b c}

    # Spawn threads and then group results (lists of true/false values) by
    # store index (0...1000)
    results = names
      .map { |name| create_thread(name) }
      .map(&:value)
      .transpose.each_with_index
      .map { |created_values, i| [i.to_s, created_values] }
      .to_h

    # Just a quick sanity check
    expect(results.length).to eq 1000

    # Ensure that for each index, one and only one created value is true
    expect(results.map { |_, created_values| created_values.inject(:^) }).to all(be true)

    # Check that the when a call to create returned true, that the store
    # contains the correct value as a result
    expect(store.slice(*results.keys).to_h).to eq(results.map do |i, values|
      [i, names[values.index(true)]]
    end.to_h)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
moneta-1.5.2 spec/features/concurrent_create.rb
moneta-1.5.1 spec/features/concurrent_create.rb
moneta-1.5.0 spec/features/concurrent_create.rb
moneta-1.4.2 spec/features/concurrent_create.rb
moneta-1.4.1 spec/features/concurrent_create.rb
moneta-1.4.0 spec/features/concurrent_create.rb
moneta-1.3.0 spec/features/concurrent_create.rb
moneta-1.2.1 spec/features/concurrent_create.rb
moneta-1.2.0 spec/features/concurrent_create.rb