chillout 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,18 +27,14 @@ module Chillout
27
27
 
28
28
  @logger = PrefixedLogger.new("Chillout", @config.logger)
29
29
 
30
- @http_client = HttpClient.new(@config, logger)
31
- @event_data_builder = EventDataBuilder.new(@config)
32
- @server_side = ServerSide.new(@event_data_builder, @http_client)
30
+ @http_client = HttpClient.new(@config, logger).freeze
31
+ @event_data_builder = EventDataBuilder.new(@config).freeze
32
+ @server_side = ServerSide.new(@event_data_builder, @http_client).freeze
33
33
  @filter = ErrorFilter.new
34
- @dispatcher = Dispatcher.new(@filter, @server_side)
34
+ @dispatcher = Dispatcher.new(@filter, @server_side).freeze
35
35
  @queue = Queue.new
36
36
  end
37
37
 
38
- def send_exception(exception, environment = {})
39
- send_error(Error.new(exception, environment))
40
- end
41
-
42
38
  def enqueue(creations)
43
39
  @queue << creations
44
40
  end
@@ -1,5 +1,7 @@
1
1
  module Chillout
2
2
  class Dispatcher
3
+ class SendCreationsFailed < StandardError
4
+ end
3
5
 
4
6
  def initialize(filter, server_side)
5
7
  @filter = filter
@@ -20,6 +22,7 @@ module Chillout
20
22
  def send_creations(creations)
21
23
  @server_side.send_creations(creations)
22
24
  rescue HttpClient::NotSent
25
+ raise SendCreationsFailed.new
23
26
  end
24
27
  end
25
28
  end
@@ -1,3 +1,3 @@
1
1
  module Chillout
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -2,28 +2,46 @@ module Chillout
2
2
  class Worker
3
3
  attr_reader :dispatcher, :queue, :logger
4
4
 
5
- def initialize(dispatcher, queue, logger)
5
+ def initialize(dispatcher, queue, logger, container_class=CreationsContainer)
6
6
  @dispatcher = dispatcher
7
7
  @queue = queue
8
8
  @logger = logger
9
+ @container_class = container_class
9
10
  end
10
11
 
11
- def run
12
+ def get_all_containers_to_process
13
+ all_containers = [queue.pop]
12
14
  loop do
13
- containers_to_merge = [queue.pop]
14
- loop do
15
- begin
16
- containers_to_merge << queue.pop(true)
17
- rescue ThreadError
18
- break
19
- end
20
- end
21
- creations_container = CreationsContainer.new
22
- for container in containers_to_merge
23
- creations_container.merge(container)
15
+ begin
16
+ all_containers << queue.pop(true)
17
+ rescue ThreadError
18
+ break
24
19
  end
25
- dispatcher.send_creations(creations_container)
26
- logger.info "Sending metrics"
20
+ end
21
+ all_containers
22
+ end
23
+
24
+ def merge_containers(containers_to_merge)
25
+ creations_container = @container_class.new
26
+ for container in containers_to_merge
27
+ creations_container.merge(container)
28
+ end
29
+ creations_container
30
+ end
31
+
32
+ def send_creations(creations_container)
33
+ dispatcher.send_creations(creations_container)
34
+ logger.info "Sending metrics"
35
+ rescue Dispatcher::SendCreationsFailed
36
+ queue << creations_container
37
+ logger.error "Sending metrics failed"
38
+ end
39
+
40
+ def run
41
+ loop do
42
+ containers_to_merge = get_all_containers_to_process
43
+ creations_container = merge_containers(containers_to_merge)
44
+ send_creations(creations_container)
27
45
  sleep 5
28
46
  end
29
47
  end
@@ -56,13 +56,15 @@ class DispatcherTest < ChilloutTestCase
56
56
  dispatcher.send_creations(:creations)
57
57
  end
58
58
 
59
- def test_send_creations_ignore_not_sent_exception
59
+ def test_send_creations_raise_exception_if_server_side_failed_to_send_request
60
60
  server_side = stub()
61
61
  server_side.stubs(:send_creations).raises(Chillout::HttpClient::NotSent.new(:http_error))
62
62
 
63
63
  dispatcher = Chillout::Dispatcher.new(mock, server_side)
64
64
 
65
- assert_nil dispatcher.send_creations(:creations)
65
+ assert_raises Chillout::Dispatcher::SendCreationsFailed do
66
+ dispatcher.send_creations(:creations)
67
+ end
66
68
  end
67
69
 
68
70
  end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module Chillout
4
+ class WorkerTest < ChilloutTestCase
5
+ def setup
6
+ @dispatcher = stub
7
+ @queue = stub
8
+ @container = stub
9
+ @logger = stub(info: "", error: "")
10
+ @container_class = stub(new: @container)
11
+ @worker = Worker.new(@dispatcher, @queue, @logger, @container_class)
12
+ end
13
+
14
+ def test_get_all_containers_to_process_pops_all_existings_jobs_from_queue
15
+ @queue.expects(:pop).times(3).returns(:container1, :container2).then.raises(ThreadError)
16
+ all_jobs = @worker.get_all_containers_to_process
17
+ assert_equal 2, all_jobs.count
18
+ assert_includes all_jobs, :container1
19
+ assert_includes all_jobs, :container2
20
+ end
21
+
22
+ def test_merge_containers_to_one
23
+ @container.expects(:merge).with(:container1)
24
+ @container.expects(:merge).with(:container2)
25
+ result = @worker.merge_containers([:container1, :container2])
26
+ assert_equal @container, result
27
+ end
28
+
29
+ def test_send_creations_just_send_creations_with_dispatcher
30
+ @dispatcher.expects(:send_creations).with(:creations_container)
31
+ @worker.send_creations(:creations_container)
32
+ end
33
+
34
+ def test_send_creations_with_interuption
35
+ @dispatcher.stubs(:send_creations).raises(Dispatcher::SendCreationsFailed)
36
+ @queue.expects(:<<).with(:creations_container)
37
+ @worker.send_creations(:creations_container)
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chillout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-12 00:00:00.000000000 Z
13
+ date: 2013-03-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
@@ -149,12 +149,12 @@ files:
149
149
  - test/http_client_test.rb
150
150
  - test/integration/creations_monitor_rack_test.rb
151
151
  - test/integration/exception_monitor_rack_test.rb
152
- - test/integration/standalone_client_test.rb
153
152
  - test/middleware/creations_monitor_test.rb
154
153
  - test/middleware/exception_monitor_test.rb
155
154
  - test/prefixed_logger_test.rb
156
155
  - test/server_side_test.rb
157
156
  - test/test_helper.rb
157
+ - test/worker_test.rb
158
158
  homepage: http://chillout.io/
159
159
  licenses: []
160
160
  post_install_message:
@@ -169,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  segments:
171
171
  - 0
172
- hash: -4082367495555425928
172
+ hash: 111791620312850416
173
173
  required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  none: false
175
175
  requirements:
@@ -178,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
178
  version: '0'
179
179
  segments:
180
180
  - 0
181
- hash: -4082367495555425928
181
+ hash: 111791620312850416
182
182
  requirements: []
183
183
  rubyforge_project:
184
184
  rubygems_version: 1.8.25
@@ -195,9 +195,9 @@ test_files:
195
195
  - test/http_client_test.rb
196
196
  - test/integration/creations_monitor_rack_test.rb
197
197
  - test/integration/exception_monitor_rack_test.rb
198
- - test/integration/standalone_client_test.rb
199
198
  - test/middleware/creations_monitor_test.rb
200
199
  - test/middleware/exception_monitor_test.rb
201
200
  - test/prefixed_logger_test.rb
202
201
  - test/server_side_test.rb
203
202
  - test/test_helper.rb
203
+ - test/worker_test.rb
@@ -1,61 +0,0 @@
1
- require 'test_helper'
2
- require 'webmock/minitest'
3
-
4
- class StandaloneClientTest < ChilloutTestCase
5
-
6
- def setup
7
- api_key = "xyz123"
8
- stub_api_request(api_key, "events")
9
- config = Chillout::Config.new(api_key)
10
- config.ssl = false
11
- @client = Chillout::Client.new(config)
12
- @error = build_error(NameError, "FooBar is not defined")
13
- end
14
-
15
- def test_request_headers_contain_content_type
16
- send_error
17
- assert_request_headers 'events', 'Content-Type' => 'application/vnd.chillout.v1+json'
18
- end
19
-
20
- def test_request_body_contains_exception_class
21
- send_error
22
- assert_equal "NameError", request_body["content"]["class"]
23
- end
24
-
25
- def test_request_body_contains_exception_message
26
- send_error
27
- assert_equal "FooBar is not defined", request_body["content"]["message"]
28
- end
29
-
30
- def test_request_body_contains_event_type
31
- send_error
32
- assert_equal "exception", request_body["event"]
33
- end
34
-
35
- def test_request_body_contains_notifier_name
36
- send_error
37
- assert_equal "Chillout", request_body["notifier"]["name"]
38
- end
39
-
40
- def test_request_body_contains_notifier_version
41
- send_error
42
- assert_equal Chillout::VERSION, request_body["notifier"]["version"]
43
- end
44
-
45
- def test_allows_to_send_exceptions
46
- exception = build_exception(ArgumentError, "wrong number of arguments")
47
- @client.send_exception(exception)
48
- assert_equal "ArgumentError", request_body["content"]["class"]
49
- assert_equal "wrong number of arguments", request_body["content"]["message"]
50
- end
51
-
52
- private
53
- def send_error
54
- @client.send_error(@error)
55
- end
56
-
57
- def request_body
58
- assert_request_body("events") { |body| return body }
59
- end
60
-
61
- end