fsevent 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,167 @@
1
+ # test_framework.rb --- test for framework.rb
2
+ #
3
+ # Copyright (C) 2014 National Institute of Advanced Industrial Science and Technology (AIST)
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'test/unit'
19
+ require 'fsevent'
20
+
21
+ class TestFSEventFramework < Test::Unit::TestCase
22
+
23
+ class TDevice < FSEvent::AbstractDevice
24
+ attr_accessor :test_result
25
+ def initialize(device_name)
26
+ super
27
+ @test_result = []
28
+ end
29
+ end
30
+
31
+ def test_nodevice
32
+ fsevent = FSEvent.new
33
+ assert_nothing_raised { fsevent.start }
34
+ end
35
+
36
+ def test_inactive_device
37
+ fsevent = FSEvent.new
38
+ device = FSEvent::AbstractDevice.new("test_inactive_device")
39
+ assert_nothing_raised { fsevent.start }
40
+ end
41
+
42
+ def test_single_run
43
+ t0 = Time.utc(2000)
44
+ t1 = Time.utc(2001)
45
+ fsevent = FSEvent.new(t0)
46
+ device = TDevice.new("test_single_run")
47
+ device.schedule.merge_schedule([t1])
48
+ def device.run(watched_status_change)
49
+ @test_result << @framework.current_time
50
+ @test_result << watched_status_change
51
+ end
52
+ fsevent.register_device(device)
53
+ assert_nothing_raised { fsevent.start }
54
+ assert_equal([t1, nil], device.test_result)
55
+ end
56
+
57
+ def test_double_run
58
+ t0 = Time.utc(2000)
59
+ t1 = Time.utc(2001)
60
+ t2 = Time.utc(2002)
61
+ fsevent = FSEvent.new(t0)
62
+ device = TDevice.new("test_double_run")
63
+ device.schedule.merge_schedule([t1, t2])
64
+ def device.run(watched_status_change)
65
+ @test_result << @framework.current_time
66
+ @test_result << watched_status_change
67
+ end
68
+ fsevent.register_device(device)
69
+ assert_nothing_raised { fsevent.start }
70
+ assert_equal([t1, nil, t2, nil], device.test_result)
71
+ end
72
+
73
+ def test_repeated_run
74
+ t0 = Time.utc(2000)
75
+ t1 = Time.utc(2001)
76
+ fsevent = FSEvent.new(t0)
77
+ device = TDevice.new("test_repeated_run")
78
+ schedule = FSEvent::PeriodicSchedule.new(t1, 3)
79
+ device.schedule.merge_schedule(schedule)
80
+ def device.run(watched_status_change)
81
+ @test_result << @framework.current_time
82
+ @schedule = [] if 2 < @test_result.length
83
+ end
84
+ fsevent.register_device(device)
85
+ assert_nothing_raised { fsevent.start }
86
+ assert_equal([t1, t1+3, t1+6], device.test_result)
87
+ end
88
+
89
+ def test_twodevice
90
+ t0 = Time.utc(2000)
91
+ t1 = Time.utc(2001)
92
+ t2 = Time.utc(2002)
93
+ fsevent = FSEvent.new(t0)
94
+ device1 = TDevice.new("test_twodevice_1")
95
+ device1.schedule.merge_schedule([t1])
96
+ def device1.run(watched_status_change)
97
+ @test_result << @framework.current_time
98
+ end
99
+ device2 = TDevice.new("test_twodevice_2")
100
+ device2.schedule.merge_schedule([t2])
101
+ def device2.run(watched_status_change)
102
+ @test_result << @framework.current_time
103
+ end
104
+ fsevent.register_device(device1)
105
+ fsevent.register_device(device2)
106
+ assert_nothing_raised { fsevent.start }
107
+ assert_equal([t1], device1.test_result)
108
+ assert_equal([t2], device2.test_result)
109
+ end
110
+
111
+ def test_unregister_in_sleeping
112
+ t0 = Time.utc(2000)
113
+ fsevent = FSEvent.new(t0)
114
+ sched1 = FSEvent::PeriodicSchedule.new(t0+10,5)
115
+ result = []
116
+ device1 = FSEvent::SimpleDevice.new("target", {}, [], 1, sched1) {
117
+ |watched_status_change|
118
+ result << fsevent.current_time
119
+ fsevent.set_elapsed_time(2)
120
+ }
121
+ device2 = FSEvent::SimpleDevice.new("dev", {}, [], 1, [t0+23]) {
122
+ fsevent.unregister_device("target")
123
+ }
124
+ fsevent.register_device device1
125
+ fsevent.register_device device2
126
+ fsevent.start
127
+ assert_equal([t0+10,t0+15,t0+20], result)
128
+ end
129
+
130
+ def test_unregister_in_working
131
+ t0 = Time.utc(2000)
132
+ fsevent = FSEvent.new(t0)
133
+ sched1 = FSEvent::PeriodicSchedule.new(t0+10,5)
134
+ result = []
135
+ device1 = FSEvent::SimpleDevice.new("target", {}, [], 1, sched1) {
136
+ |watched_status_change|
137
+ result << fsevent.current_time
138
+ fsevent.set_elapsed_time(2)
139
+ }
140
+ device2 = FSEvent::SimpleDevice.new("dev", {}, [], 1, [t0+21]) {
141
+ fsevent.unregister_device("target")
142
+ }
143
+ fsevent.register_device device1
144
+ fsevent.register_device device2
145
+ fsevent.start
146
+ assert_equal([t0+10,t0+15,t0+20], result)
147
+ end
148
+
149
+ def test_unregister_self
150
+ t0 = Time.utc(2000)
151
+ fsevent = FSEvent.new(t0)
152
+ sched1 = FSEvent::PeriodicSchedule.new(t0+10,5)
153
+ result = []
154
+ device1 = FSEvent::SimpleDevice.new("target", {}, [], 1, sched1) {
155
+ |watched_status_change|
156
+ result << fsevent.current_time
157
+ if fsevent.current_time == t0+20
158
+ fsevent.unregister_device("target")
159
+ end
160
+ fsevent.set_elapsed_time(2)
161
+ }
162
+ fsevent.register_device device1
163
+ fsevent.start
164
+ assert_equal([t0+10,t0+15,t0+20], result)
165
+ end
166
+
167
+ end
@@ -0,0 +1,76 @@
1
+ # test_processdevice.rb --- tests for processdevice.rb
2
+ #
3
+ # Copyright (C) 2014 National Institute of Advanced Industrial Science and Technology (AIST)
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'test/unit'
19
+ require 'fsevent'
20
+
21
+ class TestFSEventProcessDevice < Test::Unit::TestCase
22
+ def test_empty_definition_and_finish
23
+ d = FSEvent::ProcessDevice.spawner("").new("dname")
24
+ assert_kind_of(FSEvent::ProcessDevice, d)
25
+ assert_equal(nil, d.finish)
26
+ end
27
+
28
+ def test_name_call
29
+ d = FSEvent::ProcessDevice.spawner("").new("dname")
30
+ assert_equal("dname", d.call_subprocess(:name))
31
+ ensure
32
+ d.finish
33
+ end
34
+
35
+ def test_eval
36
+ d = FSEvent::ProcessDevice.spawner(<<-'End').new("dname")
37
+ def test_eval(str) eval(str) end
38
+ End
39
+ assert_equal("foo", d.call_subprocess(:test_eval, '"foo"'))
40
+ ensure
41
+ d.finish
42
+ end
43
+
44
+ def test_upcall_simple
45
+ framework = "123456"
46
+ d = FSEvent::ProcessDevice.spawner(<<-'End').new("dname")
47
+ def test_eval(str) eval(str) end
48
+ End
49
+ d.framework = framework
50
+ d.registered
51
+ assert_equal(123456,
52
+ d.call_subprocess(:test_eval, 'call_parent(:to_i)'))
53
+ ensure
54
+ d.finish
55
+ end
56
+
57
+ def test_upcall_add_watch
58
+ framework = Object.new
59
+ def framework.add_watch(watchee_device_name, status_name)
60
+ @ary ||= []
61
+ @ary << watchee_device_name
62
+ @ary << status_name
63
+ nil
64
+ end
65
+ d = FSEvent::ProcessDevice.spawner(<<-'End').new("dname")
66
+ def test_eval(str) eval(str) end
67
+ End
68
+ d.framework = framework
69
+ d.registered
70
+ d.call_subprocess(:test_eval, '@framework.add_watch(1,2)')
71
+ assert_equal([1,2], framework.instance_variable_get(:@ary))
72
+ ensure
73
+ d.finish
74
+ end
75
+
76
+ end
data/test/test_util.rb ADDED
@@ -0,0 +1,53 @@
1
+ # test_util.rb --- tests for util.rb
2
+ #
3
+ # Copyright (C) 2014 National Institute of Advanced Industrial Science and Technology (AIST)
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'test/unit'
19
+ require 'fsevent'
20
+
21
+ class TestFSEventUtil < Test::Unit::TestCase
22
+ def test_nested_hash_level1
23
+ h = FSEvent::Util.nested_hash(1)
24
+ assert_equal(0, h.size)
25
+ assert_equal(nil, h[:k])
26
+ assert_equal(0, h.size)
27
+ end
28
+
29
+ def test_nested_hash_level2
30
+ h = FSEvent::Util.nested_hash(2)
31
+ assert_equal(0, h.size)
32
+ h2 = h[:k1]
33
+ assert_equal({}, h2)
34
+ assert_same(h2, h[:k1])
35
+ assert_equal(1, h.size)
36
+ assert_equal(nil, h[:k1][:k2])
37
+ assert_equal(0, h2.size)
38
+ end
39
+
40
+ def test_nested_hash_level3
41
+ h = FSEvent::Util.nested_hash(3)
42
+ assert_equal(0, h.size)
43
+ h2 = h[:k1]
44
+ assert_equal({}, h2)
45
+ assert_same(h2, h[:k1])
46
+ assert_equal(1, h.size)
47
+ h3 = h[:k1][:k2]
48
+ assert_equal({}, h3)
49
+ assert_same(h3, h[:k1][:k2])
50
+ assert_equal(1, h2.size)
51
+ end
52
+
53
+ end
@@ -0,0 +1,169 @@
1
+ # test_watch.rb --- tests for watching device status
2
+ #
3
+ # Copyright (C) 2014 National Institute of Advanced Industrial Science and Technology (AIST)
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'test/unit'
19
+ require 'fsevent'
20
+
21
+ class TestFSEventWatch < Test::Unit::TestCase
22
+ def test_srcdevice
23
+ t = Time.utc(2000)
24
+ fsevent = FSEvent.new(t)
25
+ values = [9,2,3,5,1]
26
+ src_sched = values.map.with_index {|v, i| t + (i+1)*10 }
27
+ srcdevice = FSEvent::SimpleDevice.new("src", {"s"=>0}, [], 1, src_sched) {
28
+ |watched_status_change|
29
+ fsevent.set_elapsed_time(5)
30
+ fsevent.status_changed "s", values.shift
31
+ }
32
+ fsevent.register_device(srcdevice)
33
+ fsevent.start
34
+ assert_equal([], values)
35
+ end
36
+
37
+ def test_watch_register_order
38
+ 2.times {|i|
39
+ t = Time.utc(2000)
40
+ fsevent = FSEvent.new(t)
41
+ values = [9,2,3,5,1]
42
+ src_sched = values.map.with_index {|v, i| t + (i+1)*10 }
43
+ srcdevice = FSEvent::SimpleDevice.new("src", {"s"=>0}, [], 1, src_sched) {
44
+ |watched_status_change|
45
+ fsevent.set_elapsed_time(5)
46
+ fsevent.status_changed "s", values.shift
47
+ }
48
+ test_result = []
49
+ dstdevice = FSEvent::SimpleDevice.new("dst", {}, [["src","s"]], 1) {
50
+ |watched_status_change|
51
+ fsevent.set_elapsed_time(1)
52
+ test_result << [fsevent.current_time, watched_status_change]
53
+ }
54
+ if i == 0
55
+ fsevent.register_device(srcdevice)
56
+ fsevent.register_device(dstdevice)
57
+ else
58
+ fsevent.register_device(dstdevice)
59
+ fsevent.register_device(srcdevice)
60
+ end
61
+ fsevent.start
62
+ assert_equal(
63
+ [[t + 1, {"src"=>{"s"=>0}}],
64
+ [t + 10*1+5, {"src"=>{"s"=>9}}],
65
+ [t + 10*2+5, {"src"=>{"s"=>2}}],
66
+ [t + 10*3+5, {"src"=>{"s"=>3}}],
67
+ [t + 10*4+5, {"src"=>{"s"=>5}}],
68
+ [t + 10*5+5, {"src"=>{"s"=>1}}]],
69
+ test_result)
70
+ }
71
+ end
72
+
73
+ def test_wakeup_immediate
74
+ t = Time.utc(2000)
75
+ fsevent = FSEvent.new(t)
76
+ srcdevice = FSEvent::SimpleDevice.new("src", {"s"=>0}, [], 1, [t+10]) {
77
+ |watched_status_change|
78
+ fsevent.set_elapsed_time(5)
79
+ fsevent.status_changed "s", 100
80
+ }
81
+ test_result = []
82
+ dstdevice = FSEvent::SimpleDevice.new("dst", {}, [["src","s", :immediate]], 1, [t+20]) {
83
+ |watched_status_change|
84
+ fsevent.set_elapsed_time(1)
85
+ test_result << [fsevent.current_time, watched_status_change]
86
+ }
87
+ fsevent.register_device(srcdevice)
88
+ fsevent.register_device(dstdevice)
89
+ fsevent.start
90
+ assert_equal(
91
+ [[t + 1, {"src"=>{"s"=>0}}],
92
+ [t + 15, {"src"=>{"s"=>100}}]],
93
+ test_result)
94
+ end
95
+
96
+ def test_wakeup_immediate_only_at_beginning
97
+ t = Time.utc(2000)
98
+ fsevent = FSEvent.new(t)
99
+ srcdevice = FSEvent::SimpleDevice.new("src", {"s"=>0}, [], 1, [t+10]) {
100
+ |watched_status_change|
101
+ fsevent.set_elapsed_time(5)
102
+ fsevent.status_changed "s", 100
103
+ }
104
+ test_result = []
105
+ dstdevice = FSEvent::SimpleDevice.new("dst", {}, [["src","s", :immediate_only_at_beginning]], 1, [t+20]) {
106
+ |watched_status_change|
107
+ fsevent.set_elapsed_time(1)
108
+ test_result << [fsevent.current_time, watched_status_change]
109
+ }
110
+ fsevent.register_device(srcdevice)
111
+ fsevent.register_device(dstdevice)
112
+ fsevent.start
113
+ assert_equal(
114
+ [[t + 1, {"src"=>{"s"=>0}}],
115
+ [t + 20, {"src"=>{"s"=>100}}]],
116
+ test_result)
117
+ end
118
+
119
+ def test_wakeup_schedule
120
+ t = Time.utc(2000)
121
+ fsevent = FSEvent.new(t)
122
+ srcdevice = FSEvent::SimpleDevice.new("src", {"s"=>0}, [], 1, [t+10]) {
123
+ |watched_status_change|
124
+ fsevent.set_elapsed_time(5)
125
+ fsevent.status_changed "s", 100
126
+ }
127
+ test_result = []
128
+ dstdevice = FSEvent::SimpleDevice.new("dst", {}, [["src","s", :schedule]], 1, [t+20]) {
129
+ |watched_status_change|
130
+ fsevent.set_elapsed_time(1)
131
+ test_result << [fsevent.current_time, watched_status_change]
132
+ }
133
+ fsevent.register_device(srcdevice)
134
+ fsevent.register_device(dstdevice)
135
+ fsevent.start
136
+ assert_equal(
137
+ [[t + 20, {"src"=>{"s"=>100}}]],
138
+ test_result)
139
+ end
140
+
141
+ def test_unwatch
142
+ t = Time.utc(2000)
143
+ fsevent = FSEvent.new(t)
144
+ s = 0
145
+ srcdevice = FSEvent::SimpleDevice.new("src", {"s"=>0}, [], 1, [t+10, t+20]) {
146
+ |watched_status_change|
147
+ fsevent.set_elapsed_time(5)
148
+ s += 100
149
+ fsevent.status_changed "s", s
150
+ }
151
+ test_result = []
152
+ dstdevice = FSEvent::SimpleDevice.new("dst", {}, [["src","s", :schedule]], 1, [t+17, t+27]) {
153
+ |watched_status_change|
154
+ fsevent.set_elapsed_time(1)
155
+ if fsevent.current_time == t+17
156
+ fsevent.del_watch("src", "s")
157
+ end
158
+ test_result << [fsevent.current_time, watched_status_change]
159
+ }
160
+ fsevent.register_device(srcdevice)
161
+ fsevent.register_device(dstdevice)
162
+ fsevent.start
163
+ assert_equal(
164
+ [[t+17, {"src"=>{"s"=>100}}],
165
+ [t+27, nil]],
166
+ test_result)
167
+ end
168
+
169
+ end