statsd-instrument 3.0.0.pre1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/lint.yml +22 -0
  3. data/.github/workflows/tests.yml +31 -0
  4. data/.rubocop.yml +3 -13
  5. data/CHANGELOG.md +50 -0
  6. data/Gemfile +8 -2
  7. data/README.md +6 -3
  8. data/Rakefile +7 -7
  9. data/benchmark/send-metrics-to-dev-null-log +12 -11
  10. data/benchmark/send-metrics-to-local-udp-receiver +16 -15
  11. data/bin/rake +29 -0
  12. data/bin/rubocop +29 -0
  13. data/lib/statsd-instrument.rb +1 -1
  14. data/lib/statsd/instrument.rb +112 -145
  15. data/lib/statsd/instrument/assertions.rb +200 -208
  16. data/lib/statsd/instrument/batched_udp_sink.rb +154 -0
  17. data/lib/statsd/instrument/capture_sink.rb +23 -19
  18. data/lib/statsd/instrument/client.rb +410 -306
  19. data/lib/statsd/instrument/datagram.rb +69 -65
  20. data/lib/statsd/instrument/datagram_builder.rb +81 -77
  21. data/lib/statsd/instrument/dogstatsd_datagram.rb +76 -72
  22. data/lib/statsd/instrument/dogstatsd_datagram_builder.rb +68 -64
  23. data/lib/statsd/instrument/environment.rb +88 -77
  24. data/lib/statsd/instrument/expectation.rb +96 -96
  25. data/lib/statsd/instrument/helpers.rb +11 -7
  26. data/lib/statsd/instrument/log_sink.rb +20 -16
  27. data/lib/statsd/instrument/matchers.rb +93 -74
  28. data/lib/statsd/instrument/null_sink.rb +12 -8
  29. data/lib/statsd/instrument/railtie.rb +11 -7
  30. data/lib/statsd/instrument/rubocop.rb +8 -8
  31. data/lib/statsd/instrument/rubocop/measure_as_dist_argument.rb +1 -1
  32. data/lib/statsd/instrument/rubocop/metaprogramming_positional_arguments.rb +2 -2
  33. data/lib/statsd/instrument/rubocop/metric_prefix_argument.rb +1 -1
  34. data/lib/statsd/instrument/rubocop/metric_return_value.rb +2 -2
  35. data/lib/statsd/instrument/rubocop/metric_value_keyword_argument.rb +1 -1
  36. data/lib/statsd/instrument/rubocop/positional_arguments.rb +4 -4
  37. data/lib/statsd/instrument/rubocop/singleton_configuration.rb +1 -1
  38. data/lib/statsd/instrument/rubocop/splat_arguments.rb +2 -2
  39. data/lib/statsd/instrument/statsd_datagram_builder.rb +12 -8
  40. data/lib/statsd/instrument/strict.rb +1 -6
  41. data/lib/statsd/instrument/udp_sink.rb +49 -47
  42. data/lib/statsd/instrument/version.rb +1 -1
  43. data/statsd-instrument.gemspec +4 -8
  44. data/test/assertions_test.rb +205 -161
  45. data/test/benchmark/clock_gettime.rb +1 -1
  46. data/test/benchmark/default_tags.rb +9 -9
  47. data/test/benchmark/metrics.rb +8 -8
  48. data/test/benchmark/tags.rb +4 -4
  49. data/test/capture_sink_test.rb +14 -14
  50. data/test/client_test.rb +96 -96
  51. data/test/datagram_builder_test.rb +55 -55
  52. data/test/datagram_test.rb +5 -5
  53. data/test/dogstatsd_datagram_builder_test.rb +37 -37
  54. data/test/environment_test.rb +30 -21
  55. data/test/helpers/rubocop_helper.rb +12 -9
  56. data/test/helpers_test.rb +15 -15
  57. data/test/integration_test.rb +7 -7
  58. data/test/log_sink_test.rb +4 -4
  59. data/test/matchers_test.rb +54 -54
  60. data/test/null_sink_test.rb +4 -4
  61. data/test/rubocop/measure_as_dist_argument_test.rb +2 -2
  62. data/test/rubocop/metaprogramming_positional_arguments_test.rb +2 -2
  63. data/test/rubocop/metric_prefix_argument_test.rb +2 -2
  64. data/test/rubocop/metric_return_value_test.rb +6 -6
  65. data/test/rubocop/metric_value_keyword_argument_test.rb +3 -3
  66. data/test/rubocop/positional_arguments_test.rb +12 -12
  67. data/test/rubocop/singleton_configuration_test.rb +8 -8
  68. data/test/rubocop/splat_arguments_test.rb +2 -2
  69. data/test/statsd_datagram_builder_test.rb +6 -6
  70. data/test/statsd_instrumentation_test.rb +122 -122
  71. data/test/statsd_test.rb +69 -67
  72. data/test/test_helper.rb +19 -10
  73. data/test/udp_sink_test.rb +122 -50
  74. metadata +12 -92
  75. data/.github/workflows/ci.yml +0 -56
  76. data/.rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml +0 -1027
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'test_helper'
3
+ require "test_helper"
4
4
 
5
5
  class DatagramBuilderTest < Minitest::Test
6
6
  def setup
@@ -8,111 +8,111 @@ class DatagramBuilderTest < Minitest::Test
8
8
  end
9
9
 
10
10
  def test_normalize_name
11
- assert_equal 'foo', @datagram_builder.send(:normalize_name, 'foo')
12
- assert_equal 'fo_o', @datagram_builder.send(:normalize_name, 'fo|o')
13
- assert_equal 'fo_o', @datagram_builder.send(:normalize_name, 'fo@o')
14
- assert_equal 'fo_o', @datagram_builder.send(:normalize_name, 'fo:o')
11
+ assert_equal("foo", @datagram_builder.send(:normalize_name, "foo"))
12
+ assert_equal("fo_o", @datagram_builder.send(:normalize_name, "fo|o"))
13
+ assert_equal("fo_o", @datagram_builder.send(:normalize_name, "fo@o"))
14
+ assert_equal("fo_o", @datagram_builder.send(:normalize_name, "fo:o"))
15
15
  end
16
16
 
17
17
  def test_normalize_unsupported_tag_names
18
- assert_equal ['ign#ored'], @datagram_builder.send(:normalize_tags, ['ign#o|re,d'])
18
+ assert_equal(["ign#ored"], @datagram_builder.send(:normalize_tags, ["ign#o|re,d"]))
19
19
  # Note: how this is interpreted by the backend is undefined.
20
20
  # We rely on the user to not do stuff like this if they don't want to be surprised.
21
21
  # We do not want to take the performance hit of normalizing this.
22
- assert_equal ['lol::class:omg::lol'], @datagram_builder.send(:normalize_tags, "lol::class" => "omg::lol")
22
+ assert_equal(["lol::class:omg::lol"], @datagram_builder.send(:normalize_tags, "lol::class" => "omg::lol"))
23
23
  end
24
24
 
25
25
  def test_normalize_tags_converts_hash_to_array
26
- assert_equal ['tag:value'], @datagram_builder.send(:normalize_tags, tag: 'value')
27
- assert_equal ['tag1:v1', 'tag2:v2'], @datagram_builder.send(:normalize_tags, tag1: 'v1', tag2: 'v2')
26
+ assert_equal(["tag:value"], @datagram_builder.send(:normalize_tags, tag: "value"))
27
+ assert_equal(["tag1:v1", "tag2:v2"], @datagram_builder.send(:normalize_tags, tag1: "v1", tag2: "v2"))
28
28
  end
29
29
 
30
30
  def test_c
31
- datagram = @datagram_builder.c('foo', 1, nil, nil)
32
- assert_equal "foo:1|c", datagram
31
+ datagram = @datagram_builder.c("foo", 1, nil, nil)
32
+ assert_equal("foo:1|c", datagram)
33
33
 
34
- datagram = @datagram_builder.c('fo:o', 10, 0.1, nil)
35
- assert_equal "fo_o:10|c|@0.1", datagram
34
+ datagram = @datagram_builder.c("fo:o", 10, 0.1, nil)
35
+ assert_equal("fo_o:10|c|@0.1", datagram)
36
36
  end
