rorvswild 0.6.1 → 1.0.0.pre.alpha

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.
@@ -0,0 +1,27 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../helper")
2
+
3
+ require "active_job"
4
+
5
+ class RorVsWild::Plugin::ActiveMailerTest < Minitest::Test
6
+ include RorVsWildAgentHelper
7
+
8
+ def test_callback
9
+ line = nil
10
+ agent = initialize_agent(app_root: File.dirname(__FILE__))
11
+ agent.measure_block("test") do
12
+ ActiveSupport::Notifications.instrument("deliver.action_mailer", {mailer: "Mailer"}) do line = __LINE__
13
+ sleep 0.01
14
+ end
15
+ end
16
+
17
+ section = agent.data[:sections][0]
18
+
19
+ assert_equal(1, agent.data[:sections].size)
20
+ assert_equal("Mailer", section.command)
21
+ assert_equal(line, section.line.to_i)
22
+ assert_equal("mail", section.kind)
23
+ assert(section.self_runtime > 10)
24
+ assert_equal(1, section.calls)
25
+ end
26
+ end
27
+
@@ -0,0 +1,40 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../helper")
2
+
3
+ require "active_job"
4
+
5
+ class RorVsWild::Plugin::ActionViewTest < Minitest::Test
6
+ include RorVsWildAgentHelper
7
+
8
+ def test_render_template_callback
9
+ agent.measure_block("test") do
10
+ ActiveSupport::Notifications.instrument("render_template.action_view", {identifier: "template.html.erb"}) do
11
+ ActiveSupport::Notifications.instrument("render_partial.action_view", {identifier: "_partial.html.erb"}) do
12
+ ActiveSupport::Notifications.instrument("render_partial.action_view", {identifier: "_sub_partial.html.erb"}) do
13
+ sleep 0.03
14
+ end
15
+ sleep 0.02
16
+ end
17
+ sleep 0.01
18
+ end
19
+ end
20
+
21
+ sections = agent.data[:sections]
22
+ sub_partial, partial, template = sections[0], sections[1], sections[2]
23
+ assert_equal(3, sections.size)
24
+
25
+ assert_equal("view", sub_partial.kind)
26
+ assert_equal("_sub_partial.html.erb", sub_partial.command)
27
+
28
+ assert_equal("view", partial.kind)
29
+ assert_equal("_partial.html.erb", partial.command)
30
+
31
+ assert_equal("view", template.kind)
32
+ assert_equal("template.html.erb", template.command)
33
+
34
+ assert(sub_partial.self_runtime > partial.self_runtime)
35
+ assert(partial.self_runtime > template.self_runtime)
36
+ assert(partial.total_runtime < template.total_runtime)
37
+ assert(sub_partial.total_runtime < partial.total_runtime)
38
+ end
39
+ end
40
+
@@ -0,0 +1,21 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../helper")
2
+
3
+ require "active_job"
4
+
5
+ class RorVsWild::Plugin::ActiveJobTest < Minitest::Test
6
+ include RorVsWildAgentHelper
7
+
8
+ class SampleJob < ::ActiveJob::Base
9
+ queue_as :default
10
+
11
+ def perform
12
+ end
13
+ end
14
+
15
+ def test_callback
16
+ ActiveJob::Base.logger = Logger.new("/dev/null")
17
+ agent.expects(:post_job)
18
+ SampleJob.perform_now
19
+ end
20
+ end
21
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../helper")
2
+
3
+ require "active_job"
4
+
5
+ class RorVsWild::Plugin::ActiveRecordTest < Minitest::Test
6
+ include RorVsWildAgentHelper
7
+
8
+ def test_render_template_callback
9
+ line1, line2 = nil
10
+ agent = initialize_agent(app_root: File.dirname(__FILE__))
11
+ agent.measure_block("test") do
12
+ ActiveSupport::Notifications.instrument("sql.active_record", {sql: "SELECT COUNT(*) FROM users"}) do line1 = __LINE__
13
+ sleep 0.01
14
+ end
15
+ 2.times do
16
+ ActiveSupport::Notifications.instrument("sql.active_record", {sql: "SELECT * FROM users"}) do line2 = __LINE__
17
+ sleep 0.02
18
+ end
19
+ end
20
+ end
21
+
22
+ sections = agent.data[:sections]
23
+ sql1, sql2 = sections[0], sections[1]
24
+ assert_equal(2, sections.size)
25
+
26
+ assert_equal("sql", sql1.kind)
27
+ assert_equal("SELECT COUNT(*) FROM users", sql1.command)
28
+ assert_equal(line1, sql1.line.to_i)
29
+ assert_equal(1, sql1.calls)
30
+ assert(sql1.self_runtime > 10)
31
+
32
+ assert_equal("sql", sql2.kind)
33
+ assert_equal("SELECT * FROM users", sql2.command)
34
+ assert_equal(line2, sql2.line.to_i)
35
+ assert(sql2.self_runtime > 40)
36
+ assert_equal(2, sql2.calls)
37
+ end
38
+ end
39
+
@@ -0,0 +1,35 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../helper")
2
+
3
+ require "delayed_job"
4
+
5
+ class RorVsWild::Plugin::DelayedJobTest < Minitest::Test
6
+ include RorVsWildAgentHelper
7
+
8
+ class SampleJob
9
+ def perform
10
+ end
11
+ end
12
+
13
+ class SampleBackend
14
+ include Delayed::Backend::Base
15
+
16
+ attr_accessor :id, :attempts
17
+
18
+ def initialize(options)
19
+ end
20
+
21
+ def payload_object
22
+ SampleJob.new
23
+ end
24
+
25
+ def destroy
26
+ end
27
+ end
28
+
29
+ def test_callback
30
+ agent.expects(:post_job)
31
+ Delayed::Worker.delay_jobs = false
32
+ SampleBackend.enqueue(SampleJob.new)
33
+ end
34
+ end
35
+
@@ -3,6 +3,8 @@ require File.expand_path("#{File.dirname(__FILE__)}/../helper")
3
3
  require "mongo"
