spec/flipper/middleware/local_cache_spec.rb in flipper-0.2.1 vs spec/flipper/middleware/local_cache_spec.rb in flipper-0.3.0

- old
+ new

@@ -1,43 +1,24 @@ require 'helper' require 'rack/test' require 'flipper/middleware/local_cache' +require 'flipper/adapters/operation_logger' +require 'flipper/adapters/memory' describe Flipper::Middleware::LocalCache do include Rack::Test::Methods - class LoggedHash < Hash - attr_reader :reads, :writes - - Read = Struct.new(:key) - Write = Struct.new(:key, :value) - - def initialize(*args) - @reads, @writes = [], [] - super - end - - def [](key) - @reads << Read.new(key) - super - end - - def []=(key, value) - @writes << Write.new(key, value) - super - end - end - class Enum < Struct.new(:iter) - def each(&b) - iter.call(&b) + def each(&block) + iter.call(&block) end end - let(:source) { LoggedHash.new } - let(:adapter) { Flipper::Adapters::Memory.new(source) } - let(:flipper) { Flipper.new(adapter) } + let(:source) { {} } + let(:memory_adapter) { Flipper::Adapters::Memory.new(source) } + let(:adapter) { Flipper::Adapters::OperationLogger.new(memory_adapter) } + let(:flipper) { Flipper.new(adapter) } let(:app) { # ensure scoped for builder block, annoying... instance = flipper middleware = described_class @@ -53,10 +34,14 @@ run lambda {|env| raise "FAIL!" } end end.to_app } + before do + adapter.reset + end + it "delegates" do called = false app = lambda { |env| called = true [200, {}, nil] @@ -75,13 +60,13 @@ middleware.call({}) end it "enables local cache for body each" do app = lambda { |env| - [200, {}, Enum.new(lambda { |&b| + [200, {}, Enum.new(lambda { |&block| flipper.adapter.using_local_cache?.should be_true - b.call "hello" + block.call "hello" })] } middleware = described_class.new app, flipper body = middleware.call({}).last body.each { |x| x.should eql('hello') } @@ -108,10 +93,11 @@ flipper.adapter.local_cache.should be_empty end it "really does cache" do flipper[:stats].enable + adapter.reset app = lambda { |env| flipper[:stats].enabled? flipper[:stats].enabled? flipper[:stats].enabled? @@ -122,10 +108,12 @@ [200, {}, []] } middleware = described_class.new app, flipper middleware.call({}) - source.reads.map(&:key).should eq(["stats/boolean"]) + adapter.operations.should eq([ + Flipper::Adapters::OperationLogger::Read.new("stats/boolean"), + ]) end context "with a successful request" do it "clears the local cache" do flipper.adapter.local_cache.should_receive(:clear).twice