Sha256: 9b1bfe063f13c86f3e178c3513759e34f1729c503b84b9297153b5aa7ba814af

Contents?: true

Size: 1.57 KB

Versions: 16

Compression:

Stored size: 1.57 KB

Contents

#!/usr/bin/env ruby

Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }

require 'puppet/util/file_locking'

describe Puppet::Util::FileLocking do
  before :each do
    @file = Tempfile.new("puppetspec")
    filepath = @file.path
    @file.close!()
    @file = filepath
    @data = {:a => :b, :c => "A string", :d => "another string", :e => %w{an array of strings}}
    File.open(@file, "w") { |f| f.puts YAML.dump(@data) }
  end

  it "should be able to keep file corruption from happening when there are multiple writers threads" do
    threads = []
    sync = Sync.new
    9.times { |a|
      threads << Thread.new {
        9.times { |b|
          sync.synchronize(Sync::SH) {
            Puppet::Util::FileLocking.readlock(@file) { |f|
              YAML.load(f.read).should == @data
            }
          }
          sleep 0.01
          sync.synchronize(Sync::EX) {
            Puppet::Util::FileLocking.writelock(@file) { |f|
              f.puts YAML.dump(@data)
            }
          }
        }
      }
    }
    threads.each { |th| th.join }
  end

  it "should be able to keep file corruption from happening when there are multiple writers processes" do
    unless Process.fork
      50.times { |b|
        Puppet::Util::FileLocking.writelock(@file) { |f|
          f.puts YAML.dump(@data)
        }
        sleep 0.01
      }
      Kernel.exit!
    end

    50.times { |c|
      Puppet::Util::FileLocking.readlock(@file) { |f|
        YAML.load(f.read).should == @data
      }
    }
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
puppet-2.6.18 spec/integration/util/file_locking_spec.rb
puppet-2.6.17 spec/integration/util/file_locking_spec.rb
puppet-2.6.16 spec/integration/util/file_locking_spec.rb
puppet-2.6.15 spec/integration/util/file_locking_spec.rb
puppet-2.6.14 spec/integration/util/file_locking_spec.rb
puppet-2.6.13 spec/integration/util/file_locking_spec.rb
puppet-2.6.12 spec/integration/util/file_locking_spec.rb
puppet-2.6.11 spec/integration/util/file_locking_spec.rb
puppet-2.6.10 spec/integration/util/file_locking_spec.rb
puppet-2.6.9 spec/integration/util/file_locking_spec.rb
puppet-2.6.8 spec/integration/util/file_locking_spec.rb
puppet-2.6.7 spec/integration/util/file_locking_spec.rb
puppet-2.6.6 spec/integration/util/file_locking_spec.rb
puppet-2.6.5 spec/integration/util/file_locking_spec.rb
puppet-2.6.4 spec/integration/util/file_locking_spec.rb
puppet-2.6.3 spec/integration/util/file_locking_spec.rb