firebolt 0.13.0 → 0.13.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0065c480c50f012bb42b0721eea423e5900870b0
4
- data.tar.gz: 94eb5045e18dd501a20edf20a81c3223b4346b48
3
+ metadata.gz: c5f6d3818aa3574b3122735bda4c7687ed19bdf4
4
+ data.tar.gz: 04fd500afdae9d599e22f6599b3ec5ab2ab743e0
5
5
  SHA512:
6
- metadata.gz: ffa7d19032757582ecdc2bc0fa16254cafd8853f83ea74c67a99eb681e2fc5930df49b18043522a451f915b38b788af2d0150f11068fb1e7dc7d2de13b5e6a4d
7
- data.tar.gz: 6aed00633b8315bdead63914601d285d1d04f66bc141a72d931a2305959288c48e5975897b78c8a253851f20e5cf0a34c8cfd7ac4983be75c235708d0be21809
6
+ metadata.gz: 96299bfa9cf4729757264a178ea7c1981e86ffc194a3f1e9851da4805be9f7aebf13b716166e25ef82207e95f72634ae237855e9adb402d5cc97930df14d6fb1
7
+ data.tar.gz: 01844bf0b93e6b574da623e140f8b25edbf83cf7c910b8b0ddae452d3f1723b4e41aef044a45bfc19b511f2fd16c5c18890d3df3c0ba4487d21b9c9d50a3bc6b
data/firebolt.gemspec CHANGED
@@ -24,9 +24,12 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency "json"
25
25
  spec.add_dependency "rufus-scheduler", "~> 3.0"
26
26
  spec.add_dependency "concurrent-ruby", "~> 1.0"
27
+ spec.add_dependency "activesupport"
27
28
 
28
29
  ##
29
30
  # Development Dependencies
30
31
  #
32
+ spec.add_development_dependency "pry"
31
33
  spec.add_development_dependency "rake"
34
+ spec.add_development_dependency "rspec"
32
35
  end
@@ -3,36 +3,36 @@ module Firebolt
3
3
  include ::Firebolt::Keys
4
4
 
5
5
  def delete(key_suffix, options = nil)
6
- salted_key = self.cache_key_with_salt(key_suffix, salt)
6
+ salted_key = cache_key_with_salt(key_suffix, salt)
7
7
  return nil if salted_key.nil?
8
8
 
9
9
  ::Firebolt.config.cache.delete(salted_key, options)
10
10
  end
11
11
 
12
- def fetch(suffix, options = nil, &block)
13
- salted_key = self.cache_key_with_salt(key_suffix, salt)
12
+ def fetch(key_suffix, options = nil, &block)
13
+ salted_key = cache_key_with_salt(key_suffix, salt)
14
14
  return nil if salted_key.nil?
15
15
 
16
16
  ::Firebolt.config.cache.fetch(salted_key, options, &block)
17
17
  end
18
18
 
19
19
  def read(key_suffix, options = nil)
20
- salted_key = self.cache_key_with_salt(key_suffix, salt)
20
+ salted_key = cache_key_with_salt(key_suffix, salt)
21
21
  return nil if salted_key.nil?
22
22
 
23
23
  ::Firebolt.config.cache.read(salted_key, options)
24
24
  end
25
25
 
26
26
  def reset_salt!(new_salt)
27
- ::Firebolt.config.cache.write(self.salt_key, new_salt)
27
+ ::Firebolt.config.cache.write(salt_key, new_salt)
28
28
  end
29
29
 
30
30
  def salt
31
- ::Firebolt.config.cache.read(self.salt_key)
31
+ ::Firebolt.config.cache.read(salt_key)
32
32
  end
33
33
 
34
34
  def write(key_suffix, value, options = {})
35
- salted_key = self.cache_key_with_salt(key_suffix, salt)
35
+ salted_key = cache_key_with_salt(key_suffix, salt)
36
36
  return nil if salted_key.nil?
37
37
 
38
38
  options.merge!(:expires_in => ::Firebolt.config.warming_frequency + 1.hour)
@@ -78,7 +78,7 @@ module Firebolt
78
78
 
79
79
  def warmer=(value)
80
80
  raise ArgumentError, "Warmer must include the ::Firebolt::Warmer module." unless value.ancestors.include?(::Firebolt::Warmer)
81
- raise ArgumentError, "Warmer must respond to `perform`." unless value.instance_methods.include?(:perform)
81
+ raise ArgumentError, "Warmer must respond to #perform." unless value.instance_methods.include?(:perform)
82
82
 
83
83
  self[:warmer] = value
84
84
  end
@@ -7,7 +7,7 @@ module Firebolt
7
7
  def perform
8
8
  return nil unless cache_file_exists?
9
9
 
10
- return parsed_contents
10
+ parsed_contents
11
11
  end
12
12
 
13
13
  private
@@ -1,3 +1,3 @@
1
1
  module Firebolt
2
- VERSION = "0.13.0"
2
+ VERSION = "0.13.2"
3
3
  end
data/lib/firebolt.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "active_support"
2
+ require "active_support/core_ext/numeric"
3
+
1
4
  require "json"
2
5
  require "securerandom"
3
6
  require "concurrent"
@@ -17,20 +20,21 @@ module Firebolt
17
20
  extend ::Firebolt::Cache
18
21
 
19
22
  # Using a mutex to control access while creating a ::Firebolt::Config
20
- @firebolt_mutex = ::Mutex.new
23
+ FIREBOLT_MUTEX = ::Mutex.new
24
+ CONFIGURE_MUTEX = ::Mutex.new
21
25
 
22
26
  def self.config
23
27
  return @config unless @config.nil?
24
28
 
25
- @firebolt_mutex.synchronize do
26
- @config = ::Firebolt::Config.new if @config.nil?
29
+ FIREBOLT_MUTEX.synchronize do
30
+ @config ||= ::Firebolt::Config.new
27
31
  end
28
32
 
29
- return @config
33
+ @config
30
34
  end
31
35
 
32
36
  def self.configure
33
- ::Thread.exclusive do
37
+ CONFIGURE_MUTEX.synchronize do
34
38
  yield(config)
35
39
  end
36
40
  end
@@ -42,7 +46,7 @@ module Firebolt
42
46
 
43
47
  scheduler = ::Rufus::Scheduler.new
44
48
  scheduler.every(warming_frequency) do
45
- ::Firebolt::WarmCacheJob.new.async.perform(config.warmer)
49
+ ::Firebolt::WarmCacheJob.new.perform(config.warmer)
46
50
  end
47
51
  end
48
52
 
@@ -66,11 +70,11 @@ module Firebolt
66
70
  def self.initialized!
67
71
  return @initialized unless @initialized.nil?
68
72
 
69
- @firebolt_mutex.synchronize do
70
- @initialized = true if @initialized.nil?
73
+ FIREBOLT_MUTEX.synchronize do
74
+ @initialized ||= true
71
75
  end
72
76
 
73
- return @initialized
77
+ @initialized
74
78
  end
75
79
 
76
80
  def self.initialized?
