rims 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,169 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- require 'fileutils'
4
- require 'pp' if $DEBUG
5
- require 'rims'
6
- require 'test/unit'
7
-
8
- module RIMS::Test
9
- class DaemonStatusFileTest < Test::Unit::TestCase
10
- def setup
11
- @stat_path = "status.#{$$}"
12
- @stat_file = RIMS::Daemon.new_status_file(@stat_path, exclusive: true)
13
- @read_file = RIMS::Daemon.new_status_file(@stat_path, exclusive: false)
14
- @ready_spin_lock = "ready_spin_lock.#{$$}"
15
- @final_spin_lock = "final_spin_lock.#{$$}"
16
- end
17
-
18
- def teardown
19
- FileUtils.rm_f(@stat_path)
20
- FileUtils.rm_f(@ready_spin_lock)
21
- FileUtils.rm_f(@final_spin_lock)
22
- end
23
-
24
- def test_lock_status
25
- @stat_file.open{
26
- assert_equal(false, @stat_file.locked?)
27
- @stat_file.synchronize{
28
- assert_equal(true, @stat_file.locked?)
29
- }
30
- assert_equal(false, @stat_file.locked?)
31
- }
32
- end
33
-
34
- def test_should_be_locked
35
- assert_raise(RuntimeError) {
36
- @stat_file.should_be_locked
37
- }
38
-
39
- @stat_file.open{
40
- @stat_file.synchronize{
41
- @stat_file.should_be_locked
42
- }
43
- }
44
- end
45
-
46
- def test_should_not_be_locked
47
- @stat_file.should_not_be_locked
48
-
49
- @stat_file.open{
50
- @stat_file.synchronize{
51
- assert_raise(RuntimeError) {
52
- @stat_file.should_not_be_locked
53
- }
54
- }
55
- }
56
- end
57
-
58
- def test_lock_guard
59
- @stat_file.open{
60
- @stat_file.synchronize{
61
- assert_raise(RuntimeError) {
62
- @stat_file.lock
63
- }
64
- }
65
- }
66
- end
67
-
68
- def test_unlock_guard
69
- @stat_file.open{
70
- assert_raise(RuntimeError) {
71
- @stat_file.unlock
72
- }
73
- }
74
- end
75
-
76
- def another_process_exclusive_lock(write_text: nil)
77
- FileUtils.touch(@ready_spin_lock)
78
- FileUtils.touch(@final_spin_lock)
79
-
80
- pid = Process.fork{
81
- lock_file = RIMS::Daemon.new_status_file(@stat_path, exclusive: true)
82
- lock_file.open{
83
- lock_file.synchronize{
84
- lock_file.write(write_text) if write_text
85
- FileUtils.rm_f(@ready_spin_lock)
86
- while (File.exist? @final_spin_lock)
87
- # nothing to do.
88
- end
89
- }
90
- }
91
- exit!
92
- }
93
-
94
- while (File.exist? @ready_spin_lock)
95
- # nothing to do.
96
- end
97
-
98
- begin
99
- yield
100
- ensure
101
- FileUtils.rm_f(@final_spin_lock)
102
- Process.waitpid(pid)
103
- end
104
- end
105
- private :another_process_exclusive_lock
106
-
107
- def test_exclusive_lock
108
- another_process_exclusive_lock{
109
- @stat_file.open{
110
- assert_raise(RuntimeError) {
111
- @stat_file.synchronize{
112
- flunk("don't reach here.")
113
- }
114
- }
115
- }
116
- }
117
- end
118
-
119
- def test_readable_lock_status
120
- assert_raise(Errno::ENOENT) {
121
- @read_file.open
122
- }
123
-
124
- another_process_exclusive_lock{
125
- @read_file.open{
126
- assert_equal(true, @read_file.locked?)
127
- }
128
- }
129
-
130
- @read_file.open{
131
- assert_equal(false, @read_file.locked?)
132
- }
133
- end
134
-
135
- def test_readable_should_be_locked
136
- another_process_exclusive_lock{
137
- @read_file.open{
138
- @read_file.should_be_locked
139
- }
140
- }
141
-
142
- @read_file.open{
143
- assert_raise(RuntimeError) {
144
- @read_file.should_be_locked
145
- }
146
- }
147
- end
148
-
149
- def test_write_read
150
- another_process_exclusive_lock(write_text: "pid: #{$$}") {
151
- @read_file.open{
152
- assert_equal("pid: #{$$}", @read_file.read)
153
- assert_equal("pid: #{$$}", @read_file.read, 'rewind')
154
- }
155
- }
156
-
157
- @read_file.open{
158
- assert_raise(RuntimeError) {
159
- @read_file.read
160
- }
161
- }
162
- end
163
- end
164
- end
165
-
166
- # Local Variables:
167
- # mode: Ruby
168
- # indent-tabs-mode: nil
169
- # End:
@@ -1,72 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- require 'pp' if $DEBUG
4
- require 'test/unit'
5
-
6
- module RIMS::Test
7
- class DaemonWaitpidTest < Test::Unit::TestCase
8
- def setup
9
- @child_pid_list = []
10
- end
11
-
12
- def teardown
13
- for pid in @child_pid_list
14
- begin
15
- unless (Process.waitpid(pid, Process::WNOHANG)) then
16
- Process.kill('KILL', pid)
17
- Process.wait
18
- end
19
- rescue SystemCallError
20
- next
21
- end
22
- end
23
- end
24
-
25
- def fork_child_process
26
- latch_in, latch_out = IO.pipe
27
-
28
- pid = fork{
29
- latch_out.close
30
- latch_in.gets
31
- yield
32
- exit!
33
- }
34
- @child_pid_list << pid
35
- latch_in.close
36
-
37
- return latch_out, pid
38
- end
39
- private :fork_child_process
40
-
41
- def until_child_process_exit
42
- until (pid = yield)
43
- # nothing to do.
44
- end
45
-
46
- pid
47
- end
48
- private :until_child_process_exit
49
-
50
- def test_waitpid
51
- latch_out1, pid1 = fork_child_process{ exit!(0) }
52
- latch_out2, pid2 = fork_child_process{ exit!(1) }
53
- latch_out3, pid3 = fork_child_process{ exit!(2) }
54
-
55
- assert_nil(Process.waitpid(-1, Process::WNOHANG))
56
-
57
- latch_out3.puts
58
- assert_equal(pid3, until_child_process_exit{ Process.waitpid(-1) })
59
-
60
- latch_out1.puts
61
- assert_equal(pid1, until_child_process_exit{ Process.waitpid(-1) })
62
-
63
- latch_out2.puts
64
- assert_equal(pid2, until_child_process_exit{ Process.waitpid(-1) })
65
- end
66
- end
67
- end
68
-
69
- # Local Variables:
70
- # mode: Ruby
71
- # indent-tabs-mode: nil
72
- # End: