Browse code

Structure changes in main file, tests

Cinan Rakosnik authored on 16/12/2012 at 03:20:32
Showing 7 changed files
... ...
@@ -11,29 +11,36 @@ module Rubysermon
11 11
 	MOD_PATH = "#{LIB_PATH}/rubysermon/mod"
12 12
 
13 13
 	class App
14
+		attr_accessor :config_path
14 15
 
15
-		#todo: refactor
16 16
 		def initialize
17
-			@config = {repeat: 60}
18
-			@running_mods = []
19
-			@config_path = "#{LIB_PATH}/rubysermon/config.json"
17
+			@config 		= {repeat: 60}
18
+			@running_mods	= []
19
+			@config_path	= "#{LIB_PATH}/rubysermon/config.json"
20
+			@enabled_mods	= []
21
+		end
20 22
 
21
-			config()
22
-			@enabled_mods = @config[:modules].to_a
23
+		def run
24
+			read_config()
25
+			enable_and_start_mods()
26
+
27
+			if (msg = cannot_start_fetch_process_sleep_cycle?)
28
+				abort(msg)
29
+			end
23 30
 
24
-			start_mods()
25 31
 			start_fetch_process_sleep_cycle()
26 32
 		end
27 33
 
28 34
 		private
29 35
 
30
-		def config
36
+		def read_config
31 37
 			configurator = Configurator.new(@config_path)
32 38
 			config = configurator.get_settings()
33 39
 			@config.merge!(config)
34 40
 		end
35 41
 
36
-		def start_mods
42
+		def enable_and_start_mods
43
+			@enabled_mods = @config[:modules].to_a
37 44
 			@enabled_mods.each do |mod| load_mod(mod) end
38 45
 		end
39 46
 
... ...
@@ -42,15 +49,21 @@ module Rubysermon
42 42
 				mod = ModLoader.load(mod_name)
43 43
 				@running_mods.push(mod)
44 44
 			rescue ModLoaderException => e
45
-				$stderr = e.message
45
+				$stderr.puts e.message
46 46
 			end
47 47
 		end
48 48
 
49
-		def start_fetch_process_sleep_cycle
50
-			if (msg = cannot_start_fetch_process_sleep_cycle?)
51
-				abort(msg)
49
+		def cannot_start_fetch_process_sleep_cycle?
50
+			if @config[:repeat].to_i < 1
51
+				return "Repeat cycle is too short"
52
+			elsif @running_mods.empty?
53
+				return "There are no enabled modules"
52 54
 			end
53 55
 
56
+			false
57
+		end
58
+
59
+		def start_fetch_process_sleep_cycle
54 60
 			while true
55 61
 				current_time = DateTime.now()
56 62
 				@running_mods.each do |mod|
... ...
@@ -60,13 +73,5 @@ module Rubysermon
60 60
 				sleep @config[:repeat]
61 61
 			end
62 62
 		end
63
-
64
-		def cannot_start_fetch_process_sleep_cycle?
65
-			if @config[:repeat].to_i < 1
66
-				return "Repeat cycle too short"
67
-			end
68
-
69
-			false
70
-		end
71 63
 	end
72 64
 end
73 65
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+{}
0 1
\ No newline at end of file
1 2
new file mode 100644
2 3
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+{
1
+  "modules" : []
2
+}
0 3
\ No newline at end of file
1 4
new file mode 100644
... ...
@@ -0,0 +1,7 @@
0
+{
1
+  "modules" : [
2
+    "cpu"
3
+  ],
4
+
5
+  "repeat": 0
6
+}
0 7
\ No newline at end of file
... ...
@@ -2,4 +2,39 @@ require_relative 'test_helper.rb'
2 2
 
3 3
 class Rubysermon_test < MiniTest::Unit::TestCase
4 4
 
5
+	def setup
6
+		@app = Rubysermon::App.new
7
+	end
8
+
9
+	def test_wrong_refresh_time
10
+		@app.config_path = "#{TEST_PATH}/fixtures/short_repeat_period.json"
11
+
12
+		assert_raises(SystemExit) do
13
+			@app.run()
14
+		end
15
+	end
16
+
17
+	def test_empty_modules
18
+		@app.config_path = "#{TEST_PATH}/fixtures/empty_modules.json"
19
+
20
+		assert_raises(SystemExit) do
21
+			@app.run()
22
+		end
23
+	end
24
+
25
+	def test_empty_config1
26
+		@app.config_path = "#{TEST_PATH}/fixtures/empty1.json"
27
+
28
+		assert_raises(SystemExit) do
29
+			@app.run()
30
+		end
31
+	end
32
+
33
+	def test_empty_config2
34
+		@app.config_path = "#{TEST_PATH}/fixtures/empty2.json"
35
+
36
+		assert_raises(SystemExit) do
37
+			@app.run()
38
+		end
39
+	end
5 40
 end
6 41
\ No newline at end of file
... ...
@@ -2,4 +2,6 @@ require 'minitest/autorun'
2 2
 require 'minitest/reporters'
3 3
 
4 4
 MiniTest::Reporters.use!
5
-require File.expand_path('../../lib/rubysermon.rb', __FILE__)
6 5
\ No newline at end of file
6
+require File.expand_path('../../lib/rubysermon.rb', __FILE__)
7
+
8
+TEST_PATH = File.expand_path(File.dirname(__FILE__))
7 9
\ No newline at end of file