37
37
 
38
38
  def test_ms
39
- datagram = @datagram_builder.ms('foo', 1, nil, nil)
40
- assert_equal "foo:1|ms", datagram
39
+ datagram = @datagram_builder.ms("foo", 1, nil, nil)
40
+ assert_equal("foo:1|ms", datagram)
41
41
 
42
- datagram = @datagram_builder.ms('fo:o', 10, 0.1, nil)
43
- assert_equal "fo_o:10|ms|@0.1", datagram
42
+ datagram = @datagram_builder.ms("fo:o", 10, 0.1, nil)
43
+ assert_equal("fo_o:10|ms|@0.1", datagram)
44
44
  end
45
45
 
46
46
  def test_g
47
- datagram = @datagram_builder.g('foo', 1, nil, nil)
48
- assert_equal "foo:1|g", datagram
47
+ datagram = @datagram_builder.g("foo", 1, nil, nil)
48
+ assert_equal("foo:1|g", datagram)
49
49
 
50
- datagram = @datagram_builder.g('fo|o', 10, 0.01, nil)
51
- assert_equal "fo_o:10|g|@0.01", datagram
50
+ datagram = @datagram_builder.g("fo|o", 10, 0.01, nil)
51
+ assert_equal("fo_o:10|g|@0.01", datagram)
52
52
  end
53
53
 
54
54
  def test_s
55
- datagram = @datagram_builder.s('foo', 1, nil, nil)
56
- assert_equal "foo:1|s", datagram
55
+ datagram = @datagram_builder.s("foo", 1, nil, nil)
56
+ assert_equal("foo:1|s", datagram)
57
57
 
58
- datagram = @datagram_builder.s('fo@o', 10, 0.01, nil)
59
- assert_equal "fo_o:10|s|@0.01", datagram
58
+ datagram = @datagram_builder.s("fo@o", 10, 0.01, nil)
59
+ assert_equal("fo_o:10|s|@0.01", datagram)
60
60
  end
61
61
 
62
62
  def test_h
63
- datagram = @datagram_builder.h('foo', 1, nil, nil)
64
- assert_equal "foo:1|h", datagram
63
+ datagram = @datagram_builder.h("foo", 1, nil, nil)
64
+ assert_equal("foo:1|h", datagram)
65
65
 
66
- datagram = @datagram_builder.h('fo@o', 10, 0.01, nil)
67
- assert_equal "fo_o:10|h|@0.01", datagram
66
+ datagram = @datagram_builder.h("fo@o", 10, 0.01, nil)
67
+ assert_equal("fo_o:10|h|@0.01", datagram)
68
68
  end
69
69
 
70
70
  def test_d
71
- datagram = @datagram_builder.d('foo', 1, nil, nil)
72
- assert_equal "foo:1|d", datagram
71
+ datagram = @datagram_builder.d("foo", 1, nil, nil)
72
+ assert_equal("foo:1|d", datagram)
73
73
 
74
- datagram = @datagram_builder.d('fo@o', 10, 0.01, nil)
75
- assert_equal "fo_o:10|d|@0.01", datagram
74
+ datagram = @datagram_builder.d("fo@o", 10, 0.01, nil)
75
+ assert_equal("fo_o:10|d|@0.01", datagram)
76
76
  end
77
77
 
78
78
  def test_tags
79
- datagram = @datagram_builder.d('foo', 10, nil, ['foo', 'bar'])
80
- assert_equal "foo:10|d|#foo,bar", datagram
79
+ datagram = @datagram_builder.d("foo", 10, nil, ["foo", "bar"])
80
+ assert_equal("foo:10|d|#foo,bar", datagram)
81
81
 
82
- datagram = @datagram_builder.d('foo', 10, 0.1, ['foo:bar'])
83
- assert_equal "foo:10|d|@0.1|#foo:bar", datagram
82
+ datagram = @datagram_builder.d("foo", 10, 0.1, ["foo:bar"])
83
+ assert_equal("foo:10|d|@0.1|#foo:bar", datagram)
84
84
 
