forkbreak / fork_break

Fork with breakpoints, for testing multiprocess behaviour.
MIT License
61 stars 6 forks source link

`include ForkBreak::Breakpoints` sets a nil object #13

Closed vemv closed 8 years ago

vemv commented 8 years ago

Hi, I'm having trouble using the include ForkBreak::Breakpoints mechanism in any Rails model or plain class.

class Foo

  include ForkBreak::Breakpoints

  def bar
    breakpoints << :test
  end

end
[8] pry(main)> Foo.new.bar
NoMethodError: undefined method `<<' for nil:NilClass

Using 0.1.3.

Any clue?

Cheers - Victor

pedrocarrico commented 8 years ago

Hi @vemv,

You can only access the breakpoints if you're inside a ForkBreak process. Picking up on your example above:

require 'fork_break'

class Foo
  include ForkBreak::Breakpoints

  def bar
    breakpoints << :test
  end
end

process = ForkBreak::Process.new { Foo.new.bar }

process.run_until(:test).wait # runs and stops at the :test breakpoint
process.finish # runs the process until finished

Check out the specs for another example. Perhaps a better error message would help in this case or perhaps skip breakpoints if not in a ForkBreak process.

Thanks,

vemv commented 8 years ago

Ah, I see. Thanks!

Hmmm. I had imagined that by default breakpoints were always skipped if the current process is not a ForkBreak one. Else adding breakpoints would break production code, wouldn't they?

As I see it, one should be free to add breakpoints to controllers/models/etc, which would be a no-op in production. That way one could use the same exact code in prod and test envs...

Thoughts?

Cheers - Victor

pedrocarrico commented 8 years ago

Yes, that makes perfect sense!

I'll try to cook up the next version ASAP with that in mind.

Many thanks!

vemv commented 8 years ago

Thanks to you!

vemv commented 8 years ago

I played quite a lot with fork_break last night and was able to add breakpoints in production code without breaking anything. Feel free to merge!

pedrocarrico commented 8 years ago

Thank you for "playing" with fork_break, your comments matter a lot to us.

I'll rebase all PRs shortly and hopefully soon I'll push a new version to rubygems.

remen commented 8 years ago

Excellent! Being able to set ignored breakpoints in production code was of course the intention all along!