ferret 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. data/Rakefile +23 -5
  2. data/TODO +2 -1
  3. data/ext/analysis.c +838 -177
  4. data/ext/analysis.h +55 -7
  5. data/ext/api.c +69 -0
  6. data/ext/api.h +27 -0
  7. data/ext/array.c +8 -5
  8. data/ext/compound_io.c +132 -96
  9. data/ext/document.c +58 -28
  10. data/ext/except.c +59 -0
  11. data/ext/except.h +88 -0
  12. data/ext/ferret.c +47 -3
  13. data/ext/ferret.h +3 -0
  14. data/ext/field.c +15 -9
  15. data/ext/filter.c +1 -1
  16. data/ext/fs_store.c +215 -34
  17. data/ext/global.c +72 -3
  18. data/ext/global.h +4 -3
  19. data/ext/hash.c +44 -3
  20. data/ext/hash.h +9 -0
  21. data/ext/header.h +58 -0
  22. data/ext/inc/except.h +88 -0
  23. data/ext/inc/lang.h +23 -13
  24. data/ext/ind.c +16 -10
  25. data/ext/index.h +2 -22
  26. data/ext/index_io.c +3 -11
  27. data/ext/index_rw.c +245 -193
  28. data/ext/lang.h +23 -13
  29. data/ext/libstemmer.c +92 -0
  30. data/ext/libstemmer.h +79 -0
  31. data/ext/modules.h +162 -0
  32. data/ext/q_boolean.c +34 -21
  33. data/ext/q_const_score.c +6 -12
  34. data/ext/q_filtered_query.c +206 -0
  35. data/ext/q_fuzzy.c +18 -15
  36. data/ext/q_match_all.c +3 -7
  37. data/ext/q_multi_phrase.c +10 -14
  38. data/ext/q_parser.c +29 -2
  39. data/ext/q_phrase.c +14 -21
  40. data/ext/q_prefix.c +15 -12
  41. data/ext/q_range.c +30 -28
  42. data/ext/q_span.c +13 -21
  43. data/ext/q_term.c +17 -26
  44. data/ext/r_analysis.c +693 -21
  45. data/ext/r_doc.c +11 -12
  46. data/ext/r_index_io.c +4 -1
  47. data/ext/r_qparser.c +21 -2
  48. data/ext/r_search.c +285 -18
  49. data/ext/ram_store.c +5 -2
  50. data/ext/search.c +11 -17
  51. data/ext/search.h +21 -45
  52. data/ext/similarity.h +67 -0
  53. data/ext/sort.c +30 -25
  54. data/ext/stem_ISO_8859_1_danish.c +338 -0
  55. data/ext/stem_ISO_8859_1_danish.h +16 -0
  56. data/ext/stem_ISO_8859_1_dutch.c +635 -0
  57. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  58. data/ext/stem_ISO_8859_1_english.c +1156 -0
  59. data/ext/stem_ISO_8859_1_english.h +16 -0
  60. data/ext/stem_ISO_8859_1_finnish.c +792 -0
  61. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  62. data/ext/stem_ISO_8859_1_french.c +1276 -0
  63. data/ext/stem_ISO_8859_1_french.h +16 -0
  64. data/ext/stem_ISO_8859_1_german.c +512 -0
  65. data/ext/stem_ISO_8859_1_german.h +16 -0
  66. data/ext/stem_ISO_8859_1_italian.c +1091 -0
  67. data/ext/stem_ISO_8859_1_italian.h +16 -0
  68. data/ext/stem_ISO_8859_1_norwegian.c +296 -0
  69. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  70. data/ext/stem_ISO_8859_1_porter.c +776 -0
  71. data/ext/stem_ISO_8859_1_porter.h +16 -0
  72. data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
  73. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  74. data/ext/stem_ISO_8859_1_spanish.c +1119 -0
  75. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  76. data/ext/stem_ISO_8859_1_swedish.c +307 -0
  77. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  78. data/ext/stem_KOI8_R_russian.c +701 -0
  79. data/ext/stem_KOI8_R_russian.h +16 -0
  80. data/ext/stem_UTF_8_danish.c +344 -0
  81. data/ext/stem_UTF_8_danish.h +16 -0
  82. data/ext/stem_UTF_8_dutch.c +653 -0
  83. data/ext/stem_UTF_8_dutch.h +16 -0
  84. data/ext/stem_UTF_8_english.c +1176 -0
  85. data/ext/stem_UTF_8_english.h +16 -0
  86. data/ext/stem_UTF_8_finnish.c +808 -0
  87. data/ext/stem_UTF_8_finnish.h +16 -0
  88. data/ext/stem_UTF_8_french.c +1296 -0
  89. data/ext/stem_UTF_8_french.h +16 -0
  90. data/ext/stem_UTF_8_german.c +526 -0
  91. data/ext/stem_UTF_8_german.h +16 -0
  92. data/ext/stem_UTF_8_italian.c +1113 -0
  93. data/ext/stem_UTF_8_italian.h +16 -0
  94. data/ext/stem_UTF_8_norwegian.c +302 -0
  95. data/ext/stem_UTF_8_norwegian.h +16 -0
  96. data/ext/stem_UTF_8_porter.c +794 -0
  97. data/ext/stem_UTF_8_porter.h +16 -0
  98. data/ext/stem_UTF_8_portuguese.c +1055 -0
  99. data/ext/stem_UTF_8_portuguese.h +16 -0
  100. data/ext/stem_UTF_8_russian.c +709 -0
  101. data/ext/stem_UTF_8_russian.h +16 -0
  102. data/ext/stem_UTF_8_spanish.c +1137 -0
  103. data/ext/stem_UTF_8_spanish.h +16 -0
  104. data/ext/stem_UTF_8_swedish.c +313 -0
  105. data/ext/stem_UTF_8_swedish.h +16 -0
  106. data/ext/stopwords.c +325 -0
  107. data/ext/store.c +34 -2
  108. data/ext/tags +2953 -0
  109. data/ext/term.c +21 -15
  110. data/ext/termdocs.c +5 -3
  111. data/ext/utilities.c +446 -0
  112. data/ext/vector.c +27 -13
  113. data/lib/ferret/document/document.rb +1 -1
  114. data/lib/ferret/index/index.rb +44 -6
  115. data/lib/ferret/query_parser/query_parser.tab.rb +7 -3
  116. data/lib/rferret.rb +2 -1
  117. data/test/test_helper.rb +2 -2
  118. data/test/unit/analysis/ctc_analyzer.rb +401 -0
  119. data/test/unit/analysis/ctc_tokenstream.rb +423 -0
  120. data/test/unit/analysis/{tc_letter_tokenizer.rb → rtc_letter_tokenizer.rb} +0 -0
  121. data/test/unit/analysis/{tc_lower_case_filter.rb → rtc_lower_case_filter.rb} +0 -0
  122. data/test/unit/analysis/{tc_lower_case_tokenizer.rb → rtc_lower_case_tokenizer.rb} +0 -0
  123. data/test/unit/analysis/{tc_per_field_analyzer_wrapper.rb → rtc_per_field_analyzer_wrapper.rb} +0 -0
  124. data/test/unit/analysis/{tc_porter_stem_filter.rb → rtc_porter_stem_filter.rb} +0 -0
  125. data/test/unit/analysis/{tc_standard_analyzer.rb → rtc_standard_analyzer.rb} +0 -0
  126. data/test/unit/analysis/{tc_standard_tokenizer.rb → rtc_standard_tokenizer.rb} +0 -0
  127. data/test/unit/analysis/{tc_stop_analyzer.rb → rtc_stop_analyzer.rb} +0 -0
  128. data/test/unit/analysis/{tc_stop_filter.rb → rtc_stop_filter.rb} +0 -0
  129. data/test/unit/analysis/{tc_white_space_analyzer.rb → rtc_white_space_analyzer.rb} +0 -0
  130. data/test/unit/analysis/{tc_white_space_tokenizer.rb → rtc_white_space_tokenizer.rb} +0 -0
  131. data/test/unit/analysis/{tc_word_list_loader.rb → rtc_word_list_loader.rb} +0 -0
  132. data/test/unit/analysis/tc_analyzer.rb +1 -2
  133. data/test/unit/analysis/{c_token.rb → tc_token.rb} +0 -0
  134. data/test/unit/document/rtc_field.rb +28 -0
  135. data/test/unit/document/{c_document.rb → tc_document.rb} +0 -0
  136. data/test/unit/document/tc_field.rb +82 -12
  137. data/test/unit/index/{tc_compound_file_io.rb → rtc_compound_file_io.rb} +0 -0
  138. data/test/unit/index/{tc_field_infos.rb → rtc_field_infos.rb} +0 -0
  139. data/test/unit/index/{tc_fields_io.rb → rtc_fields_io.rb} +0 -0
  140. data/test/unit/index/{tc_multiple_term_doc_pos_enum.rb → rtc_multiple_term_doc_pos_enum.rb} +0 -0
  141. data/test/unit/index/{tc_segment_infos.rb → rtc_segment_infos.rb} +0 -0
  142. data/test/unit/index/{tc_segment_term_docs.rb → rtc_segment_term_docs.rb} +0 -0
  143. data/test/unit/index/{tc_segment_term_enum.rb → rtc_segment_term_enum.rb} +0 -0
  144. data/test/unit/index/{tc_segment_term_vector.rb → rtc_segment_term_vector.rb} +0 -0
  145. data/test/unit/index/{tc_term_buffer.rb → rtc_term_buffer.rb} +0 -0
  146. data/test/unit/index/{tc_term_info.rb → rtc_term_info.rb} +0 -0
  147. data/test/unit/index/{tc_term_infos_io.rb → rtc_term_infos_io.rb} +0 -0
  148. data/test/unit/index/{tc_term_vectors_io.rb → rtc_term_vectors_io.rb} +0 -0
  149. data/test/unit/index/{c_index.rb → tc_index.rb} +26 -6
  150. data/test/unit/index/{c_index_reader.rb → tc_index_reader.rb} +0 -0
  151. data/test/unit/index/{c_index_writer.rb → tc_index_writer.rb} +0 -0
  152. data/test/unit/index/{c_term.rb → tc_term.rb} +0 -0
  153. data/test/unit/index/{c_term_voi.rb → tc_term_voi.rb} +0 -0
  154. data/test/unit/query_parser/{c_query_parser.rb → rtc_query_parser.rb} +14 -14
  155. data/test/unit/query_parser/tc_query_parser.rb +24 -16
  156. data/test/unit/search/{tc_similarity.rb → rtc_similarity.rb} +0 -0
  157. data/test/unit/search/rtc_sort_field.rb +14 -0
  158. data/test/unit/search/{c_filter.rb → tc_filter.rb} +11 -11
  159. data/test/unit/search/{c_fuzzy_query.rb → tc_fuzzy_query.rb} +0 -0
  160. data/test/unit/search/{c_index_searcher.rb → tc_index_searcher.rb} +0 -0
  161. data/test/unit/search/{c_search_and_sort.rb → tc_search_and_sort.rb} +0 -0
  162. data/test/unit/search/{c_sort.rb → tc_sort.rb} +0 -0
  163. data/test/unit/search/tc_sort_field.rb +20 -7
  164. data/test/unit/search/{c_spans.rb → tc_spans.rb} +0 -0
  165. data/test/unit/store/rtc_fs_store.rb +62 -0
  166. data/test/unit/store/rtc_ram_store.rb +15 -0
  167. data/test/unit/store/rtm_store.rb +150 -0
  168. data/test/unit/store/rtm_store_lock.rb +2 -0
  169. data/test/unit/store/tc_fs_store.rb +54 -40
  170. data/test/unit/store/tc_ram_store.rb +20 -0
  171. data/test/unit/store/tm_store.rb +30 -146
  172. data/test/unit/store/tm_store_lock.rb +66 -0
  173. data/test/unit/utils/{tc_bit_vector.rb → rtc_bit_vector.rb} +0 -0
  174. data/test/unit/utils/{tc_date_tools.rb → rtc_date_tools.rb} +0 -0
  175. data/test/unit/utils/{tc_number_tools.rb → rtc_number_tools.rb} +0 -0
  176. data/test/unit/utils/{tc_parameter.rb → rtc_parameter.rb} +0 -0
  177. data/test/unit/utils/{tc_priority_queue.rb → rtc_priority_queue.rb} +0 -0
  178. data/test/unit/utils/{tc_string_helper.rb → rtc_string_helper.rb} +0 -0
  179. data/test/unit/utils/{tc_thread.rb → rtc_thread.rb} +0 -0
  180. data/test/unit/utils/{tc_weak_key_hash.rb → rtc_weak_key_hash.rb} +0 -0
  181. metadata +360 -289
  182. data/test/unit/document/c_field.rb +0 -98
  183. data/test/unit/search/c_sort_field.rb +0 -27
  184. data/test/unit/store/c_fs_store.rb +0 -76
  185. data/test/unit/store/c_ram_store.rb +0 -35
  186. data/test/unit/store/m_store.rb +0 -34
  187. data/test/unit/store/m_store_lock.rb +0 -68
@@ -1,98 +0,0 @@
1
- require File.dirname(__FILE__) + "/../../test_helper"
2
-
3
-
4
- class FieldTest < Test::Unit::TestCase
5
- include Ferret::Document
6
- include Ferret::Utils
7
-
8
- def test_store()
9
- assert_not_nil(Field::Store::COMPRESS)
10
- assert_not_nil(Field::Store::YES)
11
- assert_not_nil(Field::Store::NO)
12
- end
13
-
14
- def test_index()
15
- assert_not_nil(Field::Index::TOKENIZED)
16
- assert_not_nil(Field::Index::UNTOKENIZED)
17
- assert_not_nil(Field::Index::NO)
18
- assert_not_nil(Field::Index::NO_NORMS)
19
- end
20
-
21
- def test_term_vector()
22
- assert_not_nil(Field::TermVector::YES)
23
- assert_not_nil(Field::TermVector::NO)
24
- assert_not_nil(Field::TermVector::WITH_POSITIONS)
25
- assert_not_nil(Field::TermVector::WITH_OFFSETS)
26
- assert_not_nil(Field::TermVector::WITH_POSITIONS_OFFSETS)
27
- end
28
-
29
- def test_standard_field()
30
- f = Field.new("name", "value", Field::Store::COMPRESS, Field::Index::TOKENIZED)
31
- assert_equal("name", f.name)
32
- assert_equal("value", f.data)
33
- assert_equal(true, f.stored?)
34
- assert_equal(true, f.compressed?)
35
- assert_equal(true, f.indexed?)
36
- assert_equal(true, f.tokenized?)
37
- assert_equal(false, f.store_term_vector?)
38
- assert_equal(false, f.store_offsets?)
39
- assert_equal(false, f.store_positions?)
40
- assert_equal(false, f.omit_norms?)
41
- assert_equal(false, f.binary?)
42
- assert_equal("stored/compressed,indexed,tokenized,<name:value>", f.to_s)
43
- f.data = "183"
44
- f.boost = 0.001
45
- assert_equal("183", f.data)
46
- assert(0.001 =~ f.boost)
47
- end
48
-
49
- def test_set_store()
50
- f = Field.new("name", "", Field::Store::COMPRESS, Field::Index::TOKENIZED)
51
- f.store = Field::Store::NO
52
- assert_equal(false, f.stored?)
53
- assert_equal(false, f.compressed?)
54
- assert_equal("indexed,tokenized,<name:>", f.to_s)
55
- end
56
-
57
- def test_set_index()
58
- f = Field.new("name", "value", Field::Store::COMPRESS, Field::Index::TOKENIZED)
59
- f.index = Field::Index::NO
60
- assert_equal(false, f.indexed?)
61
- assert_equal(false, f.tokenized?)
62
- assert_equal(false, f.omit_norms?)
63
- assert_equal("stored/compressed,<name:value>", f.to_s)
64
- f.index = Field::Index::NO_NORMS
65
- assert_equal(true, f.indexed?)
66
- assert_equal(false, f.tokenized?)
67
- assert_equal(true, f.omit_norms?)
68
- assert_equal("stored/compressed,indexed,omit_norms,<name:value>", f.to_s)
69
- end
70
-
71
- def test_set_term_vector()
72
- f = Field.new("name", "value", Field::Store::COMPRESS, Field::Index::TOKENIZED)
73
- f.term_vector = Field::TermVector::WITH_POSITIONS_OFFSETS
74
- assert_equal(true, f.store_term_vector?)
75
- assert_equal(true, f.store_offsets?)
76
- assert_equal(true, f.store_positions?)
77
- assert_equal("stored/compressed,indexed,tokenized,store_term_vector,store_offsets,store_positions,<name:value>", f.to_s)
78
- end
79
-
80
- def test_new_binary_field()
81
- tmp = []
82
- 256.times {|i| tmp[i] = i}
83
- bin = tmp.pack("c*")
84
- f = Field.new_binary_field("name", bin, Field::Store::YES)
85
- assert_equal("name", f.name)
86
- assert_equal(bin, f.data)
87
- assert_equal(true, f.stored?)
88
- assert_equal(false, f.compressed?)
89
- assert_equal(false, f.indexed?)
90
- assert_equal(false, f.tokenized?)
91
- assert_equal(false, f.store_term_vector?)
92
- assert_equal(false, f.store_offsets?)
93
- assert_equal(false, f.store_positions?)
94
- assert_equal(false, f.omit_norms?)
95
- assert_equal(true, f.binary?)
96
- assert_equal("stored/uncompressed,binary,<name:=bin_data=>", f.to_s)
97
- end
98
- end
@@ -1,27 +0,0 @@
1
- require File.dirname(__FILE__) + "/../../test_helper"
2
-
3
- class SortFieldTest < Test::Unit::TestCase
4
- include Ferret::Search
5
-
6
- def test_field_score()
7
- fs = SortField::FIELD_SCORE
8
- assert_equal(SortField::SortType::SCORE, fs.sort_type)
9
- assert_nil(fs.name)
10
- assert(!fs.reverse?, "FIELD_SCORE should not be reverse")
11
- assert_nil(fs.comparator)
12
- end
13
-
14
- def test_field_doc()
15
- fs = SortField::FIELD_DOC
16
- assert_equal(SortField::SortType::DOC, fs.sort_type)
17
- assert_nil(fs.name)
18
- assert(!fs.reverse?, "FIELD_DOC should not be reverse")
19
- assert_nil(fs.comparator)
20
- end
21
-
22
- def test_error_raised()
23
- assert_raise(ArgumentError) {
24
- fs = SortField.new(nil, {:sort_type => SortField::SortType::INTEGER})
25
- }
26
- end
27
- end
@@ -1,76 +0,0 @@
1
- require File.dirname(__FILE__) + "/../../test_helper"
2
- require File.dirname(__FILE__) + "/m_store"
3
- require File.dirname(__FILE__) + "/m_store_lock"
4
-
5
- class FSStoreTest < Test::Unit::TestCase
6
- include Ferret::Store
7
- include StoreTest
8
- include StoreLockTest
9
- def setup
10
- @dpath = File.join(File.dirname(__FILE__),
11
- '../../temp/fsdir')
12
- @dir = FSDirectory.new(@dpath, true)
13
- @dir1 = FSDirectory.new(@dpath, true)
14
- @dir2 = FSDirectory.new(@dpath, true)
15
- end
16
-
17
- def teardown
18
- @dir.refresh()
19
- @dir.close()
20
- end
21
-
22
- def test_fslock
23
- lock_name = "lfile"
24
- lock_file_path = make_lock_file_path(lock_name)
25
- assert(! File.exists?(lock_file_path), "There should be no lock file")
26
- lock = @dir.make_lock(lock_name)
27
- assert(! File.exists?(lock_file_path), "There should still be no lock file")
28
- assert(! lock.locked?, "lock shouldn't be locked yet")
29
-
30
- lock.obtain
31
-
32
- assert(lock.locked?, "lock should now be locked")
33
-
34
- assert(File.exists?(lock_file_path), "A lock file should have been created")
35
-
36
- assert(! @dir.exists?(lock_file_path),
37
- "The lock should be hidden by the FSDirectories directory scan")
38
-
39
- lock.release
40
-
41
- assert(! lock.locked?, "lock should be freed again")
42
- assert(! File.exists?(lock_file_path), "The lock file should have been deleted")
43
- end
44
-
45
- # def make_and_loose_lock
46
- # lock = @dir.make_lock("finalizer_lock")
47
- # lock.obtain
48
- # lock = nil
49
- # end
50
- #
51
- # def test_fslock_finalizer
52
- # lock_name = "finalizer_lock"
53
- # lock_file_path = make_lock_file_path(lock_name)
54
- # assert(! File.exists?(lock_file_path), "There should be no lock file")
55
- #
56
- # make_and_loose_lock
57
- #
58
- # #assert(File.exists?(lock_file_path), "There should now be a lock file")
59
- #
60
- # lock = @dir.make_lock(lock_name)
61
- # assert(lock.locked?, "lock should now be locked")
62
- #
63
- # GC.start
64
- #
65
- # assert(! lock.locked?, "lock should be freed again")
66
- # assert(! File.exists?(lock_file_path), "The lock file should have been deleted")
67
- # end
68
- #
69
- def make_lock_file_path(name)
70
- lock_file_path = File.join(@dpath, "ferret-#{name}.lck")
71
- if File.exists?(lock_file_path) then
72
- File.delete(lock_file_path)
73
- end
74
- return lock_file_path
75
- end
76
- end
@@ -1,35 +0,0 @@
1
- require File.dirname(__FILE__) + "/../../test_helper"
2
- require File.dirname(__FILE__) + "/m_store"
3
- require File.dirname(__FILE__) + "/m_store_lock"
4
-
5
- class RAMStoreTest < Test::Unit::TestCase
6
- include StoreTest
7
- include StoreLockTest
8
- def setup
9
- @dir = Ferret::Store::RAMDirectory.new
10
- end
11
-
12
- def teardown
13
- @dir.close()
14
- end
15
-
16
- def test_ramlock
17
- name = "lfile"
18
- lfile = Ferret::Store::Directory::LOCK_PREFIX + name + ".lck"
19
- assert(! @dir.exists?(lfile),
20
- "There should be no lock file")
21
- lock = @dir.make_lock(name)
22
- assert(! @dir.exists?(lfile),
23
- "There should still be no lock file")
24
- assert(! @dir.exists?(lfile),
25
- "The lock should be hidden by the FSDirectories directory scan")
26
- assert(! lock.locked?, "lock shouldn't be locked yet")
27
- lock.obtain
28
- assert(lock.locked?, "lock should now be locked")
29
- assert(@dir.exists?(lfile), "A lock file should have been created")
30
- lock.release
31
- assert(! lock.locked?, "lock should be freed again")
32
- assert(! @dir.exists?(lfile),
33
- "The lock file should have been deleted")
34
- end
35
- end
@@ -1,34 +0,0 @@
1
- module StoreTest
2
- # declare dir so inheritors can access it.
3
- attr_accessor :dir
4
-
5
- # test the basic file manipulation methods;
6
- # - exists?
7
- # - touch
8
- # - delete
9
- # - file_count
10
- def test_basic_file_ops
11
- assert_equal(0, @dir.file_count(), "directory should be empty")
12
- assert(! @dir.exists?('filename'), "File should not exist")
13
- @dir.touch('tmpfile1')
14
- assert_equal(1, @dir.file_count(), "directory should have one file")
15
- @dir.touch('tmpfile2')
16
- assert_equal(2, @dir.file_count(), "directory should have two files")
17
- assert(@dir.exists?('tmpfile1'), "'tmpfile1' should exist")
18
- @dir.delete('tmpfile1')
19
- assert(! @dir.exists?('tmpfile1'), "'tmpfile1' should no longer exist")
20
- assert_equal(1, @dir.file_count(), "directory should have one file")
21
- end
22
-
23
- def test_rename
24
- @dir.touch("from")
25
- assert(@dir.exists?('from'), "File should exist")
26
- assert(! @dir.exists?('to'), "File should not exist")
27
- cnt_before = @dir.file_count()
28
- @dir.rename('from', 'to')
29
- cnt_after = @dir.file_count()
30
- assert_equal(cnt_before, cnt_after, "the number of files shouldn't have changed")
31
- assert(@dir.exists?('to'), "File should now exist")
32
- assert(! @dir.exists?('from'), "File should no longer exist")
33
- end
34
- end
@@ -1,68 +0,0 @@
1
- module StoreLockTest
2
- class Switch
3
- @@counter = 0
4
- def Switch.counter() return @@counter end
5
- def Switch.counter=(counter) @@counter = counter end
6
- end
7
-
8
- def test_locking()
9
- lock_time_out = 0.001 # we want this test to run quickly
10
- lock1 = @dir.make_lock("l.lck")
11
- lock2 = @dir.make_lock("l.lck")
12
-
13
- assert(!lock2.locked?)
14
- assert(lock1.obtain(lock_time_out))
15
- assert(lock2.locked?)
16
-
17
- assert(! obtain_lock_true_false(lock2))
18
-
19
- exception_thrown = false
20
- begin
21
- lock2.while_locked(lock_time_out) do
22
- assert(false, "lock should not have been obtained")
23
- end
24
- rescue
25
- exception_thrown = true
26
- ensure
27
- assert(exception_thrown)
28
- end
29
-
30
- lock1.release()
31
- assert(lock2.obtain(lock_time_out))
32
- lock2.release()
33
-
34
- t = Thread.new() do
35
- lock1.while_locked(lock_time_out) do
36
- Switch.counter = 1
37
- # make sure lock2 obtain test was run
38
- while Switch.counter < 2
39
- end
40
- Switch.counter = 3
41
- end
42
- end
43
- t.run()
44
-
45
- #make sure thread has started and lock been obtained
46
- while Switch.counter < 1
47
- end
48
-
49
- assert(! obtain_lock_true_false(lock2))
50
-
51
- Switch.counter = 2
52
- while Switch.counter < 3
53
- end
54
-
55
- assert(lock2.obtain(lock_time_out))
56
- lock2.release()
57
- end
58
-
59
- def obtain_lock_true_false(lock)
60
- lock_time_out = 0.001 # we want this test to run quickly
61
- begin
62
- lock.obtain(lock_time_out)
63
- return true
64
- rescue
65
- end
66
- return false
67
- end
68
- end