NoBrainerORM / nobrainer

Ruby ORM for RethinkDB
http://nobrainer.io/
Other
387 stars 49 forks source link

"undefined method `<<' for false:FalseClass" on `machine_id` from `NoBrainer::Document::PrimaryKey::Generator._generate` with Rspec #267

Closed zedtux closed 3 years ago

zedtux commented 3 years ago

I got that quite strange issue with Rspec while building a new gem.

I'm using rspec-rails with the default spec/spec_helper.rb file:

# frozen_string_literal: true

require 'bundler/setup'
require 'byebug'

require 'nobrainer'
# require 'rspec/core'
# require 'rspec/expectations'

Dir[
  File.join(File.expand_path(__dir__), 'models', '*.rb')
].sort.each { |file| require file }

require 'my_gem'

RSpec.configure do |config|
  # Enable flags like --only-failures and --next-failure
  config.example_status_persistence_file_path = '.rspec_status'

  # These two settings work together to allow you to limit a spec run
  # to individual examples or groups you care about by tagging them with
  # `:focus` metadata. When nothing is tagged with `:focus`, all examples
  # get run.
  config.filter_run :focus
  config.run_all_when_everything_filtered = true

  # Disable RSpec exposing methods globally on `Module` and `main`
  config.disable_monkey_patching!

  config.expect_with :rspec do |c|
    c.syntax = :expect
  end

  config.include RSpec::Matchers
  config.include NoBrainer::Matchers
end

My test is using a NoBrainer document to test a field but fails with:

     NoMethodError:
       undefined method `<<' for false:FalseClass
     # /nobrainer/lib/no_brainer/document/primary_key/generator.rb:65:in `_generate'
     # /nobrainer/lib/no_brainer/document/primary_key/generator.rb:82:in `block in generate'
     # /nobrainer/lib/no_brainer/document/primary_key/generator.rb:82:in `synchronize'
     # /nobrainer/lib/no_brainer/document/primary_key/generator.rb:82:in `generate'
     # /nobrainer/lib/no_brainer/document/primary_key.rb:48:in `block in define_default_pk'
     # /nobrainer/lib/no_brainer/document/attributes.rb:67:in `instance_exec'
     # /nobrainer/lib/no_brainer/document/attributes.rb:67:in `block in assign_defaults'
     # /nobrainer/lib/no_brainer/document/attributes.rb:53:in `each'
     # /nobrainer/lib/no_brainer/document/attributes.rb:53:in `assign_defaults'
     # /nobrainer/lib/no_brainer/document/attributes.rb:93:in `assign_attributes'
     # /nobrainer/lib/no_brainer/document/types.rb:14:in `assign_attributes'
     # /nobrainer/lib/no_brainer/document/polymorphic.rb:12:in `assign_attributes'
     # /nobrainer/lib/no_brainer/document/missing_attributes.rb:23:in `assign_attributes'
     # /nobrainer/lib/no_brainer/document/lazy_fetch.rb:15:in `assign_attributes'
     # /nobrainer/lib/no_brainer/document/atomic_ops.rb:216:in `assign_attributes'
     # /nobrainer/lib/no_brainer/document/attributes.rb:19:in `_initialize'
     # /nobrainer/lib/no_brainer/document/persistance.rb:6:in `_initialize'
     # /nobrainer/lib/no_brainer/document/callbacks.rb:19:in `block in initialize'
     # /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/callbacks.rb:101:in `run_callbacks'
     # /nobrainer/lib/no_brainer/document/callbacks.rb:19:in `initialize'
     # ./spec/unit/document_spec.rb:7:in `block (3 levels) in <top (required)>'

The error come from the NoBrainer::Document::PrimaryKey::Generator._generate method, from the last line:

    (timestamp << TIMESTAMP_SHIFT) | (sequence << SEQUENCE_SHIFT) |
      (machine_id << MACHINE_ID_SHIFT) | (pid << PID_SHIFT)

Actually machine_id is false because of this line:

    machine_id = NoBrainer::Config.machine_id & MACHINE_ID_MASK

NoBrainer::Config.machine_id being nil make the code returning false. Adding a to_i on it "solve" the issue but look weird so I'd like to get your input @nviennot if you will :)

zedtux commented 3 years ago

Okay, so I actually figured it out: I was not calling NoBrainer.configure do |config| ... end in the Rspec initialization.