1 # frozen_string_literal: true
6 class PStoreTest < Test::Unit::TestCase
8 @pstore_file = File.join(Dir.tmpdir, "pstore.tmp.#{Process.pid}")
9 @pstore = PStore.new(@pstore_file)
13 File.unlink(@pstore_file) rescue nil
16 def test_opening_new_file_in_readonly_mode_should_result_in_empty_values
17 @pstore.transaction(true) do
18 assert_nil @pstore[:foo]
19 assert_nil @pstore[:bar]
23 def test_opening_new_file_in_readwrite_mode_should_result_in_empty_values
24 @pstore.transaction do
25 assert_nil @pstore[:foo]
26 assert_nil @pstore[:bar]
30 def test_data_should_be_loaded_correctly_when_in_readonly_mode
31 @pstore.transaction do
34 @pstore.transaction(true) do
35 assert_equal "bar", @pstore[:foo]
39 def test_data_should_be_loaded_correctly_when_in_readwrite_mode
40 @pstore.transaction do
43 @pstore.transaction do
44 assert_equal "bar", @pstore[:foo]
48 def test_changes_after_commit_are_discarded
49 @pstore.transaction do
54 @pstore.transaction(true) do
55 assert_equal "bar", @pstore[:foo]
59 def test_changes_are_not_written_on_abort
60 @pstore.transaction do
64 @pstore.transaction(true) do
65 assert_nil @pstore[:foo]
69 def test_writing_inside_readonly_transaction_raises_error
70 assert_raise(PStore::Error) do
71 @pstore.transaction(true) do
78 q1 = Thread::Queue.new
79 assert_raise(PStore::Error) do
81 @pstore.transaction do
89 @pstore.transaction {}
95 q2 = Thread::Queue.new
97 pstore = PStore.new(second_file, true)
100 pstore.transaction do
104 # wait for cur to enter a transaction
105 sleep 0.1 until cur.stop?
111 assert_equal("bar", pstore.transaction { pstore[:foo] })
117 File.unlink(second_file) rescue nil
120 def test_nested_transaction_raises_error
121 assert_raise(PStore::Error) do
122 @pstore.transaction { @pstore.transaction { } }
124 pstore = PStore.new(second_file, true)
125 assert_raise(PStore::Error) do
126 pstore.transaction { pstore.transaction { } }
129 File.unlink(second_file) rescue nil
132 # Test that PStore's file operations do not blow up when default encodings are set
133 def test_pstore_files_are_accessed_as_binary_files
134 bug5311 = '[ruby-core:39503]'
136 assert_in_out_err(["-Eutf-8:utf-8", "-rpstore", "-", @pstore_file], <<-SRC, [bug5311], [], bug5311, timeout: 30)
137 @pstore = PStore.new(ARGV[0])
138 (1..#{n}).each do |i|
139 @pstore.transaction {@pstore["Key\#{i}"] = "value \#{i}"}
141 @pstore.transaction {@pstore["Bug5311"] = '#{bug5311}'}
142 puts @pstore.transaction {@pstore["Bug5311"]}
144 assert_equal(bug5311, @pstore.transaction {@pstore["Bug5311"]}, bug5311)
148 File.join(Dir.tmpdir, "pstore.tmp2.#{Process.pid}")