Skip to content

Latest commit

 

History

History
90 lines (59 loc) · 2.15 KB

README.md

File metadata and controls

90 lines (59 loc) · 2.15 KB

dl_experiment

A lightweight scientist-like framework to refactor critical paths.

Requirements

  • Ruby 2.3+

Usage

Just drop this line in your Gemfile:

gem 'dl_experiment'

Let's consider the following method we'd like to refactor:

def allows?(user)
  model.check_user?(user).valid?
end

If we'd like to use cancancan, we could add the following experiment:

def allows?(user)
  Experiment.protocol('cancancan') do |e|
    e.legacy      { model.check_user?(user).valid? }  # old way
    e.experiment  { user.can?(:read, model) }         # new way
  end
end

Your code will still return the same thing (or raise the same exception), but, from now on, assuming you are using rails, your test suite will fail if there is a single time where both implementations are not returning the same thing.

More interesting: you can trigger a code block when there is a difference and log it the way you'd like:

def allows?(user)
  Experiment.protocol('cancancan') do |e|
    e.legacy      { model.check_user?(user).valid? }  # old way
    e.experiment  { user.can?(:read, model) }         # new way

    e.on_diff do |legacy, experiment|
      Rails.logger.warn(
        "[Experiment][User:#{user}] Results not equals: " +
        "#{legacy.value} != #{experiment.value}"
      )
    end
  end
end

This will allow you to compare, even in production, some code implementation.

Warning: Be careful with side effects. You don't want to create twice the same data in your database in production. Don't experiment on non-functional code.

Motivation

Sometimes, you'd like to change some code you don't fully understand and that is not fully covered by your tests.

This tool, like scientist (the framework from github), is made to help you do that, but with a smaller integration cost.

Feature set

Runnings tests

bundle
rspec

Authors

License

MIT © Doctolib

Additional resources

Alternatives: