PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_pstore.rb

http://github.com/ruby/ruby
Ruby | 150 lines | 134 code | 13 blank | 3 comment | 0 complexity | 2a362469034a5d64c44450e468172842 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: true
  2. require 'test/unit'
  3. require 'pstore'
  4. require 'tmpdir'
  5. class PStoreTest < Test::Unit::TestCase
  6. def setup
  7. @pstore_file = File.join(Dir.tmpdir, "pstore.tmp.#{Process.pid}")
  8. @pstore = PStore.new(@pstore_file)
  9. end
  10. def teardown
  11. File.unlink(@pstore_file) rescue nil
  12. end
  13. def test_opening_new_file_in_readonly_mode_should_result_in_empty_values
  14. @pstore.transaction(true) do
  15. assert_nil @pstore[:foo]
  16. assert_nil @pstore[:bar]
  17. end
  18. end
  19. def test_opening_new_file_in_readwrite_mode_should_result_in_empty_values
  20. @pstore.transaction do
  21. assert_nil @pstore[:foo]
  22. assert_nil @pstore[:bar]
  23. end
  24. end
  25. def test_data_should_be_loaded_correctly_when_in_readonly_mode
  26. @pstore.transaction do
  27. @pstore[:foo] = "bar"
  28. end
  29. @pstore.transaction(true) do
  30. assert_equal "bar", @pstore[:foo]
  31. end
  32. end
  33. def test_data_should_be_loaded_correctly_when_in_readwrite_mode
  34. @pstore.transaction do
  35. @pstore[:foo] = "bar"
  36. end
  37. @pstore.transaction do
  38. assert_equal "bar", @pstore[:foo]
  39. end
  40. end
  41. def test_changes_after_commit_are_discarded
  42. @pstore.transaction do
  43. @pstore[:foo] = "bar"
  44. @pstore.commit
  45. @pstore[:foo] = "baz"
  46. end
  47. @pstore.transaction(true) do
  48. assert_equal "bar", @pstore[:foo]
  49. end
  50. end
  51. def test_changes_are_not_written_on_abort
  52. @pstore.transaction do
  53. @pstore[:foo] = "bar"
  54. @pstore.abort
  55. end
  56. @pstore.transaction(true) do
  57. assert_nil @pstore[:foo]
  58. end
  59. end
  60. def test_writing_inside_readonly_transaction_raises_error
  61. assert_raise(PStore::Error) do
  62. @pstore.transaction(true) do
  63. @pstore[:foo] = "bar"
  64. end
  65. end
  66. end
  67. def test_thread_safe
  68. q1 = Queue.new
  69. assert_raise(PStore::Error) do
  70. th = Thread.new do
  71. @pstore.transaction do
  72. @pstore[:foo] = "bar"
  73. q1.push true
  74. sleep
  75. end
  76. end
  77. begin
  78. q1.pop
  79. @pstore.transaction {}
  80. ensure
  81. th.kill
  82. th.join
  83. end
  84. end
  85. q2 = Queue.new
  86. begin
  87. pstore = PStore.new(second_file, true)
  88. cur = Thread.current
  89. th = Thread.new do
  90. pstore.transaction do
  91. pstore[:foo] = "bar"
  92. q1.push true
  93. q2.pop
  94. # wait for cur to enter a transaction
  95. sleep 0.1 until cur.stop?
  96. end
  97. end
  98. begin
  99. q1.pop
  100. q2.push true
  101. assert_equal("bar", pstore.transaction { pstore[:foo] })
  102. ensure
  103. th.join
  104. end
  105. end
  106. ensure
  107. File.unlink(second_file) rescue nil
  108. end
  109. def test_nested_transaction_raises_error
  110. assert_raise(PStore::Error) do
  111. @pstore.transaction { @pstore.transaction { } }
  112. end
  113. pstore = PStore.new(second_file, true)
  114. assert_raise(PStore::Error) do
  115. pstore.transaction { pstore.transaction { } }
  116. end
  117. ensure
  118. File.unlink(second_file) rescue nil
  119. end
  120. # Test that PStore's file operations do not blow up when default encodings are set
  121. def test_pstore_files_are_accessed_as_binary_files
  122. bug5311 = '[ruby-core:39503]'
  123. n = 128
  124. assert_in_out_err(["-Eutf-8:utf-8", "-rpstore", "-", @pstore_file], <<-SRC, [bug5311], [], bug5311, timeout: 30)
  125. @pstore = PStore.new(ARGV[0])
  126. (1..#{n}).each do |i|
  127. @pstore.transaction {@pstore["Key\#{i}"] = "value \#{i}"}
  128. end
  129. @pstore.transaction {@pstore["Bug5311"] = '#{bug5311}'}
  130. puts @pstore.transaction {@pstore["Bug5311"]}
  131. SRC
  132. assert_equal(bug5311, @pstore.transaction {@pstore["Bug5311"]}, bug5311)
  133. end
  134. def second_file
  135. File.join(Dir.tmpdir, "pstore.tmp2.#{Process.pid}")
  136. end
  137. end