85
- datagram = @datagram_builder.d('foo', 10, 1, foo: 'bar', baz: 'quc')
86
- assert_equal "foo:10|d|#foo:bar,baz:quc", datagram
85
+ datagram = @datagram_builder.d("foo", 10, 1, foo: "bar", baz: "quc")
86
+ assert_equal("foo:10|d|#foo:bar,baz:quc", datagram)
87
87
  end
88
88
 
89
89
  def test_prefix
90
- datagram_builder = StatsD::Instrument::DatagramBuilder.new(prefix: 'foo')
91
- datagram = datagram_builder.c('bar', 1, nil, nil)
92
- assert_equal 'foo.bar:1|c', datagram
90
+ datagram_builder = StatsD::Instrument::DatagramBuilder.new(prefix: "foo")
91
+ datagram = datagram_builder.c("bar", 1, nil, nil)
92
+ assert_equal("foo.bar:1|c", datagram)
93
93
 
94
94
  # The prefix should also be normalized
95
- datagram_builder = StatsD::Instrument::DatagramBuilder.new(prefix: 'foo|bar')
96
- datagram = datagram_builder.c('baz', 1, nil, nil)
97
- assert_equal 'foo_bar.baz:1|c', datagram
95
+ datagram_builder = StatsD::Instrument::DatagramBuilder.new(prefix: "foo|bar")
96
+ datagram = datagram_builder.c("baz", 1, nil, nil)
97
+ assert_equal("foo_bar.baz:1|c", datagram)
98
98
  end
99
99
 
100
100
  def test_default_tags
101
- datagram_builder = StatsD::Instrument::DatagramBuilder.new(default_tags: ['foo'])
102
- datagram = datagram_builder.c('bar', 1, nil, nil)
103
- assert_equal 'bar:1|c|#foo', datagram
101
+ datagram_builder = StatsD::Instrument::DatagramBuilder.new(default_tags: ["foo"])
102
+ datagram = datagram_builder.c("bar", 1, nil, nil)
103
+ assert_equal("bar:1|c|#foo", datagram)
104
104
 
105
- datagram = datagram_builder.c('bar', 1, nil, a: 'b')
106
- assert_equal 'bar:1|c|#a:b,foo', datagram
105
+ datagram = datagram_builder.c("bar", 1, nil, a: "b")
106
+ assert_equal("bar:1|c|#a:b,foo", datagram)
107
107
 
108
108
  # We do not filter out duplicates, because detecting dupes is too time consuming.
109
109
  # We let the server deal with the situation
110
- datagram = datagram_builder.c('bar', 1, nil, ['foo'])
111
- assert_equal 'bar:1|c|#foo,foo', datagram
110
+ datagram = datagram_builder.c("bar", 1, nil, ["foo"])
111
+ assert_equal("bar:1|c|#foo,foo", datagram)
112
112
 
113
113
  # Default tags are also normalized
114
- datagram_builder = StatsD::Instrument::DatagramBuilder.new(default_tags: ['f,o|o'])
115
- datagram = datagram_builder.c('bar', 1, nil, nil)
116
- assert_equal 'bar:1|c|#foo', datagram
114
+ datagram_builder = StatsD::Instrument::DatagramBuilder.new(default_tags: ["f,o|o"])
115
+ datagram = datagram_builder.c("bar", 1, nil, nil)
116
+ assert_equal("bar:1|c|#foo", datagram)
117
117
  end
118
118
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'test_helper'
3
+ require "test_helper"
4
4
 
5
5
  class DatagramTest < Minitest::Test
6
6
  def test_parsing_datagrams
7
- datagram = 'Kernel.Orders.order_creation_path:1|c|' \
8
- '#order_source:web,code_source:NilController#NilAction,order_builder:false,' \
9
- 'multi_currency:false,fulfillment_orders_beta_enabled:false'
7
+ datagram = "Kernel.Orders.order_creation_path:1|c|" \
8
+ "#order_source:web,code_source:NilController#NilAction,order_builder:false," \
9
+ "multi_currency:false,fulfillment_orders_beta_enabled:false"
10
10
 
11
11
  parsed = StatsD::Instrument::Datagram.new(datagram)
12
- assert_includes parsed.tags, 'code_source:NilController#NilAction'
12
+ assert_includes(parsed.tags, "code_source:NilController#NilAction")
13
13
  end
14
14
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'test_helper'
3
+ require "test_helper"
4
4
 
5
5
  class DogStatsDDatagramBuilderTest < Minitest::Test
6
6
  def setup
@@ -8,60 +8,60 @@ class DogStatsDDatagramBuilderTest < Minitest::Test
8
8
  end
9
9
 
10
10
  def test_raises_on_unsupported_metrics
11
- assert_raises(NotImplementedError) { @datagram_builder.kv('foo', 10, nil, nil) }
11
+ assert_raises(NotImplementedError) { @datagram_builder.kv("foo", 10, nil, nil) }
12
12
  end
13
13
 
14
14
  def test_simple_service_check
15
- datagram = @datagram_builder._sc('service', :ok)
16
- assert_equal '_sc|service|0', datagram
15
+ datagram = @datagram_builder._sc("service", :ok)
16
+ assert_equal("_sc|service|0", datagram)
17
17
  parsed_datagram = StatsD::Instrument::DogStatsDDatagramBuilder.datagram_class.new(datagram)
18
- assert_equal :_sc, parsed_datagram.type
19
- assert_equal 'service', parsed_datagram.name
20
- assert_equal 0, parsed_datagram.value
18
+ assert_equal(:_sc, parsed_datagram.type)
19
+ assert_equal("service", parsed_datagram.name)
20
+ assert_equal(0, parsed_datagram.value)
21
21
  end
22
22
 
23
23
  def test_complex_service_check
24
- datagram = @datagram_builder._sc('service', :warning, timestamp: Time.parse('2019-09-30T04:22:12Z'),
25
- hostname: 'localhost', tags: { foo: 'bar|baz' }, message: 'blah')
26
- assert_equal "_sc|service|1|h:localhost|d:1569817332|#foo:barbaz|m:blah", datagram
24
+ datagram = @datagram_builder._sc("service", :warning, timestamp: Time.parse("2019-09-30T04:22:12Z"),
25
+ hostname: "localhost", tags: { foo: "bar|baz" }, message: "blah")
26
+ assert_equal("_sc|service|1|h:localhost|d:1569817332|#foo:barbaz|m:blah", datagram)
27
27
 
28
28
  parsed_datagram = StatsD::Instrument::DogStatsDDatagramBuilder.datagram_class.new(datagram)
29
- assert_equal :_sc, parsed_datagram.type
30
- assert_equal 'service', parsed_datagram.name
31
- assert_equal 1, parsed_datagram.value
32
- assert_equal 'localhost', parsed_datagram.hostname
33
- assert_equal Time.parse('2019-09-30T04:22:12Z'), parsed_datagram.timestamp
34
- assert_equal ["foo:barbaz"], parsed_datagram.tags
35
- assert_equal 'blah', parsed_datagram.message
29
+ assert_equal(:_sc, parsed_datagram.type)
30
+ assert_equal("service", parsed_datagram.name)
31
+ assert_equal(1, parsed_datagram.value)
32
+ assert_equal("localhost", parsed_datagram.hostname)
33
+ assert_equal(Time.parse("2019-09-30T04:22:12Z"), parsed_datagram.timestamp)
34
+ assert_equal(["foo:barbaz"], parsed_datagram.tags)
35
+ assert_equal("blah", parsed_datagram.message)
36
36
  end
37
37
 
38
38
  def test_simple_event
39
- datagram = @datagram_builder._e('hello', "world")
40
- assert_equal '_e{5,5}:hello|world', datagram
39
+ datagram = @datagram_builder._e("hello", "world")
40
+ assert_equal("_e{5,5}:hello|world", datagram)
41
41
 
42
42
  parsed_datagram = StatsD::Instrument::DogStatsDDatagramBuilder.datagram_class.new(datagram)
43
- assert_equal :_e, parsed_datagram.type
44
- assert_equal 'hello', parsed_datagram.name
45
- assert_equal 'world', parsed_datagram.value
43
+ assert_equal(:_e, parsed_datagram.type)
44
+ assert_equal("hello", parsed_datagram.name)
45
+ assert_equal("world", parsed_datagram.value)
46
46
  end
