[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / test / test_pstore.rb
blob7dfbdca367a8f5b0b7a891cd8db13fa8c9a494de
1 # frozen_string_literal: true
2 require 'test/unit'
3 require 'pstore'
4 require 'tmpdir'
6 class PStoreTest < Test::Unit::TestCase
7   def setup
8     @pstore_file = File.join(Dir.tmpdir, "pstore.tmp.#{Process.pid}")
9     @pstore = PStore.new(@pstore_file)
10   end
12   def teardown
13     File.unlink(@pstore_file) rescue nil
14   end
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]
20     end
21   end
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]
27     end
28   end
30   def test_data_should_be_loaded_correctly_when_in_readonly_mode
31     @pstore.transaction do
32       @pstore[:foo] = "bar"
33     end
34     @pstore.transaction(true) do
35       assert_equal "bar", @pstore[:foo]
36     end
37   end
39   def test_data_should_be_loaded_correctly_when_in_readwrite_mode
40     @pstore.transaction do
41       @pstore[:foo] = "bar"
42     end
43     @pstore.transaction do
44       assert_equal "bar", @pstore[:foo]
45     end
46   end
48   def test_changes_after_commit_are_discarded
49     @pstore.transaction do
50       @pstore[:foo] = "bar"
51       @pstore.commit
52       @pstore[:foo] = "baz"
53     end
54     @pstore.transaction(true) do
55       assert_equal "bar", @pstore[:foo]
56     end
57   end
59   def test_changes_are_not_written_on_abort
60     @pstore.transaction do
61       @pstore[:foo] = "bar"
62       @pstore.abort
63     end
64     @pstore.transaction(true) do
65       assert_nil @pstore[:foo]
66     end
67   end
69   def test_writing_inside_readonly_transaction_raises_error
70     assert_raise(PStore::Error) do
71       @pstore.transaction(true) do
72         @pstore[:foo] = "bar"
73       end
74     end
75   end
77   def test_thread_safe
78     q1 = Thread::Queue.new
79     assert_raise(PStore::Error) do
80       th = Thread.new do
81         @pstore.transaction do
82           @pstore[:foo] = "bar"
83           q1.push true
84           sleep
85         end
86       end
87       begin
88         q1.pop
89         @pstore.transaction {}
90       ensure
91         th.kill
92         th.join
93       end
94     end
95     q2 = Thread::Queue.new
96     begin
97       pstore = PStore.new(second_file, true)
98       cur = Thread.current
99       th = Thread.new do
100         pstore.transaction do
101           pstore[:foo] = "bar"
102           q1.push true
103           q2.pop
104           # wait for cur to enter a transaction
105           sleep 0.1 until cur.stop?
106         end
107       end
108       begin
109         q1.pop
110         q2.push true
111         assert_equal("bar", pstore.transaction { pstore[:foo] })
112       ensure
113         th.join
114       end
115     end
116   ensure
117     File.unlink(second_file) rescue nil
118   end
120   def test_nested_transaction_raises_error
121     assert_raise(PStore::Error) do
122       @pstore.transaction { @pstore.transaction { } }
123     end
124     pstore = PStore.new(second_file, true)
125     assert_raise(PStore::Error) do
126       pstore.transaction { pstore.transaction { } }
127     end
128   ensure
129     File.unlink(second_file) rescue nil
130   end
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]'
135     n = 128
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}"}
140       end
141       @pstore.transaction {@pstore["Bug5311"] = '#{bug5311}'}
142       puts @pstore.transaction {@pstore["Bug5311"]}
143     SRC
144     assert_equal(bug5311, @pstore.transaction {@pstore["Bug5311"]}, bug5311)
145   end
147   def test_key_p
148     [:key?, :root?].each do |method|
149       clear_store
150       @pstore.transaction do
151         @pstore[:foo] = 0
152         assert_equal(true, @pstore.send(method, :foo))
153         assert_equal(false, @pstore.send(method, :bar))
154       end
155     end
156   end
158   def test_keys
159     [:keys, :roots].each do |method|
160       clear_store
161       @pstore.transaction do
162         assert_equal([], @pstore.send(method))
163       end
164       @pstore.transaction do
165         @pstore[:foo] = 0
166         assert_equal([:foo], @pstore.send(method))
167       end
168     end
169   end
171   def clear_store
172     @pstore.transaction do
173       @pstore.keys.each do |key|
174         @pstore.delete(key)
175       end
176     end
177   end
179   def second_file
180     File.join(Dir.tmpdir, "pstore.tmp2.#{Process.pid}")
181   end