chrono_trigger 0.2.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_helper.rb DELETED
@@ -1,14 +0,0 @@
1
- require 'rubygems'
2
- require 'chrono_trigger'
3
- require 'stringio'
4
- require 'test/unit'
5
- require 'shoulda'
6
- require 'mocha'
7
-
8
-
9
- def quietly
10
- old_stderr = $stderr
11
- STDERR.reopen("/dev/null", 'w')
12
- yield
13
- $stderr = old_stderr
14
- end
data/test/test_shell.rb DELETED
@@ -1,52 +0,0 @@
1
- class TestShell < Test::Unit::TestCase
2
-
3
- context "A shell instance, @shell" do
4
- setup do
5
- @shell = ChronoTrigger::Shell.new
6
- end
7
-
8
- should "return an empty array on call to triggers" do
9
- assert_equal [], @shell.triggers
10
- end
11
-
12
- should "raise an exception unless a block is provided to #trigger" do
13
- assert_raise(ChronoTrigger::ConfigurationException) { @shell.trigger("test trigger") }
14
- end
15
-
16
- context "when provided a valid block" do
17
- setup do
18
- @trigger = @shell.trigger("name") { runs { "x" } }
19
- end
20
-
21
- should "create a new trigger" do
22
- assert_equal 1, @shell.triggers.size
23
- end
24
-
25
- should "create a trigger that will run x with execute" do
26
- assert_equal "x", @shell.triggers.first.execute
27
- end
28
-
29
- context "and configured to run the next time shell.execute_triggers is called" do
30
- setup do
31
- ChronoTrigger::CronEntry.any_instance.stubs(:matches?).returns(true)
32
- end
33
-
34
- should "execute the code block" do
35
- @trigger.expects(:execute)
36
- @shell.execute_triggers
37
- end
38
- end #and configured to run the next time shell.execute_triggers is called
39
- end #when provided a valid block
40
-
41
- context "calling load_context with a valid trigger file" do
42
- setup do
43
- @shell.load_triggers(["test/triggers.rb"])
44
- end
45
-
46
- should "create 3 triggers" do
47
- assert_equal 3, @shell.triggers.size
48
- end
49
- end #calling load_context with a valid trigger file
50
- end #A shell instance, @shell
51
-
52
- end
data/test/test_trigger.rb DELETED
@@ -1,170 +0,0 @@
1
- class TestTrigger < Test::Unit::TestCase
2
- context "A Trigger, @trigger," do
3
- setup do
4
- @trigger = ChronoTrigger::Trigger.new("test trigger")
5
- end
6
-
7
- context "and a block of code to run" do
8
- setup do
9
- @block_to_run = "hello"
10
- @trigger.runs { @block_to_run }
11
- end
12
-
13
- should "assign @exec_block on call to runs" do
14
- assert_equal @block_to_run, @trigger.instance_variable_get(:@exec_block).call
15
- end
16
-
17
- should "execute @block_to_run on call to execute" do
18
- assert_equal @block_to_run, @trigger.execute
19
- end
20
-
21
- context "that raises an exception" do
22
- setup do
23
- @trigger.runs { raise Exception.new("test error")}
24
- end
25
-
26
- should "be caught and logged by the trigger" do
27
- assert_nothing_raised do
28
- quietly { @trigger.execute }
29
- end
30
- end
31
- end #that raises an exception
32
- end #and a block of code to run
33
-
34
- context "and a call to #on with some days" do
35
- setup do
36
- @days = [:monday, :wednesday]
37
- @expected_days = [1, 3]
38
- @trigger.on(@days)
39
- end
40
-
41
- should "set the triggers cron entry days" do
42
- assert_equal @expected_days, @trigger.instance_variable_get(:@cron_entry).instance_variable_get(:@days)
43
- end
44
- end #and a call to on with some days
45
-
46
- context "and a call to #at with :hours and :minutes" do
47
- setup do
48
- @hours = 10
49
- @minutes = 5
50
- @trigger.at :hour=>@hours, :minute=>@minutes
51
- end
52
-
53
- should "set the trigger's cron entry's hours" do
54
- assert_equal [@hours], @trigger.instance_variable_get(:@cron_entry).instance_variable_get(:@hours)
55
- end
56
-
57
- should "set the trigger's cron entry's minutes" do
58
- assert_equal [@minutes], @trigger.instance_variable_get(:@cron_entry).instance_variable_get(:@minutes)
59
- end
60
-
61
- context "that are nil" do
62
- setup do
63
- @hours, @minutes = nil, nil
64
- end
65
-
66
- should "raise an exception" do
67
- assert_raise ChronoTrigger::ConfigurationException do
68
- @trigger.at :hours=>@hours, :minutes=>@minutes
69
- end
70
- end
71
- end #that are nil
72
-
73
- context "and a call to #monthly_on" do
74
- setup do
75
- @calendar_days = 15
76
- @trigger.monthly_on @calendar_days
77
- end
78
-
79
- should "set the trigger's cron entry's calendar_days" do
80
- assert_equal [@calendar_days], @trigger.instance_variable_get(:@cron_entry).instance_variable_get(:@calendar_days)
81
- end
82
-
83
- context "with mulitple #monthly_on values" do
84
- setup do
85
- @calendar_days_array = [@calendar_days, 16]
86
- @trigger.monthly_on(@calendar_days_array)
87
- end
88
-
89
- should "set the trigger's cron entry's calendar_days" do
90
- assert_equal @calendar_days_array, @trigger.instance_variable_get(:@cron_entry).instance_variable_get(:@calendar_days)
91
- end
92
- end
93
- end
94
- end #and a call to #at with :hours and :minutes
95
-
96
- context "on a call to #every" do
97
- context "for 10 minutes" do
98
- setup do
99
- @minutes = 10
100
- end
101
-
102
- context "when called" do
103
- setup do
104
- @trigger.every :minutes=>@minutes
105
- end
106
-
107
- should "set the trigger's cron entry's minutes to [0, 10,20,30,40,50]" do
108
- assert_equal [0,10,20,30,40,50], @trigger.instance_variable_get(:@cron_entry).instance_variable_get(:@minutes)
109
- end
110
-
111
- context "in addition to setting #at to hour 2" do
112
- setup do
113
- @hour = 2
114
- @trigger.at :hour=>@hour
115
- end
116
-
117
- should "set the trigger's cron entry's minutes to [0, 10,20,30,40,50]" do
118
- assert_equal [0,10,20,30,40,50], @trigger.instance_variable_get(:@cron_entry).instance_variable_get(:@minutes)
119
- end
120
-
121
- should "set the trigger's cron entry's hours to 2" do
122
- assert_equal [2], @trigger.instance_variable_get(:@cron_entry).instance_variable_get(:@hours)
123
- end
124
- end #in addition to setting #{}
125
- end #when called
126
- end #for 10 minutes
127
-
128
- context "for 11 minutes" do
129
- setup do
130
- @minutes = 11
131
- end
132
-
133
- should "raise an exception" do
134
- assert_raise ChronoTrigger::ConfigurationException do
135
- @trigger.every :minutes=>@minutes
136
- end
137
- end
138
- end #for 11 minutes
139
- end #on a call to #every
140
-
141
- context "and a block that will raise an ActiveRecord::ConnectionNotEstablished exception, then return a value" do
142
- setup do
143
- @value = "hello"
144
-
145
- @run_once = false
146
- @trigger.runs do
147
- if @run_once
148
- @value
149
- else
150
- @run_once = true
151
- raise ActiveRecord::ConnectionNotEstablished
152
- end
153
- end
154
- end
155
-
156
- should "raise an exception, which is caught, then retry the call method" do
157
- ActiveRecord::Base.stubs(:connection).returns(mock({:reconnect! => true}))
158
- assert_equal @value, @trigger.execute
159
- end
160
- end #and a block that will raise an ActiveRecord::ConnectionNotEstablished exception, then return a value
161
-
162
- should "raise an exception on call to #every without minutes or hours" do
163
- assert_raise ChronoTrigger::ConfigurationException do
164
- @trigger.every()
165
- end
166
- end
167
-
168
- end #A Trigger, @trigger,
169
-
170
- end
data/test/triggers.rb DELETED
@@ -1,17 +0,0 @@
1
- trigger "test_trigger" do
2
- runs { "hello world" }
3
- on :monday
4
- at :hour=>3, :minute=>10
5
- end
6
-
7
- trigger "test_trigger_2" do
8
- runs { "hello world 2" }
9
- on :tuesday
10
- every :hours=>2, :minutes=>10
11
- end
12
-
13
- trigger "test_trigger_3" do
14
- runs { "hello world 3" }
15
- monthly_on 15
16
- at :hours=>2, :minutes=>10
17
- end