Skip to content

Instantly share code, notes, and snippets.

@nessamurmur
Created August 31, 2016 00:08
Show Gist options
  • Save nessamurmur/c22b8514ea47e345ba15ebb44f1047ef to your computer and use it in GitHub Desktop.
Save nessamurmur/c22b8514ea47e345ba15ebb44f1047ef to your computer and use it in GitHub Desktop.
cache bug example
require 'dalli'
class ThingController
def create
thing = Thing.new(params)
thing.special_id = dalli.increment(:special_id, 1)
thing.save!
end
end
### Weird constraints...
# Because of some historical decisions
# the special id couldn't go below some really large number...
# something like 100,600
#
# We started seeing bugs where `special_id` would become corrupt.
# We'd get varying unicode characters instead of numbers.
# We tracked down the bug for several days without being able to
# reproduce it locally.
#
# Why did we not use a more reliable counter? Say,
# one provided by our database? Hell if I know...
# It was a decision made so long ago no one was left
# who understood why it was made.
require "spec_helper"
require "dalli"
describe "things with special ids" do
# We had some test like this already
describe "incrementing special_id" do
it "should get incremented by 1" do
n = 100600
dalli.set(:special_id, n)
# post to our controller with valid params
expect { dalli.get(special_id) }.to eq(n + 1)
end
end
# I added this generative test so that in CI we'd get
# 1000 runs of the test every time
# This seems like overkill in this example... but in
# the real life version there was a reason it was preferable
# to doing something like `1000.times do`. I no longer remember it.
# It definitely did something more than just run the test repeatedly,
# but I can't remember what the extra thing involved was here.
generative do
@n = 100600
describe "incrementing special_id" do
it "should get incremented by 1" do
# post to our controller with valid params
@n += 1
expect { dalli.get(special_id) }.to eq(@n)
end
end
end
# This worked... but it was *terribly* inefficient. It added
# An extra 30 seconds to a minute to our build by itself.
# So we really didn't want to leave it in but we did want
# to find the bug semi-passively if possible while we focused
# on other urgent things. So I wrote a test like this to "expire"
# the other test.
describe "temporary generative test" do
it "should get removed on January 15th" do
if Date.today > Date.new(2014,1,15)
fail "Please remove the generative test in this file"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment