chillout 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -5
  3. data/CHANGELOG.md +7 -0
  4. data/lib/chillout/client.rb +12 -9
  5. data/lib/chillout/config.rb +21 -3
  6. data/lib/chillout/creations_container.rb +26 -0
  7. data/lib/chillout/job.rb +3 -3
  8. data/lib/chillout/railtie.rb +21 -4
  9. data/lib/chillout/server_side/dispatcher.rb +2 -2
  10. data/lib/chillout/server_side/server_side.rb +12 -11
  11. data/lib/chillout/subscribers/action_controller_notifications.rb +49 -0
  12. data/lib/chillout/version.rb +1 -1
  13. data/lib/chillout/worker.rb +13 -10
  14. data/lib/chillout.rb +1 -0
  15. data/test/acceptance/client_sends_controller_performance_test.rb +33 -0
  16. data/test/acceptance/client_sends_metrics_test.rb +5 -3
  17. data/test/client_test.rb +14 -1
  18. data/test/config_test.rb +14 -0
  19. data/test/creations_container_test.rb +48 -1
  20. data/test/dispatcher_test.rb +4 -4
  21. data/test/integration/creations_monitor_rack_test.rb +3 -3
  22. data/test/server_side_test.rb +0 -12
  23. data/test/subscribers/action_controller_notifications_test.rb +48 -0
  24. data/test/support/rails_4_0_0/config/environments/production.rb +7 -1
  25. data/test/support/rails_4_0_13/config/environments/production.rb +7 -1
  26. data/test/support/rails_4_1_0/config/environments/production.rb +7 -1
  27. data/test/support/rails_4_1_16/config/environments/production.rb +7 -1
  28. data/test/support/rails_4_2_0/config/environments/production.rb +1 -0
  29. data/test/test_helper.rb +18 -2
  30. data/test/worker_test.rb +43 -18
  31. metadata +7 -77
  32. data/lib/chillout/event_data_builder.rb +0 -34
  33. data/test/event_data_builder_test.rb +0 -90
  34. data/test/support/rails_3_2_13/Gemfile +0 -5
  35. data/test/support/rails_3_2_13/Rakefile +0 -7
  36. data/test/support/rails_3_2_13/app/controllers/application_controller.rb +0 -3
  37. data/test/support/rails_3_2_13/app/controllers/entities_controller.rb +0 -70
  38. data/test/support/rails_3_2_13/app/helpers/application_helper.rb +0 -2
  39. data/test/support/rails_3_2_13/app/models/entity.rb +0 -3
  40. data/test/support/rails_3_2_13/app/views/entities/_form.html.erb +0 -21
  41. data/test/support/rails_3_2_13/app/views/entities/edit.html.erb +0 -6
  42. data/test/support/rails_3_2_13/app/views/entities/index.html.erb +0 -23
  43. data/test/support/rails_3_2_13/app/views/entities/new.html.erb +0 -5
  44. data/test/support/rails_3_2_13/app/views/entities/show.html.erb +0 -10
  45. data/test/support/rails_3_2_13/app/views/layouts/application.html.erb +0 -14
  46. data/test/support/rails_3_2_13/config/application.rb +0 -63
  47. data/test/support/rails_3_2_13/config/boot.rb +0 -6
  48. data/test/support/rails_3_2_13/config/database.yml +0 -11
  49. data/test/support/rails_3_2_13/config/environment.rb +0 -5
  50. data/test/support/rails_3_2_13/config/environments/development.rb +0 -32
  51. data/test/support/rails_3_2_13/config/environments/production.rb +0 -56
  52. data/test/support/rails_3_2_13/config/environments/test.rb +0 -37
  53. data/test/support/rails_3_2_13/config/initializers/backtrace_silencers.rb +0 -7
  54. data/test/support/rails_3_2_13/config/initializers/inflections.rb +0 -15
  55. data/test/support/rails_3_2_13/config/initializers/load_schema.rb +0 -4
  56. data/test/support/rails_3_2_13/config/initializers/mime_types.rb +0 -5
  57. data/test/support/rails_3_2_13/config/initializers/secret_token.rb +0 -7
  58. data/test/support/rails_3_2_13/config/initializers/session_store.rb +0 -8
  59. data/test/support/rails_3_2_13/config/initializers/wrap_parameters.rb +0 -14
  60. data/test/support/rails_3_2_13/config/locales/en.yml +0 -5
  61. data/test/support/rails_3_2_13/config/routes.rb +0 -3
  62. data/test/support/rails_3_2_13/config.ru +0 -4
  63. data/test/support/rails_3_2_13/db/schema.rb +0 -22
  64. data/test/support/rails_3_2_13/public/404.html +0 -26
  65. data/test/support/rails_3_2_13/public/422.html +0 -26
  66. data/test/support/rails_3_2_13/public/500.html +0 -25
  67. data/test/support/rails_3_2_13/public/favicon.ico +0 -0
  68. data/test/support/rails_3_2_13/public/robots.txt +0 -5
  69. data/test/support/rails_3_2_13/script/rails +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed69970f8a13178646f300c5a13213e7642fe3df
4
- data.tar.gz: d51d4ea537f831f0c499738bce7b2ceb2cc293f2
3
+ metadata.gz: 8423a0fd777360d79146e8270eeb5536f6132e4f
4
+ data.tar.gz: 1a2b4337e6ab9d2b62a13a111788f33b192d23a1
5
5
  SHA512:
6
- metadata.gz: 848763fd9423900ff683bc4a88ec906dda4fc01233ba64c420f5c45a4aa93690e5ed40704801d0d4b549b9d381f67672fd18025c03994dc822cd86161a09ac4a
7
- data.tar.gz: 9a12a1bcdb467d51aa95c939b18b129a44e783ca6a3779c75d46d837a42a8b497253911b83bf8df43b482d5198a41f2f451daf1bc5286f853852129eb1f198f3
6
+ metadata.gz: 952cbad057b400710e7af9ed61e3f012476f673a3f5d6b4733b09f30d50a950517d42d1c24154a4c8cce1746a19cc63f623e23bdd2b7a54a5a5a594ce12a4d4f
7
+ data.tar.gz: edde4d6c71087cc05108310b764aedc3bed7e10b390f87f49506b7a9698955b2c0ebad3c642d2d120de7f596dc46b2439a90a5344bf780b4c11b76937d3daab1
data/.travis.yml CHANGED
@@ -26,18 +26,18 @@ matrix:
26
26
  - rvm: 2.4.1
27
27
  env: SAMPLE_APP=rails_4_1_16
28
28
  - rvm: 2.4.1
29
- env: SAMPLE_APP=rails_4_2_0
29
+ env: SAMPLE_APP=rails_4_2_0 SIDEKIQ_SUPPORTED=true
30
30
  - rvm: 2.1.10
31
- env: SAMPLE_APP=rails_4_2_8
31
+ env: SAMPLE_APP=rails_4_2_8 SIDEKIQ_SUPPORTED=true
32
32
  - rvm: 2.1.10
33
- env: SAMPLE_APP=rails_5_0_3
33
+ env: SAMPLE_APP=rails_5_0_3 SIDEKIQ_SUPPORTED=true
34
34
  - rvm: 2.1.10
35
35
  env: SAMPLE_APP=rails_5_1_1 SIDEKIQ_SUPPORTED=true
36
36
  include:
37
37
  - rvm: 2.4.1
38
- env: SAMPLE_APP=rails_4_2_8 STRATEGY=active_job
38
+ env: SAMPLE_APP=rails_4_2_8 STRATEGY=active_job SIDEKIQ_SUPPORTED=true
39
39
  - rvm: 2.4.1
40
- env: SAMPLE_APP=rails_5_0_3 STRATEGY=active_job
40
+ env: SAMPLE_APP=rails_5_0_3 STRATEGY=active_job SIDEKIQ_SUPPORTED=true
41
41
  - rvm: 2.4.1
42
42
  env: SAMPLE_APP=rails_5_1_1 STRATEGY=active_job SIDEKIQ_SUPPORTED=true
43
43
  notifications:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # chillout gem changes
2
2
 
3
+ 0.8.3
4
+ -----
5
+
6
+ - Introduced ActionController performance tracking.
7
+ - Added ability to disabled automatic ActiveRecord creations tracking by passing `creations_tracking: false` via config.
8
+ - Added ability to disabled automatic ActionController performance tracking by passing `requests_tracking: false` via config.
9
+
3
10
  0.8.2
4
11
  -----
5
12
 
@@ -3,7 +3,6 @@ require 'chillout/server_side/server_side'
3
3
  require 'chillout/server_side/http_client'
4
4
  require 'chillout/server_side/dispatcher'
5
5
  require 'chillout/config'
6
- require 'chillout/event_data_builder'
7
6
  require 'chillout/prefixed_logger'
8
7
  require 'chillout/worker'
9
8
  require 'chillout/check_result'
@@ -27,14 +26,14 @@ module Chillout
27
26
  @logger = PrefixedLogger.new("Chillout", @config.logger)
28
27
 
29
28
  @http_client = HttpClient.new(@config, logger).freeze
30
- @event_data_builder = EventDataBuilder.new(@config).freeze
31
- @server_side = ServerSide.new(@event_data_builder, @http_client).freeze
29
+ @server_side = ServerSide.new(@config, @http_client).freeze
32
30
  @dispatcher = Dispatcher.new(@server_side).freeze
33
31
  case @config.strategy
34
32
  when :thread
35
33
  @queue = Queue.new
36
34
  @worker_mutex = Mutex.new
37
35
  @worker_thread = nil
36
+ @max_queue = config.max_queue
38
37
  when :active_job
39
38
  require 'chillout/job'
40
39
  Chillout::Job.dispatcher = @dispatcher
@@ -49,15 +48,19 @@ module Chillout
49
48
  end
50
49
  end
51
50
 
52
- def enqueue(creations)
51
+ def enqueue(metrics)
53
52
  case @config.strategy
54
53
  when :thread
55
54
  start_worker
56
- @logger.debug "Creations were enqueued."
57
- @queue << creations
55
+ if @queue.size < @max_queue
56
+ @queue << metrics
57
+ @logger.debug "Metrics were enqueued."
58
+ else
59
+ @logger.error "Metrics buffer overflow. Skipping enqueue."
60
+ end
58
61
  when :active_job
59
- @logger.debug "Creations were enqueued."
60
- Chillout::Job.perform_later(YAML.dump(creations))
62
+ Chillout::Job.perform_later(YAML.dump(metrics))
63
+ @logger.debug "Metrics were enqueued."
61
64
  end
62
65
  end
63
66
 
@@ -70,7 +73,7 @@ module Chillout
70
73
  @worker_mutex.synchronize do
71
74
  return if worker_running?
72
75
  @worker_thread = Thread.new do
73
- worker = Worker.new(@dispatcher, @queue, @logger)
76
+ worker = Worker.new(@max_queue, @dispatcher, @queue, @logger)
74
77
  worker.run
75
78
  end
76
79
  end
@@ -17,15 +17,18 @@ module Chillout
17
17
  attr_accessor :platform
18
18
  attr_accessor :hostname
19
19
  attr_accessor :port
20
- attr_accessor :api_key
20
+ attr_reader :api_key
21
21
  attr_writer :authentication_user
22
22
  attr_writer :authentication_password
23
23
  attr_accessor :logger
24
24
  attr_accessor :ssl
25
25
  attr_reader :strategy
26
+ attr_accessor :max_queue
27
+ attr_accessor :creations_tracking
28
+ attr_accessor :requests_tracking
26
29
 
27
- def initialize(api_key = nil)
28
- @api_key = api_key
30
+ def initialize(api_key = '')
31
+ self.api_key = api_key
29
32
  @hostname = DEFAULT_HOSTNAME
30
33
  @port = DEFAULT_PORT
31
34
  @notifier_name = DEFAULT_NOTIFIER_NAME
@@ -34,6 +37,9 @@ module Chillout
34
37
  @logger = Logger.new(STDOUT)
35
38
  @ssl = true
36
39
  @strategy = :thread
40
+ @max_queue = 5_000
41
+ @creations_tracking = true
42
+ @requests_tracking = true
37
43
 
38
44
  @authentication_user = nil
39
45
  @authentication_password = nil
@@ -45,6 +51,18 @@ module Chillout
45
51
  end
46
52
  end
47
53
 
54
+ def api_key=(value)
55
+ if String === value
56
+ @api_key = value
57
+ else
58
+ raise ArgumentError.new("Invalid config passed")
59
+ end
60
+ end
61
+
62
+ def secret=(value)
63
+ self.api_key = value
64
+ end
65
+
48
66
  def strategy=(name)
49
67
  name = name.to_sym
50
68
  precondition = {
@@ -18,6 +18,10 @@ module Chillout
18
18
  name.to_sym
19
19
  end
20
20
 
21
+ def each(&proc)
22
+ @container.each(&proc)
23
+ end
24
+
21
25
  def resource_keys
22
26
  @container.keys
23
27
  end
@@ -27,5 +31,27 @@ module Chillout
27
31
  increment!(key, other_container[key])
28
32
  end
29
33
  end
34
+
35
+ def ==(other)
36
+ self.class == other.class &&
37
+ @container == other.instance_variable_get(:@container)
38
+ end
39
+
40
+ def empty?
41
+ @container.empty?
42
+ end
43
+
44
+ def as_measurements(timestamp = Time.now)
45
+ iso_timestamp = timestamp.iso8601
46
+ @container.each.map do |model_name, value|
47
+ {
48
+ series: model_name.to_s,
49
+ tags: {},
50
+ timestamp: iso_timestamp,
51
+ values: { creations: value },
52
+ }
53
+ end
54
+ end
55
+
30
56
  end
31
57
  end
data/lib/chillout/job.rb CHANGED
@@ -5,8 +5,8 @@ class Chillout::Job < ActiveJob::Base
5
5
  attr_accessor :dispatcher
6
6
  end
7
7
 
8
- def perform(serialized_metrics)
9
- metrics = YAML.load(serialized_metrics)
10
- self.class.dispatcher.send_creations(metrics)
8
+ def perform(serialized_metric)
9
+ measurement = YAML.load(serialized_metric)
10
+ self.class.dispatcher.send_measurements([measurement])
11
11
  end
12
12
  end
@@ -34,17 +34,34 @@ module Chillout
34
34
  listeners_injector.logger = @rails_logger
35
35
 
36
36
  @rails_logger.info "[Chillout] Railtie initializing"
37
- client = Client.new(@chillout_config[:secret], options)
38
- listeners_injector.inject!
37
+ config = build_config(options)
38
+ client = Client.new(config)
39
+ if config.creations_tracking
40
+ listeners_injector.inject!
41
+ @rails_logger.info "[Chillout] Creation monitor enabled"
42
+ end
39
43
  @rails_app.middleware.use Middleware::CreationsMonitor, client
40
- @rails_logger.info "[Chillout] Creation monitor enabled"
44
+
41
45
  sidekiq = Integrations::Sidekiq.new
42
46
  sidekiq.enable(client) if sidekiq.available?
47
+
48
+ if config.requests_tracking
49
+ @rails_logger.info "[Chillout] Requests monitor enabled"
50
+ Subscribers::ActionControllerNotifications.new.enable(client)
51
+ end
43
52
  client.start
44
53
  end
45
54
 
55
+ private
56
+
46
57
  def options
47
- {logger: @rails_logger}.merge(@chillout_config).except(:secret)
58
+ {logger: @rails_logger}.merge(@chillout_config)
59
+ end
60
+
61
+ def build_config(options)
62
+ config = Config.new
63
+ config.update(options)
64
+ config
48
65
  end
49
66
 
50
67
  end
@@ -7,8 +7,8 @@ module Chillout
7
7
  @server_side = server_side
8
8
  end
9
9
 
10
- def send_creations(creations)
11
- @server_side.send_creations(creations)
10
+ def send_measurements(measurements)
11
+ @server_side.send_measurements(measurements)
12
12
  rescue HttpClient::NotSent
13
13
  raise SendCreationsFailed.new
14
14
  end
@@ -1,17 +1,22 @@
1
1
  require 'time'
2
- require 'chillout/event_data_builder'
3
2
 
4
3
  module Chillout
5
4
  class ServerSide
6
5
 
7
- def initialize(event_data_builder, http_client)
6
+ def initialize(config, http_client)
8
7
  @http_client = http_client
9
- @event_data_builder = event_data_builder
8
+ @config = config
10
9
  end
11
10
 
12
- def send_creations(creations_container)
13
- event_data = @event_data_builder.build_from_creations_container(creations_container, timestamp)
14
- send_metric(event_data)
11
+ def send_measurements(measurements)
12
+ send_metric(
13
+ :measurements => measurements.map(&:as_measurements).flatten,
14
+ :notifier => {
15
+ name: @config.notifier_name,
16
+ version: @config.version,
17
+ url: @config.notifier_url,
18
+ }
19
+ )
15
20
  end
16
21
 
17
22
  def send_metric(data)
@@ -28,9 +33,5 @@ module Chillout
28
33
  })
29
34
  end
30
35
 
31
- private
32
- def timestamp
33
- Time.now.iso8601
34
- end
35
36
  end
36
- end
37
+ end
@@ -0,0 +1,49 @@
1
+ module Chillout
2
+ module Subscribers
3
+ class ActionControllerNotifications
4
+
5
+ class RequestMetric
6
+ def initialize(event)
7
+ @end = event.end
8
+ @duration = event.duration
9
+ @payload = event.payload.except(:headers)
10
+ end
11
+
12
+ def as_measurements()
13
+ format = (payload[:format] || "all").to_s
14
+ format = "all" if format == "*/*"
15
+
16
+ [{
17
+ series: "request",
18
+ tags: {
19
+ controller: payload[:controller],
20
+ action: payload[:action],
21
+ format: format,
22
+ method: payload[:method],
23
+ status: payload[:status],
24
+ },
25
+ timestamp: self.end.iso8601,
26
+ values: {
27
+ finished: 1,
28
+ duration: duration,
29
+ db: payload[:db_runtime] || 0,
30
+ view: payload[:view_runtime] || 0,
31
+ },
32
+ }]
33
+ end
34
+
35
+ attr_reader :end, :duration, :payload
36
+ end
37
+
38
+ def enable(client)
39
+ name = "process_action.action_controller"
40
+ ActiveSupport::Notifications.subscribe(name) do |*args|
41
+ event = ActiveSupport::Notifications::Event.new(*args)
42
+ metric = RequestMetric.new(event)
43
+ client.enqueue(metric)
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module Chillout
2
- VERSION = "0.8.2"
2
+ VERSION = "0.8.3"
3
3
  end
@@ -2,7 +2,8 @@ module Chillout
2
2
  class Worker
3
3
  attr_reader :dispatcher, :queue, :logger
4
4
 
5
- def initialize(dispatcher, queue, logger, container_class=CreationsContainer)
5
+ def initialize(max_queue, dispatcher, queue, logger, container_class=CreationsContainer)
6
+ @max_queue = max_queue
6
7
  @dispatcher = dispatcher
7
8
  @queue = queue
8
9
  @logger = logger
@@ -13,7 +14,7 @@ module Chillout
13
14
  logger.debug "Waiting for at least one container."
14
15
  all_containers = [queue.pop]
15
16
  logger.debug "Received at least one container."
16
- loop do
17
+ (@max_queue-1).times do
17
18
  begin
18
19
  all_containers << queue.pop(true)
19
20
  rescue ThreadError
@@ -25,19 +26,20 @@ module Chillout
25
26
  end
26
27
 
27
28
  def merge_containers(containers_to_merge)
29
+ mergable, unmergable = containers_to_merge.partition{|cont| @container_class === cont }
28
30
  creations_container = @container_class.new
29
- for container in containers_to_merge
31
+ mergable.each do |container|
30
32
  creations_container.merge(container)
31
33
  end
32
- creations_container
34
+ unmergable.unshift(creations_container) unless creations_container.empty?
35
+ unmergable
33
36
  end
34
37
 
35
- def send_creations(creations_container)
38
+ def send_measurements(measurements)
36
39
  logger.debug "Trying to send creations"
37
- dispatcher.send_creations(creations_container)
40
+ dispatcher.send_measurements(measurements)
38
41
  logger.info "Metrics sent"
39
42
  rescue Dispatcher::SendCreationsFailed
40
- queue << creations_container
41
43
  logger.error "Sending metrics failed"
42
44
  end
43
45
 
@@ -51,10 +53,11 @@ module Chillout
51
53
  send_startup_message
52
54
  loop do
53
55
  containers_to_merge = get_all_containers_to_process
54
- creations_container = merge_containers(containers_to_merge)
55
- send_creations(creations_container)
56
+ measurements = merge_containers(containers_to_merge)
57
+ send_measurements(measurements)
56
58
  sleep 5
57
59
  end
58
60
  end
61
+
59
62
  end
60
- end
63
+ end
data/lib/chillout.rb CHANGED
@@ -2,6 +2,7 @@ require "chillout/version"
2
2
  require "chillout/config"
3
3
  require "chillout/middleware/creations_monitor"
4
4
  require "chillout/integrations/sidekiq"
5
+ require "chillout/subscribers/action_controller_notifications"
5
6
  require "chillout/server_side/dispatcher"
6
7
  require "chillout/server_side/server_side"