47
47
 
48
48
  def test_complex_event
49
- datagram = @datagram_builder._e("testing", "with\nnewline", timestamp: Time.parse('2019-09-30T04:22:12Z'),
50
- hostname: 'localhost', aggregation_key: 'my-key', priority: 'low', source_type_name: 'source',
51
- alert_type: 'success', tags: { foo: 'bar|baz' })
52
- assert_equal '_e{7,13}:testing|with\\nnewline|h:localhost|d:1569817332|k:my-key|' \
53
- 'p:low|s:source|t:success|#foo:barbaz', datagram
49
+ datagram = @datagram_builder._e("testing", "with\nnewline", timestamp: Time.parse("2019-09-30T04:22:12Z"),
50
+ hostname: "localhost", aggregation_key: "my-key", priority: "low", source_type_name: "source",
51
+ alert_type: "success", tags: { foo: "bar|baz" })
52
+ assert_equal('_e{7,13}:testing|with\\nnewline|h:localhost|d:1569817332|k:my-key|' \
53
+ "p:low|s:source|t:success|#foo:barbaz", datagram)
54
54
 
55
55
  parsed_datagram = StatsD::Instrument::DogStatsDDatagramBuilder.datagram_class.new(datagram)
56
- assert_equal :_e, parsed_datagram.type
57
- assert_equal 'testing', parsed_datagram.name
58
- assert_equal "with\nnewline", parsed_datagram.value
59
- assert_equal 'localhost', parsed_datagram.hostname
60
- assert_equal Time.parse('2019-09-30T04:22:12Z'), parsed_datagram.timestamp
61
- assert_equal ["foo:barbaz"], parsed_datagram.tags
62
- assert_equal "my-key", parsed_datagram.aggregation_key
63
- assert_equal "low", parsed_datagram.priority
64
- assert_equal "source", parsed_datagram.source_type_name
65
- assert_equal "success", parsed_datagram.alert_type
56
+ assert_equal(:_e, parsed_datagram.type)
57
+ assert_equal("testing", parsed_datagram.name)
58
+ assert_equal("with\nnewline", parsed_datagram.value)
59
+ assert_equal("localhost", parsed_datagram.hostname)
60
+ assert_equal(Time.parse("2019-09-30T04:22:12Z"), parsed_datagram.timestamp)
61
+ assert_equal(["foo:barbaz"], parsed_datagram.tags)
62
+ assert_equal("my-key", parsed_datagram.aggregation_key)
63
+ assert_equal("low", parsed_datagram.priority)
64
+ assert_equal("source", parsed_datagram.source_type_name)
65
+ assert_equal("success", parsed_datagram.alert_type)
66
66
  end
67
67
  end
@@ -1,58 +1,67 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'test_helper'
3
+ require "test_helper"
4
4
 
5
5
  module Rails; end
6
6
 
7
7
  class EnvironmentTest < Minitest::Test
8
8
  def test_environment_prefers_statsd_env_if_available
9
9
  env = StatsD::Instrument::Environment.new(
10
- 'STATSD_ENV' => 'set_from_STATSD_ENV',
11
- 'RACK_ENV' => 'set_from_RACK_ENV',
12
- 'ENV' => 'set_from_ENV',
10
+ "STATSD_ENV" => "set_from_STATSD_ENV",
11
+ "RACK_ENV" => "set_from_RACK_ENV",
12
+ "ENV" => "set_from_ENV",
13
13
  )
14
- assert_equal 'set_from_STATSD_ENV', env.environment
14
+ assert_equal("set_from_STATSD_ENV", env.environment)
15
15
  end
16
16
 
17
17
  def test_environment_uses_env_when_rails_does_not_respond_to_env_and_statsd_env_is_not_set
18
18
  env = StatsD::Instrument::Environment.new(
19
- 'ENV' => 'set_from_ENV',
19
+ "ENV" => "set_from_ENV",
20
20
  )
21
- assert_equal 'set_from_ENV', env.environment
21
+ assert_equal("set_from_ENV", env.environment)
22
22
  end
23
23
 
24
24
  def test_environment_uses_rails_env_when_rails_is_available
