twirl 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  require "forwardable"
2
2
  require "kjess"
3
3
  require "twirl/item"
4
+ require "twirl/mirror"
4
5
  require "twirl/instrumenters/noop"
5
6
 
6
7
  module Twirl
@@ -30,6 +31,9 @@ module Twirl
30
31
  # Private: What should be used to instrument all the things.
31
32
  attr_reader :instrumenter
32
33
 
34
+ # Private: What handles dumping and loading values.
35
+ attr_reader :encoder
36
+
33
37
  # Private: What errors should be considered retryable.
34
38
  attr_reader :retryable_errors
35
39
 
@@ -47,6 +51,7 @@ module Twirl
47
51
  # before rotating to the next client (default: 100)
48
52
  # :retries - The Number of times a command should be retried (default: 5).
49
53
  # :instrumenter - Where to send instrumention (defaults: noop).
54
+ # :encoder - What to use to dump/load vlues (defaults: mirror).
50
55
  def initialize(clients, options = {})
51
56
  @client_index = 0
52
57
  @command_count = 0
@@ -54,6 +59,7 @@ module Twirl
54
59
  @retries = options.fetch(:retries, 5)
55
60
  @commands_per_client = options.fetch(:commands_per_client, 100)
56
61
  @instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
62
+ @encoder = options.fetch(:encoder, Mirror)
57
63
  @retryable_errors = options.fetch(:retryable_errors, RetryableErrors)
58
64
  end
59
65
 
@@ -73,11 +79,16 @@ module Twirl
73
79
  with_retries { |tries|
74
80
  @instrumenter.instrument("op.twirl") { |payload|
75
81
  payload[:op] = :set
76
- payload[:bytes] = item.size
82
+ payload[:bytes] = item.to_s.size
77
83
  payload[:queue_name] = queue_name
78
84
  payload[:retry] = tries != @retries
79
85
 
80
- client.set(queue_name, item, expiration)
86
+ value = if item
87
+ @encoder.dump(item)
88
+ else
89
+ nil
90
+ end
91
+ client.set(queue_name, value, expiration)
81
92
  }
82
93
  }
83
94
  end
@@ -244,6 +255,7 @@ module Twirl
244
255
 
245
256
  if value = client.send(op, queue_name, *args)
246
257
  payload[:bytes] = value.size
258
+ value = @encoder.load(value) if value
247
259
  Item.new queue_name, value, client, @instrumenter
248
260
  else
249
261
  rotate_for_next_op
@@ -304,6 +316,7 @@ module Twirl
304
316
  }
305
317
  end
306
318
 
319
+ # Private: Retries an operation a number of times if it raises exception.
307
320
  def with_retries
308
321
  tries = @retries
309
322
  begin
@@ -0,0 +1,19 @@
1
+ module Twirl
2
+ class Mirror
3
+ def self.dump(value)
4
+ value
5
+ end
6
+
7
+ def self.load(value)
8
+ value
9
+ end
10
+
11
+ def dump(value)
12
+ value
13
+ end
14
+
15
+ def load(value)
16
+ value
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Twirl
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -15,6 +15,7 @@ class ClusterTest < Minitest::Test
15
15
  assert_equal [KJess::NetworkError, KJess::ServerError],
16
16
  cluster.retryable_errors
17
17
  assert_equal Twirl::Instrumenters::Noop, cluster.instrumenter
18
+ assert_equal Twirl::Mirror, cluster.encoder
18
19
  end
19
20
 
20
21
  def test_initialize_shuffles_clients
@@ -31,6 +32,12 @@ class ClusterTest < Minitest::Test
31
32
  assert_equal instrumenter, cluster.instrumenter
32
33
  end
33
34
 
35
+ def test_overriding_encoder
36
+ encoder = Object.new
37
+ cluster = Twirl::Cluster.new([], encoder: encoder)
38
+ assert_equal encoder, cluster.encoder
39
+ end
40
+
34
41
  def test_overriding_retryable_errors
35
42
  retryable_errors = StandardError
36
43
  cluster = Twirl::Cluster.new([], retryable_errors: retryable_errors)
@@ -466,4 +473,32 @@ class ClusterTest < Minitest::Test
466
473
  end
467
474
  end
468
475
  end
476
+
477
+ def test_values_dumped_and_loaded_by_encoder
478
+ encoder = Minitest::Mock.new
479
+ encoder.expect :dump, :dump, ["data"]
480
+ encoder.expect :load, :load, ["data"]
481
+
482
+ cluster = build(:mock_cluster, encoder: encoder)
483
+ cluster[0].expect :set, true, ["testing", :dump, 0]
484
+ cluster.set("testing", "data")
485
+
486
+ cluster[0].expect :get, "data", ["testing", {}]
487
+ assert_equal :load, cluster.get("testing").value
488
+
489
+ encoder.verify
490
+ end
491
+
492
+ def test_nil_values_not_dumped_or_loaded
493
+ encoder = Minitest::Mock.new
494
+
495
+ cluster = build(:mock_cluster, encoder: encoder)
496
+ cluster[0].expect :set, true, ["testing", nil, 0]
497
+ cluster.set("testing", nil)
498
+
499
+ cluster[0].expect :get, nil, ["testing", {}]
500
+ assert_nil cluster.get("testing")
501
+
502
+ encoder.verify
503
+ end
469
504
  end
@@ -0,0 +1,24 @@
1
+ require "helper"
2
+ require "twirl/mirror"
3
+
4
+ class MirrorTest < Minitest::Test
5
+ def test_dump
6
+ data = "data"
7
+ assert_equal data, Twirl::Mirror.dump(data)
8
+ end
9
+
10
+ def test_load
11
+ data = "data"
12
+ assert_equal data, Twirl::Mirror.load(data)
13
+ end
14
+
15
+ def test_dump
16
+ data = "data"
17
+ assert_equal data, Twirl::Mirror.new.dump(data)
18
+ end
19
+
20
+ def test_load
21
+ data = "data"
22
+ assert_equal data, Twirl::Mirror.new.load(data)
23
+ end
24
+ end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["nunemaker@gmail.com"]
11
11
  spec.description = %q{Wrapper for kjess that works with multiple kestrel instances intelligently.}
12
12
  spec.summary = %q{Wrapper for kjess that works with multiple kestrel instances intelligently.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/jnunemaker/twirl"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twirl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-15 00:00:00.000000000 Z
12
+ date: 2013-09-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: kjess
@@ -65,6 +65,7 @@ files:
65
65
  - lib/twirl/instrumenters/memory.rb
66
66
  - lib/twirl/instrumenters/noop.rb
67
67
  - lib/twirl/item.rb
68
+ - lib/twirl/mirror.rb
68
69
  - lib/twirl/server.rb
69
70
  - lib/twirl/version.rb
70
71
  - script/bootstrap
@@ -80,10 +81,11 @@ files:
80
81
  - test/instrumenters/noop_test.rb
81
82
  - test/integration/cluster_test.rb
82
83
  - test/item_test.rb
84
+ - test/mirror_test.rb
83
85
  - test/support/fake_udp_socket.rb
84
86
  - test/twirl_test.rb
85
87
  - twirl.gemspec
86
- homepage: ''
88
+ homepage: https://github.com/jnunemaker/twirl
87
89
  licenses:
88
90
  - MIT
89
91
  post_install_message:
@@ -117,5 +119,6 @@ test_files:
117
119
  - test/instrumenters/noop_test.rb
118
120
  - test/integration/cluster_test.rb
119
121
  - test/item_test.rb
122
+ - test/mirror_test.rb
120
123
  - test/support/fake_udp_socket.rb
121
124
  - test/twirl_test.rb