7
8
  require "chillout/server_side/http_client"
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class ClientSendsControllerPerformanceTest < AcceptanceTestCase
4
+
5
+ def test_client_sends_controller_metrics
6
+ test_app = TestApp.new
7
+ test_endpoint = TestEndpoint.new(port: 8082)
8
+ test_user = TestUser.new
9
+
10
+ test_endpoint.listen
11
+ test_app.boot(chillout_port: 8082)
12
+ if ENV['STRATEGY'] != 'active_job'
13
+ assert test_endpoint.has_received_information_about_startup
14
+ end
15
+ test_user.create_entity('Myrmecophagidae')
16
+ assert request = test_endpoint.has_one_controller_metric
17
+
18
+ assert_equal "request", request["series"]
19
+ assert_equal "EntitiesController", request["tags"]["controller"]
20
+ assert_equal "create", request["tags"]["action"]
21
+ assert_equal "all", request["tags"]["format"]
22
+ assert_equal "POST", request["tags"]["method"]
23
+ assert_equal 302, request["tags"]["status"]
24
+
25
+ assert_equal 1, request["values"]["finished"]
26
+ assert_operator 0, :<, request["values"]["duration"]
27
+ assert_operator 0, :<, request["values"]["db"]
28
+ assert_equal 0, request["values"]["view"]
29
+ ensure
30
+ test_app.shutdown if test_app
31
+ end
32
+
33
+ end
@@ -4,16 +4,18 @@ class ClientSendsMetricsTest < AcceptanceTestCase
4
4
 
5
5
  def test_client_sends_metrics
6
6
  test_app = TestApp.new
7
- test_endpoint = TestEndpoint.new
7
+ test_endpoint = TestEndpoint.new(port: 8080)
8
8
  test_user = TestUser.new
9
9
 
10
10
  test_endpoint.listen
11
- test_app.boot
11
+ test_app.boot(chillout_port: 8080)
12
12
  if ENV['STRATEGY'] != 'active_job'
13
13
  assert test_endpoint.has_received_information_about_startup
14
14
  end
15
15
  test_user.create_entity('Myrmecophagidae')
16
- assert test_endpoint.has_one_creation_for_entity_resource
16
+ assert creation = test_endpoint.has_one_creation_for_entity_resource
17
+ assert_equal "Entity", creation["series"]
18
+ assert_equal 1, creation["values"]["creations"]
17
19
  ensure
18
20
  test_app.shutdown if test_app
19
21
  end
data/test/client_test.rb CHANGED
@@ -37,4 +37,17 @@ class ClientTest < ChilloutTestCase
37
37
  end
38
38
  end
39
39
 
40
- end
40
+ def test_enqueues_up_to_5K_metrics
41
+ client = Chillout::Client.new("xyz")
42
+ client.queue.expects(:size).returns(4999)
43
+ client.queue.expects(:<<).returns(nil)
44
+ client.enqueue(Object.new)
45
+ end
46
+
47
+ def test_does_not_enqueue_more_than_5K_metrics
48
+ client = Chillout::Client.new("xyz")
49
+ client.queue.expects(:size).returns(5000)
50
+ client.queue.expects(:<<).never
51
+ client.enqueue(Object.new)
52
+ end
53
+ end
data/test/config_test.rb CHANGED
@@ -11,6 +11,20 @@ class ConfigTest < ChilloutTestCase
11
11
  assert_equal "xyz123", @config.api_key
12
12
  end
13
13
 
14
+ def test_cannot_assing_wrong_api_key
15
+ assert_raises(ArgumentError) do
16
+ @config.api_key = Object.new
17
+ end
18
+ assert_raises(ArgumentError) do
19
+ Chillout::Config.new(Object.new)
20
+ end
21
+ end
22
+
23
+ def test_secret_setter
24
+ @config.secret = "SsEeCret"
25
+ assert_equal "SsEeCret", @config.api_key
26
+ end
27
+
14
28
  def test_update_with_options_hash
15
29
  @config.update(:platform => 'rack')
16
30
  assert_equal 'rack', @config.platform
@@ -23,5 +23,52 @@ module Chillout
23
23
  assert_equal 3, @creations_container[:User]
24
24
  assert_equal 1, @creations_container[:Entity]
25
25
  end