25
- Rails.stubs(:env).returns('production')
26
- assert_equal 'production', StatsD::Instrument::Environment.environment
25
+ Rails.stubs(:env).returns("production")
26
+ assert_equal("production", StatsD::Instrument::Environment.environment)
27
27
  end
28
28
 
29
29
  def test_environment_defaults_to_development
30
30
  env = StatsD::Instrument::Environment.new({})
31
- assert_equal 'development', env.environment
31
+ assert_equal("development", env.environment)
32
32
  end
33
33
 
34
34
  def test_client_returns_client_instance
35
35
  env = StatsD::Instrument::Environment.new({})
36
- assert_kind_of StatsD::Instrument::Client, env.client
36
+ assert_kind_of(StatsD::Instrument::Client, env.client)
37
37
  end
38
38
 
39
39
  def test_client_from_env_uses_log_sink_in_development_environment
40
- env = StatsD::Instrument::Environment.new('STATSD_USE_NEW_CLIENT' => '1', 'STATSD_ENV' => 'development')
41
- assert_kind_of StatsD::Instrument::LogSink, env.client.sink
40
+ env = StatsD::Instrument::Environment.new("STATSD_USE_NEW_CLIENT" => "1", "STATSD_ENV" => "development")
41
+ assert_kind_of(StatsD::Instrument::LogSink, env.client.sink)
42
42
  end
43
43
 
44
44
  def test_client_from_env_uses_null_sink_in_test_environment
45
- env = StatsD::Instrument::Environment.new('STATSD_USE_NEW_CLIENT' => '1', 'STATSD_ENV' => 'test')
46
- assert_kind_of StatsD::Instrument::NullSink, env.client.sink
45
+ env = StatsD::Instrument::Environment.new("STATSD_USE_NEW_CLIENT" => "1", "STATSD_ENV" => "test")
46
+ assert_kind_of(StatsD::Instrument::NullSink, env.client.sink)
47
47
  end
48
48
 
49
- def test_client_from_env_uses_udp_sink_in_staging_environment
50
- env = StatsD::Instrument::Environment.new('STATSD_USE_NEW_CLIENT' => '1', 'STATSD_ENV' => 'staging')
51
- assert_kind_of StatsD::Instrument::UDPSink, env.client.sink
49
+ def test_client_from_env_uses_batched_udp_sink_in_staging_environment
50
+ env = StatsD::Instrument::Environment.new("STATSD_USE_NEW_CLIENT" => "1", "STATSD_ENV" => "staging")
51
+ assert_kind_of(StatsD::Instrument::BatchedUDPSink, env.client.sink)
52
52
  end
53
53
 
54
- def test_client_from_env_uses_udp_sink_in_production_environment
55
- env = StatsD::Instrument::Environment.new('STATSD_USE_NEW_CLIENT' => '1', 'STATSD_ENV' => 'production')
56
- assert_kind_of StatsD::Instrument::UDPSink, env.client.sink
54
+ def test_client_from_env_uses_batched_udp_sink_in_production_environment
55
+ env = StatsD::Instrument::Environment.new("STATSD_USE_NEW_CLIENT" => "1", "STATSD_ENV" => "production")
56
+ assert_kind_of(StatsD::Instrument::BatchedUDPSink, env.client.sink)
57
+ end
58
+
59
+ def test_client_from_env_uses_regular_udp_sink_when_flush_interval_is_0
60
+ env = StatsD::Instrument::Environment.new(
61
+ "STATSD_USE_NEW_CLIENT" => "1",
62
+ "STATSD_ENV" => "staging",
63
+ "STATSD_FLUSH_INTERVAL" => "0.0",
64
+ )
65
+ assert_kind_of(StatsD::Instrument::UDPSink, env.client.sink)
57
66
  end
58
67
  end
@@ -1,25 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rubocop'
3
+ require "rubocop"
4
+ require "rubocop/cop/legacy/corrector"
4
5
 
5
6
  module RubocopHelper
6
7
  attr_accessor :cop
7
8
 
8
9
  private
9
10
 
11
+ RUBY_VERSION = 2.5
12
+ private_constant :RUBY_VERSION
13
+
10
14
  def assert_no_offenses(source)
11
- investigate(RuboCop::ProcessedSource.new(source, 2.3, nil))
12
- assert_predicate cop.offenses, :empty?, "Did not expect Rubocop to find offenses"
15
+ investigate(RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil))
16
+ assert_predicate(cop.offenses, :empty?, "Did not expect Rubocop to find offenses")
13
17
  end
14
18
 
15
19
  def assert_offense(source)
16
- investigate(RuboCop::ProcessedSource.new(source, 2.3, nil))
17
- refute_predicate cop.offenses, :empty?, "Expected Rubocop to find offenses"
20
+ investigate(RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil))
21
+ refute_predicate(cop.offenses, :empty?, "Expected Rubocop to find offenses")
18
22
  end
19
23
 
20
24
  def assert_no_autocorrect(source)
21
25
  corrected = autocorrect_source(source)
22
- assert_equal source, corrected
26
+ assert_equal(source, corrected)
23
27
  end
24
28
 
25
29
  def autocorrect_source(source)
@@ -27,16 +31,15 @@ module RubocopHelper
27
31
  RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
28
32
  cop.instance_variable_get(:@options)[:auto_correct] = true
29
33
 
30
- processed_source = RuboCop::ProcessedSource.new(source, 2.3, nil)
34
+ processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil)
31
35
  investigate(processed_source)
32
36
 
33
- corrector = RuboCop::Cop::Corrector.new(processed_source.buffer, cop.corrections)
37
+ corrector = RuboCop::Cop::Legacy::Corrector.new(processed_source.buffer, cop.corrections)
34
38
  corrector.rewrite
35
39
  end
36
40
 
37
41
  def investigate(processed_source)
38
42
  forces = RuboCop::Cop::Force.all.each_with_object([]) do |klass, instances|
39
- next unless cop.join_force?(klass)
40
43
  instances << klass.new([cop])
41
44
  end
42
45
 
data/test/helpers_test.rb CHANGED
@@ -1,40 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'test_helper'
3
+ require "test_helper"
4
4
 
5
5
  class HelpersTest < Minitest::Test
6
6
  def setup
7
7
  test_class = Class.new(Minitest::Test)
8
8
  test_class.send(:include, StatsD::Instrument::Helpers)
9
- @test_case = test_class.new('fake')
9
+ @test_case = test_class.new("fake")
10
10
  end
11
11
 
12
12
  def test_capture_metrics_inside_block_only
13
- StatsD.increment('counter')
13
+ StatsD.increment("counter")
14
14
  metrics = @test_case.capture_statsd_calls do
15
- StatsD.increment('counter')
16
- StatsD.gauge('gauge', 12)
15
+ StatsD.increment("counter")
16
+ StatsD.gauge("gauge", 12)
17
17
  end
18
- StatsD.gauge('gauge', 15)
18
+ StatsD.gauge("gauge", 15)
19
19
 
20
- assert_equal 2, metrics.length
21
- assert_equal 'counter', metrics[0].name
22
- assert_equal 'gauge', metrics[1].name
23
- assert_equal 12, metrics[1].value
20
+ assert_equal(2, metrics.length)
21
+ assert_equal("counter", metrics[0].name)
22
+ assert_equal("gauge", metrics[1].name)
23
+ assert_equal(12, metrics[1].value)
24
24
  end
25
25
 
26
26
  def test_capture_metrics_with_new_client
27
27
  @old_client = StatsD.singleton_client
28
28
  StatsD.singleton_client = StatsD::Instrument::Client.new
29
29
 
30
- StatsD.increment('counter')
30
+ StatsD.increment("counter")
31
31
  metrics = @test_case.capture_statsd_datagrams do
32
- StatsD.increment('counter')
33
- StatsD.gauge('gauge', 12)
32
+ StatsD.increment("counter")
33
+ StatsD.gauge("gauge", 12)
34
34
  end
35
- StatsD.gauge('gauge', 15)
35
+ StatsD.gauge("gauge", 15)
36
36
 
37
- assert_equal 2, metrics.length
37
+ assert_equal(2, metrics.length)
38
38
 
39
39
  ensure
40
40
  StatsD.singleton_client = @old_client