@@ -0,0 +1,73 @@
1
+ require "spec_helper"
2
+
3
+ describe ::Firebolt::Cache do
4
+ mock_firebolt_cache!
5
+ subject { klass.new }
6
+ let(:klass) { ::Class.new { include ::Firebolt::Cache } }
7
+
8
+ let(:key_suffix) { "test" }
9
+ let(:options) { { :some => :thing } }
10
+ let(:salt) { "1234" }
11
+ let(:salt_key) { "firebolt.mx.salt" }
12
+ let(:salted_key) { "yolo" }
13
+
14
+ before do
15
+ ::Firebolt.config.namespace = "mx"
16
+ ::Firebolt.config.warming_frequency = 1000
17
+ end
18
+
19
+ context "with a valid salt key" do
20
+ before do
21
+ allow(subject).to receive(:salt_key).and_return(salt_key)
22
+ allow(subject).to receive(:salt).and_return(salt)
23
+ allow(subject).to receive(:cache_key_with_salt).with(key_suffix, salt).and_return(salted_key)
24
+ end
25
+
26
+ describe "#delete" do
27
+ it "deletes the key" do
28
+ expect(::Firebolt.config.cache).to receive(:delete).with(salted_key, options)
29
+ subject.delete(key_suffix, options)
30
+ end
31
+ end
32
+
33
+ describe "#fetch" do
34
+ it "fetches out the key" do
35
+ expect(::Firebolt.config.cache).to receive(:fetch).with(salted_key, options)
36
+ subject.fetch(key_suffix, options)
37
+ end
38
+ end
39
+
40
+ describe "#read" do
41
+ it "reads out the key" do
42
+ expect(::Firebolt.config.cache).to receive(:read).with(salted_key, options)
43
+ subject.read(key_suffix, options)
44
+ end
45
+ end
46
+
47
+ describe "#write" do
48
+ let(:merged_options) { options.merge(:expires_in => 4600) }
49
+ let(:value) { "idk" }
50
+
51
+ it "write the key" do
52
+ expect(::Firebolt.config.cache).to receive(:write).with(salted_key, value, merged_options)
53
+ subject.write(key_suffix, value, options)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "#reset_salt!" do
59
+ let(:new_salt_key) { "new_salt_key" }
60
+
61
+ it "can reset the salt key" do
62
+ expect(::Firebolt.config.cache).to receive(:write).with(salt_key, new_salt_key)
63
+ subject.reset_salt!(new_salt_key)
64
+ end
65
+ end
66
+
67
+ describe "#salt" do
68
+ it "can get the salt key" do
69
+ expect(::Firebolt.config.cache).to receive(:read).with(salt_key)
70
+ subject.salt
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+
3
+ describe ::Firebolt::Config do
4
+ subject { described_class.new(options) }
5
+
6
+ let(:options) { { :namespace => "mx" } }
7
+
8
+ describe ".hash_accessor" do
9
+ it "creates a reader and writer" do
10
+ described_class.hash_accessor :thing
11
+ expect { subject.thing = :no_way }.to_not raise_error
12
+ expect(subject.thing).to eq(:no_way)
13
+ expect(subject[:thing]).to eq(:no_way)
14
+ end
15
+ end
16
+
17
+ describe "#namespace" do
18
+ it "can build a namespace" do
19
+ expect(subject.namespace).to eq("firebolt.mx")
20
+ end
21
+ end
22
+
23
+ describe "#warmer=" do
24
+ context "when does not include ::Firebolt::Warmer" do
25
+ it "raises an error" do
26
+ expect { subject.warmer = ::String }.to raise_error(::ArgumentError, /must include the ::Firebolt::Warmer/)
27
+ end
28
+ end
29
+
30
+ context "when does not have a #perform method" do
31
+ let(:warmer) { ::Class.new { include ::Firebolt::Warmer } }
32
+
33
+ it "raises an error" do
34
+ expect { subject.warmer = warmer }.to raise_error(::ArgumentError, /must respond to #perform/)
35
+ end
36
+ end
37
+
38
+ context "when includes ::Firebolt::Warmer and responsd to #perform" do
39
+ let(:warmer) do
40
+ ::Class.new do
41
+ include ::Firebolt::Warmer
42
+
43
+ def perform
44
+ end
45
+ end
46
+ end
47
+
48
+ it "does not raise an error" do
49
+ expect { subject.warmer = warmer }.to_not raise_error
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ describe ::Firebolt::Keys do
4
+ let(:klass) { ::Class.new { include ::Firebolt::Keys } }
5
+ subject { klass.new }
6
+
7
+ before { ::Firebolt.config.namespace = "mx" }
8
+
9
+ describe "#cache_key" do
10
+ it "creates a cache key via a namespace" do
11
+ expect(subject.cache_key("yolo")).to eq("firebolt.mx.yolo")
12
+ end
13
+ end
14
+
15
+ describe "#cache_key_with_salt" do
16
+ it "creates a cache key with key suffix" do
17
+ expect(subject.cache_key_with_salt("yolo", "test")).to eq("firebolt.mx.test.yolo")
18
+ end
19
+ end
20
+
21
+ describe "#namespace" do
22
+ it "gets the namespace from the config" do
23
+ expect(subject.namespace).to eq("firebolt.mx")
24
+ end
25
+ end
26
+
27
+ describe "#salt_key" do
28
+ it "creates a salt key" do
29
+ expect(subject.salt_key).to eq("firebolt.mx.salt")
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ describe ::Firebolt::Warmer do
4
+ mock_firebolt_cache!
5
+
6
+ let(:klass) do
7
+ ::Class.new do
8
+ include ::Firebolt::Warmer
9
+
10
+ def initialize(results)
11
+ @results = results
12
+ end
13
+
14
+ def perform
15
+ @results
16
+ end
17
+ end
18
+ end
19
+ subject { klass.new(results) }
20
+ let(:results) { { "some" => "thing" } }
21
+
22
+ before do
23
+ ::Firebolt.config.namespace = "mx"
24
+ ::Firebolt.config.warming_frequency = 1000
25
+ end
26
+
27
+ describe "#warm" do
28
+ it "calls #perform on the warmer class" do
29
+ expect(subject).to receive(:perform).and_return(results)
30
+ expect(subject).to receive(:_warmer_reset_salt!)
31
+ expect(subject).to receive(:_warmer_write_results_to_cache).with(results)
32
+ subject.warm
33
+ end
34
+
35
+ it "writes the results of #perform to the cache" do
36
+ allow(subject).to receive(:_warmer_salt).and_return("test")
37
+ expect(subject).to receive(:_warmer_expires_in).and_return(4600)
38
+ expect(subject).to receive(:_warmer_salted_cache_key).with("some").and_return("firebolt.mx.test.some")
39
+ expect(::Firebolt.config.cache).to receive(:write).with("firebolt.mx.test.some", "thing", :expires_in => 4600)
40
+ subject.warm
41
+ end
42
+
43
+ context "when an result that does not respond to #each_pair is returned from #perform" do
44
+ let(:results) { nil }
45
+
46
+ it "raises an error" do
47
+ expect { subject.warm }.to raise_error("Warmer must return an object that responds to #each_pair.")
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,86 @@
1
+ require "spec_helper"
2
+
3
+ describe ::Firebolt do
4
+ mock_firebolt_cache!
5
+
6
+ describe ".config" do
7
+ it "creates a config" do
8
+ expect(described_class.config).to be_a(Firebolt::Config)
9
+ end
10
+ end
11
+
12
+ describe ".configure" do
13
+ it "can be configured via a block" do
14
+ described_class.configure do |config|
15
+ expect(config).to eq(described_class.config)
16
+ end
17
+ end
18
+ end
19
+
20
+ describe ".initialize_rufus_scheduler" do
21
+ let(:scheduler) { ::Rufus::Scheduler.new }
22
+
23
+ before { ::Firebolt.config.warming_frequency = 1000 }
24
+
25
+ it "creates an instance of rufus scheduler" do
26
+ # TODO: Test the config.warmer call.
27
+ allow(::Rufus::Scheduler).to receive(:new).and_return(scheduler)
28
+ expect(scheduler).to receive(:every).with("1000")
29
+ described_class.initialize_rufus_scheduler
30
+ end
31
+ end
32
+
33
+ describe ".initialize!" do
34
+ let(:warmer) do
35
+ ::Class.new do
36
+ include ::Firebolt::Warmer
37
+
38
+ def perform
39
+ {}
40
+ end
41
+ end
42
+ end
43
+ let(:warmer_instance) { warmer.new }
44
+
45
+ before do
46
+ ::Firebolt.config.warming_frequency = 1000
47
+ ::Firebolt.config.warmer = warmer
48
+ end
49
+
50
+ it "calls the warmer" do
51
+ expect(warmer).to receive(:new).and_return(warmer_instance)
52
+ expect(warmer_instance).to receive(:warm)
53
+ described_class.initialize!
54
+ sleep 0.1 # Await async future to complete
55
+ end
56
+ end
57
+
58
+ describe ".skip_warming?" do
59
+ context "when firebolt set to skip" do
60
+ before { ENV["FIREBOLT_SKIP_WARMING"] = "true" }
61
+ after { ENV["FIREBOLT_SKIP_WARMING"] = nil }
62
+
63
+ it "skips the warming" do
64
+ expect(described_class).to be_skip_warming
65
+ end
66
+ end
67
+
68
+ context "when rails testing mode" do
69
+ before { ENV["RAILS_ENV"] = "test" }
70
+ after { ENV["RAILS_ENV"] = nil }
71
+
72
+ it "skips the warming" do
73
+ expect(described_class).to be_skip_warming
74
+ end
75
+ end
76
+
77
+ context "when no skip flags are set" do
78
+ before { ENV["FIREBOLT_SKIP_WARMING"] = nil }
79
+ after { ENV["FIREBOLT_SKIP_WARMING"] = "true" }
80
+
81
+ it "does not skip the warming" do
82
+ expect(described_class).to_not be_skip_warming
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require "firebolt"
4
+ require "pry"
5
+
6
+ RSpec.configure do
7
+ def mock_firebolt_cache!
8
+ let(:_mock_firebolt_cache) { double(:write => nil, :fetch => nil, :delete => nil, :read => nil) }
9
+
10
+ before { ::Firebolt.config.cache = _mock_firebolt_cache }
11
+ after { ::Firebolt.config.cache = nil }
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firebolt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hutchison
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-01-26 00:00:00.000000000 Z
12
+ date: 2016-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -53,6 +53,34 @@ dependencies:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '1.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: activesupport
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
56
84
  - !ruby/object:Gem::Dependency
57
85
  name: rake
58
86
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +95,20 @@ dependencies:
67
95
  - - ">="
68
96
  - !ruby/object:Gem::Version
69
97
  version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rspec
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
70
112
  description: Simple little cache warmer.
71
113
  email:
72
114
  - liveh2o@gmail.com
@@ -91,6 +133,12 @@ files:
91
133
  - lib/firebolt/version.rb
92
134
  - lib/firebolt/warm_cache_job.rb
93
135
  - lib/firebolt/warmer.rb
136
+ - spec/lib/firebolt/cache_spec.rb
137
+ - spec/lib/firebolt/config_spec.rb
138
+ - spec/lib/firebolt/key_spec.rb
139
+ - spec/lib/firebolt/warmer_spec.rb
140
+ - spec/lib/firebolt_spec.rb
141
+ - spec/spec_helper.rb
94
142
  homepage: https://github.com/moneydesktop/firebolt
95
143
  licenses:
96
144
  - MIT
@@ -116,4 +164,10 @@ signing_key:
116
164
  specification_version: 4
117
165
  summary: Firebolt is a simple cache warmer. It warms the cache using a specially defined
118
166
  warmer class.
119
- test_files: []
167
+ test_files:
168
+ - spec/lib/firebolt/cache_spec.rb
169
+ - spec/lib/firebolt/config_spec.rb
170
+ - spec/lib/firebolt/key_spec.rb
171
+ - spec/lib/firebolt/warmer_spec.rb
172
+ - spec/lib/firebolt_spec.rb
173
+ - spec/spec_helper.rb