26
+
27
+ def test_equality
28
+ assert_equal CreationsContainer.new, CreationsContainer.new
29
+ assert_equal(
30
+ CreationsContainer.new.tap{|one| one.increment!("User") },
31
+ CreationsContainer.new.tap{|one| one.increment!("User") },
32
+ )
33
+ assert_equal(
34
+ CreationsContainer.new.tap{|one| one.increment!("User", 2) },
35
+ CreationsContainer.new.tap{|one| one.increment!("User", 2) },
36
+ )
37
+ refute_equal(
38
+ CreationsContainer.new.tap{|one| one.increment!("User", 3) },
39
+ CreationsContainer.new.tap{|one| one.increment!("User", 2) },
40
+ )
41
+ refute_equal(
42
+ CreationsContainer.new.tap{|one| one.increment!("User", 3) },
43
+ CreationsContainer.new.tap{|one| one.increment!("Post", 3) },
44
+ )
45
+ refute_equal(
46
+ Object.new,
47
+ CreationsContainer.new.tap{|one| one.increment!("Post", 3) },
48
+ )
49
+ refute_equal(
50
+ CreationsContainer.new.tap{|one| one.increment!("User", 3) },
51
+ Object.new,
52
+ )
53
+ end
54
+
55
+ def test_build_measurements_from_creations_container
56
+ creations_container = CreationsContainer.new
57
+ 5.times { creations_container.increment!("User") }
58
+ 3.times { creations_container.increment!("Cart") }
59
+ timestamp = Time.utc(2017, 6, 26, 15, 57, 10)
60
+
61
+ assert_equal [{
62
+ series: "User",
63
+ tags: {},
64
+ timestamp: "2017-06-26T15:57:10Z",
65
+ values: { creations: 5 },
66
+ },{
67
+ series: "Cart",
68
+ tags: {},
69
+ timestamp: "2017-06-26T15:57:10Z",
70
+ values: { creations: 3 },
71
+ }], creations_container.as_measurements(timestamp)
72
+ end
26
73
  end
27
- end
74
+ end
@@ -3,20 +3,20 @@ require 'test_helper'
3
3
  class DispatcherTest < ChilloutTestCase
4
4
  def test_send_creations
5
5
  server_side = mock("ServerSide")
6
- server_side.expects(:send_creations).with(:creations)
6
+ server_side.expects(:send_measurements).with(:creations)
7
7
 
8
8
  dispatcher = Chillout::Dispatcher.new(server_side)
9
- dispatcher.send_creations(:creations)
9
+ dispatcher.send_measurements(:creations)
10
10
  end
11
11
 
12
12
  def test_send_creations_raise_exception_if_server_side_failed_to_send_request
13
13
  server_side = stub()
14
- server_side.stubs(:send_creations).raises(Chillout::HttpClient::NotSent.new(:http_error))
14
+ server_side.stubs(:send_measurements).raises(Chillout::HttpClient::NotSent.new(:http_error))
15
15
 
16
16
  dispatcher = Chillout::Dispatcher.new(server_side)
17
17
 
18
18
  assert_raises Chillout::Dispatcher::SendCreationsFailed do
19
- dispatcher.send_creations(:creations)
19
+ dispatcher.send_measurements(:creations)
20
20
  end
21
21
  end
22
22
 
@@ -30,9 +30,9 @@ module Chillout
30
30
  def test_creations_values
31
31
  get "/"
32
32
  Thread.pass
33
- sleep 2
34
- assert_equal 2, request_body["content"]["creations"]["User"]
35
- assert_equal 3, request_body["content"]["creations"]["Cart"]
33
+ sleep 3
34
+ assert_equal 2, request_body["measurements"].find{|m| m["series"] == "User" }.fetch("values").fetch("creations")
35
+ assert_equal 3, request_body["measurements"].find{|m| m["series"] == "Cart" }.fetch("values").fetch("creations")
36
36
  end
37
37
 
38
38
  private
@@ -8,16 +8,4 @@ class ServerSideTest < ChilloutTestCase
8
8
  @http_client = mock("HttpClient")
9
9
  @server_side = Chillout::ServerSide.new(@event_data_builder, @http_client)
10
10
  end
11
-
12
- def test_send_creations_use_event_data_builder
13
- @event_data_builder.expects(:build_from_creations_container).with(:creations_container, anything).returns(:event_data_from_builder)
14
- @http_client.stubs(:post)
15
- @server_side.send_creations(:creations_container)
16
- end
17
-
18
- def test_send_creations_use_http_client
19
- @event_data_builder.stubs(:build_from_creations_container).returns(:event_data_from_builder)
20
- @http_client.expects(:post).with('/metrics', :event_data_from_builder)
21
- @server_side.send_creations(:creations_container)
22
- end
23
11
  end