rorvswild 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,36 +0,0 @@
1
- require File.expand_path("#{File.dirname(__FILE__)}/../helper")
2
-
3
- require "sidekiq"
4
- require "sidekiq/testing"
5
-
6
- class RorVsWild::Plugin::SidekiqTest < Minitest::Test
7
- include RorVsWildAgentHelper
8
-
9
- Sidekiq::Testing.server_middleware do |chain|
10
- chain.add(RorVsWild::Plugin::Sidekiq)
11
- end
12
-
13
- class SampleJob
14
- include ::Sidekiq::Worker
15
-
16
- # SampleSidekiqJob.perform_async(1)
17
- def perform(arg)
18
- raise "Exception" unless arg
19
- end
20
- end
21
-
22
- def test_callback
23
- agent.expects(:post_job)
24
- Sidekiq::Testing.inline! { SampleJob.perform_async(1) }
25
- assert_equal("RorVsWild::Plugin::SidekiqTest::SampleJob", agent.data[:name])
26
- end
27
-
28
- def test_callback_on_exception
29
- agent.expects(:post_job)
30
- Sidekiq::Testing.inline! { SampleJob.perform_async(false) }
31
- rescue
32
- ensure
33
- assert_equal([false], agent.data[:error][:parameters])
34
- end
35
- end
36
-
data/test/queue_test.rb DELETED
@@ -1,56 +0,0 @@
1
- require File.expand_path("#{File.dirname(__FILE__)}/helper")
2
-
3
- class QueueTest < Minitest::Test
4
- include RorVsWildAgentHelper
5
-
6
- def test_push_job
7
- queue.start_thread
8
- queue.thread.expects(:wakeup)
9
- 10.times { queue.push_job(1) }
10
- assert_equal(10, queue.jobs.size)
11
- end
12
-
13
- def test_push_request
14
- queue.start_thread
15
- queue.thread.expects(:wakeup)
16
- 10.times { queue.push_request(1) }
17
- assert_equal(10, queue.requests.size)
18
- end
19
-
20
- def test_pull_jobs
21
- queue.push_job(1)
22
- assert_equal([1], queue.pull_jobs)
23
- refute(queue.pull_jobs)
24
- end
25
-
26
- def test_pull_requests
27
- queue.push_request(1)
28
- assert_equal([1], queue.pull_requests)
29
- refute(queue.pull_requests)
30
- end
31
-
32
- def test_flush_when_jobs_are_present
33
- queue.client.expects(:post)
34
- queue.push_job(1)
35
- queue.flush
36
- end
37
-
38
- def test_flush_when_requests_are_present
39
- queue.client.expects(:post)
40
- queue.push_request(1)
41
- queue.flush
42
- end
43
-
44
- def test_flush_when_empty
45
- queue.client.expects(:post).never
46
- queue.flush
47
- end
48
-
49
- def queue
50
- @queue ||= agent.instance_variable_get(:@queue)
51
- end
52
-
53
- def client
54
- @client ||= agent.instance_variable_get(:@client)
55
- end
56
- end
@@ -1,153 +0,0 @@
1
- require File.expand_path("#{File.dirname(__FILE__)}/helper")
2
-
3
- class RorVsWildTest < Minitest::Test
4
- include TopTests
5
-
6
- def test_measure_code
7
- agent.expects(:post_job)
8
- assert_equal(2, agent.measure_code("1 + 1"))
9
- assert_equal("1 + 1", agent.send(:data)[:name])
10
- assert(agent.send(:data)[:runtime] > 0)
11
- end
12
-
13
- def test_measure_code_when_raising
14
- agent.expects(:post_job)
15
- assert_raises(RuntimeError) { agent.measure_code("raise 'error'") }
16
- assert_equal(("raise 'error'"), agent.send(:data)[:name])
17
- assert(agent.send(:data)[:runtime])
18
- assert(agent.send(:data)[:error])
19
- end
20
-
21
- def test_mesure_block_when_exception_is_ignored
22
- agent = initialize_agent(ignored_exceptions: %w[ZeroDivisionError])
23
- agent.expects(:post_job)
24
- assert_raises(ZeroDivisionError) { RorVsWild.measure_code("1/0") }
25
- refute(agent.send(:data)[:error])
26
- end
27
-
28
- def test_measure_code_when_no_agent
29
- RorVsWild.instance_variable_set(:@agent, nil)
30
- RorVsWild::Agent.any_instance.expects(:post_job).never
31
- assert_equal(2, RorVsWild.measure_code("1+1"))
32
- end
33
-
34
- def test_measure_block_when_no_agent
35
- RorVsWild.instance_variable_set(:@agent, nil)
36
- RorVsWild::Agent.any_instance.expects(:post_job).never
37
- assert_equal(2, RorVsWild.measure_block("1+1") { 1+1 })
38
- end
39
-
40
- def test_measure_block_recursive
41
- agent.expects(:post_job)
42
- result = RorVsWild.measure_block("1") do
43
- RorVsWild.measure_block("2") { 1 } + 1
44
- end
45
- assert_equal(2, result)
46
- end
47
-
48
- def test_catch_error
49
- agent.expects(:post_error)
50
- exception = RorVsWild.catch_error { 1 / 0 }
51
- assert_equal(ZeroDivisionError, exception.class)
52
- end
53
-
54
- def test_catch_error_with_extra_details
55
- agent.expects(:post_error)
56
- exception = RorVsWild.catch_error(foo: "bar") { 1 / 0 }
57
- assert_equal(ZeroDivisionError, exception.class)
58
- end
59
-
60
- def test_catch_error_when_no_errors
61
- agent.expects(:post_error).never
62
- assert_equal(2, RorVsWild.catch_error { 1 + 1 })
63
- end
64
-
65
- def test_extract_most_relevant_file_and_line
66
- callstack = [
67
- stub(path: "#{ENV["GEM_HOME"]}/lib/sql.rb", lineno: 1),
68
- stub(path: "/usr/lib/ruby/net/http.rb", lineno: 2),
69
- stub(path: "/rails/root/app/models/user.rb", lineno: 3),
70
- ]
71
- assert_equal(["/app/models/user.rb", 3], agent.extract_most_relevant_file_and_line(callstack))
72
-
73
- locations = [stub(path: "#{ENV["GEM_HOME"]}/lib/sql.rb", lineno: 1)]
74
- assert_equal(["#{ENV["GEM_HOME"]}/lib/sql.rb", 1], agent.extract_most_relevant_file_and_line(locations))
75
- end
76
-
77
- def test_extract_most_relevant_file_and_line_when_there_is_not_app_root
78
- agent = initialize_agent
79
- callstack = [
80
- stub(path: "#{ENV["GEM_HOME"]}/lib/sql.rb", lineno: 1),
81
- stub(path: "/usr/lib/ruby/net/http.rb", lineno: 2),
82
- stub(path: "/rails/root/app/models/user.rb", lineno: 3),
83
- ]
84
- assert_equal(["/usr/lib/ruby/net/http.rb", 2], agent.extract_most_relevant_file_and_line(callstack))
85
- end
86
-
87
- def test_extract_most_relevant_file_and_line_when_there_is_no_method_name
88
- assert_equal(["/foo/bar.rb", 123], agent.extract_most_relevant_file_and_line([stub(path: "/foo/bar.rb", lineno:123)]))
89
- end
90
-
91
- def test_extract_most_relevant_file_and_line_when_gem_home_is_in_heroku_app_root
92
- agent = initialize_agent(app_root: app_root = File.dirname(gem_home = ENV["GEM_HOME"]))
93
- callstack = [
94
- stub(path: "#{gem_home}/lib/sql.rb", lineno: 1),
95
- stub(path: "/usr/lib/ruby/net/http.rb", lineno: 2),
96
- stub(path: "#{app_root}/app/models/user.rb", lineno: 3)
97
- ]
98
- assert_equal(["/app/models/user.rb", 3], agent.extract_most_relevant_file_and_line(callstack))
99
- end
100
-
101
- def test_extract_most_relevant_file_and_line_when_gem_path_is_set_instead_of_gem_home
102
- original_gem_home, original_gem_path = ENV["GEM_HOME"], ENV["GEM_PATH"]
103
- ENV["GEM_HOME"], ENV["GEM_PATH"] = "", "/gem/path"
104
-
105
- callstack = [
106
- stub(path: "/gem/path/lib/sql.rb", lineno:1),
107
- stub(path: "/usr/lib/ruby/net/http.rb", lineno: 2),
108
- stub(path: "/rails/root/app/models/user.rb",lineno: 3),
109
- ]
110
- assert_equal(["/app/models/user.rb", 3], agent.extract_most_relevant_file_and_line(callstack))
111
- ensure
112
- ENV["GEM_HOME"], ENV["GEM_PATH"] = original_gem_home, original_gem_path
113
- end
114
-
115
- def test_extract_most_relevant_file_and_line_when_gem_path_and_gem_home_are_undefined
116
- original_gem_home, original_gem_path = ENV["GEM_HOME"], ENV["GEM_PATH"]
117
- ENV["GEM_HOME"], ENV["GEM_PATH"] = "", ""
118
-
119
- callstack = [
120
- stub(path: "/gem/path/lib/sql.rb", lineno: 1),
121
- stub(path: "/usr/lib/ruby/net/http.rb", lineno: 2),
122
- stub(path: "/rails/root/app/models/user.rb", lineno: 3),
123
- ]
124
- assert_equal(["/app/models/user.rb", 3], agent.extract_most_relevant_file_and_line(callstack))
125
- ensure
126
- ENV["GEM_HOME"], ENV["GEM_PATH"] = original_gem_home, original_gem_path
127
- end
128
-
129
- def test_extract_most_relevant_file_and_line_from_array_of_strings
130
- callstack = ["#{ENV["GEM_HOME"]}/lib/sql.rb:1", "/usr/lib/ruby/net/http.rb:2", "/rails/root/app/models/user.rb:3"]
131
- assert_equal(["/app/models/user.rb", "3"], agent.extract_most_relevant_file_and_line_from_array_of_strings(callstack))
132
-
133
- locations = ["#{ENV["GEM_HOME"]}/lib/sql.rb:1"]
134
- assert_equal(["#{ENV["GEM_HOME"]}/lib/sql.rb", "1"], agent.extract_most_relevant_file_and_line_from_array_of_strings(locations))
135
- end
136
-
137
- def test_extract_most_relevant_file_and_line_from_exception_when_exception_has_no_backtrace
138
- assert_equal(["No backtrace", 1], agent.extract_most_relevant_file_and_line_from_exception(StandardError.new))
139
- end
140
-
141
- private
142
-
143
- def agent
144
- @agent ||= initialize_agent(app_root: "/rails/root")
145
- end
146
-
147
- def initialize_agent(options = {})
148
- agent ||= RorVsWild.start({logger: "/dev/null"}.merge(options))
149
- agent.stubs(:post_request)
150
- agent.stubs(:post_task)
151
- agent
152
- end
153
- end
data/test/run.rb DELETED
@@ -1,3 +0,0 @@
1
- require File.expand_path("#{File.dirname(__FILE__)}/helper")
2
-
3
- Dir.glob("test/**/*_test.rb").each { |file_path| require File.expand_path(file_path) }
data/test/section_test.rb DELETED
@@ -1,77 +0,0 @@
1
- require File.expand_path("#{File.dirname(__FILE__)}/helper")
2
-
3
- class RorVsWild::SectionTest < Minitest::Test
4
- include RorVsWildAgentHelper
5
-
6
- def setup
7
- agent
8
- end
9
-
10
- def test_sibling?
11
- refute(section1.sibling?(section2))
12
- refute(section2.sibling?(section1))
13
-
14
- section2.line = section1.line
15
- assert(section1.sibling?(section2))
16
- assert(section2.sibling?(section1))
17
- end
18
-
19
- def test_merge
20
- section1.merge(section2)
21
- assert_equal(3, section1.calls)
22
- assert_equal(3, section1.total_runtime)
23
- assert_equal(3, section1.children_runtime)
24
- assert_equal("command1", section1.command)
25
- end
26
-
27
- def test_merge_with_appendable_command
28
- section3.merge(section1)
29
- assert_equal("command3\ncommand1", section3.command)
30
- end
31
-
32
- def section1
33
- unless @section1
34
- s = RorVsWild::Section.new
35
- s.kind = "test"
36
- s.file = "file"
37
- s.line = 1
38
- s.calls = 1
39
- s.total_runtime = 1
40
- s.children_runtime = 1
41
- s.command = "command1"
42
- @section1 = s
43
- end
44
- @section1
45
- end
46
-
47
- def section2
48
- unless @section2
49
- s = RorVsWild::Section.new
50
- s.kind = "test"
51
- s.file = "file"
52
- s.line = 2
53
- s.calls = 2
54
- s.total_runtime = 2
55
- s.children_runtime = 2
56
- s.command = "command2"
57
- @section2 = s
58
- end
59
- @section2
60
- end
61
-
62
- def section3
63
- unless @section3
64
- s = RorVsWild::Section.new
65
- s.kind = "test"
66
- s.file = "file"
67
- s.line = 3
68
- s.calls = 0
69
- s.total_runtime = 3
70
- s.children_runtime = 3
71
- s.command = "command3"
72
- s.appendable_command = true
73
- @section3 = s
74
- end
75
- @section3
76
- end
77
- end