4
4
 
5
5
  class RorVsWild::Plugin::MongoTest < Minitest::Test
6
+ include RorVsWildAgentHelper
7
+
6
8
  Mongo::Logger.logger.level = ::Logger::FATAL
7
9
 
8
10
  def test_callback
@@ -10,26 +12,13 @@ class RorVsWild::Plugin::MongoTest < Minitest::Test
10
12
  {name: "Mont Blanc", altitude: 4807},
11
13
  {name: "Mont Cervin", altitude: 4478},
12
14
  ]
13
- client.measure_block("mongo") do
14
- client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
15
- mountains.each { |m| client[:mountains].insert_one(m) }
15
+ agent.measure_block("mongo") do
16
+ agent = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
17
+ mountains.each { |m| agent[:mountains].insert_one(m) }
16
18
  end
17
- assert_equal(1, client.send(:queries).size)
18
- assert_equal(2, client.send(:queries)[0][:times])
19
- assert_equal("mongo", client.send(:queries)[0][:kind])
20
- assert_match('{"insert"=>"mountains", "documents"=>', client.send(:queries)[0][:command])
21
- end
22
-
23
- private
24
-
25
- def client
26
- @client ||= initialize_client(app_root: "/rails/root")
27
- end
28
-
29
- def initialize_client(options = {})
30
- client = RorVsWild::Client.new(options)
31
- client.stubs(:post_request)
32
- client.stubs(:post_job)
33
- client
19
+ assert_equal(1, agent.data[:sections].size)
20
+ assert_equal(2, agent.data[:sections][0].calls)
21
+ assert_equal("mongo", agent.data[:sections][0].kind)
22
+ assert_match('{"insert"=>"mountains", "documents"=>', agent.data[:sections][0].command)
34
23
  end
35
24
  end
@@ -3,39 +3,28 @@ require File.expand_path("#{File.dirname(__FILE__)}/../helper")
3
3
  require "net/http"
4
4
 
5
5
  class RorVsWild::Plugin::NetHttpTest < Minitest::Test
6
+ include RorVsWildAgentHelper
7
+
6
8
  def test_callback
7
- client.measure_block("test") { Net::HTTP.get("ruby-lang.org", "/index.html") }
8
- assert_equal(1, client.send(:queries).size)
9
- assert_equal(1, client.send(:queries)[0][:times])
10
- assert_equal("http", client.send(:queries)[0][:kind])
11
- assert_match("GET http://ruby-lang.org/index.html", client.send(:queries)[0][:command])
9
+ agent.measure_block("test") { Net::HTTP.get("ruby-lang.org", "/index.html") }
10
+ assert_equal(1, agent.data[:sections].size)
11
+ assert_equal(1, agent.data[:sections][0].calls)
12
+ assert_equal("http", agent.data[:sections][0].kind)
13
+ assert_match("GET http://ruby-lang.org/index.html", agent.data[:sections][0].command)
12
14
  end
13
15
 
14
16
  def test_callback_with_https
15
- client.measure_block("test") { Net::HTTP.get(URI("https://www.ruby-lang.org/index.html")) }
16
- assert_match("GET https://www.ruby-lang.org/index.html", client.send(:queries)[0][:command])
17
- assert_equal("http", client.send(:queries)[0][:kind])
17
+ agent.measure_block("test") { Net::HTTP.get(URI("https://www.ruby-lang.org/index.html")) }
18
+ assert_match("GET https://www.ruby-lang.org/index.html", agent.data[:sections][0].command)
19
+ assert_equal("http", agent.data[:sections][0].kind)
18
20
  end
19
21
 
20
22
  def test_nested_query_because_net_http_request_is_recursive_when_connection_is_not_started
21
- client.measure_block("test") do
23
+ agent.measure_block("test") do
22
24
  uri = URI("http://www.ruby-lang.org/index.html")
23
25
  http = Net::HTTP.new(uri.host, uri.port)
24
26
  http.request(Net::HTTP::Get.new(uri.path))
25
27
  end
26
- assert_equal(1, client.send(:queries)[0][:times])
27
- end
28
-
29
- private
30
-
31
- def client
32
- @client ||= initialize_client(app_root: "/rails/root")
33
- end
34
-
35
- def initialize_client(options = {})
36
- client = RorVsWild::Client.new(options)
37
- client.stubs(:post_request)
38
- client.stubs(:post_job)
39
- client
28
+ assert_equal(1, agent.data[:sections][0].calls)
40
29
  end
41
30
  end
@@ -3,40 +3,28 @@ require File.expand_path("#{File.dirname(__FILE__)}/../helper")
3
3
  require "redis"
4
4
 
5
5
  class RorVsWild::Plugin::RedisTest < Minitest::Test
6
+ include RorVsWildAgentHelper
7
+
6
8
  def test_callback
7
- client.measure_code("::Redis.new.get('foo')")
8
- assert_equal(1, client.send(:queries).size)
9
- assert_equal("redis", client.send(:queries)[0][:kind])
10
- assert_equal("get foo", client.send(:queries)[0][:command])
9
+ agent.measure_code("::Redis.new.get('foo')")
10
+ assert_equal(1, agent.data[:sections].size)
11
+ assert_equal("redis", agent.data[:sections][0].kind)
12
+ assert_equal("get foo", agent.data[:sections][0].command)
11
13
  end
12
14
 
13
- def test_callback_with_pipeline
14
- skip
15
- client.measure_block("pipeline") do
16
- redis = ::Redis.new
17
- redis.get("foo")
18
- redis.set("foo", "bar")
15
+ def test_callback_when_pipelined
16
+ agent.measure_block("pipeline") do
17
+ (redis = ::Redis.new).pipelined do
18
+ redis.get("foo")
19
+ redis.set("foo", "bar")
20
+ end
19
21
  end
20
- assert_equal(2, client.send(:queries).size)
21
- assert_equal("redis", client.send(:queries)[0][:kind])
22
- assert_equal("get foo", client.send(:queries)[0][:command])
23
- assert_equal("set foo bar", client.send(:queries)[1][:command])
22
+ assert_equal(1, agent.data[:sections].size)
23
+ assert_equal("redis", agent.data[:sections][0].kind)
24
+ assert_equal("get foo\nset foo bar", agent.data[:sections][0].command)
24
25
  end
25
26
 
26
27
  def test_commands_to_string_hide_auth_password
27
28
  assert_equal("auth *****", RorVsWild::Plugin::Redis.commands_to_string([[:auth, "SECRET"]]))
28
29
  end
29
-
30
- private
31
-
32
- def client
33
- @client ||= initialize_client(app_root: "/rails/root")
34
- end
35
-
36
- def initialize_client(options = {})
37
- client ||= RorVsWild::Client.new(options)
38
- client.stubs(:post_request)
39
- client.stubs(:post_job)
40
- client
41
- end
42
30
  end
@@ -0,0 +1,141 @@
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
+ private
130
+
131
+ def agent
132
+ @agent ||= initialize_agent(app_root: "/rails/root")
133
+ end
134
+
135
+ def initialize_agent(options = {})
136
+ agent ||= RorVsWild.start(options)
137
+ agent.stubs(:post_request)
138
+ agent.stubs(:post_task)
139
+ agent
140
+ end
141
+ end