Re: Something is wrong with my threads
Ryan Davis <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <[email protected]> |
> On Apr 18, 2019, at 00:45, Andy Jones <[email protected]> wrote: > Good question. I don't _think_ I can. I want the code in the thread to be executed IN that thread, not the main one? Isn't that what #join does? (In MRI this doesn't really matter, but in jRuby it does, and this code has to work in both. See below.) That is NOT what join does. Join runs the thread (in the thread, not main) and waits for it to finish. But it sounds like that isn’t what you want based on this: > The _real_ code, the one I want to use, is inside an rspec test. It's testing a race condition between multiple threads. I start 50 threads, give them a time, and then when I restart them they all try and call the same method at that time. Then I test whether the method screwed up or not. So, yes, it has control issues. Testing is all about that ;) You don’t test race conditions by simulating non-deterministic conditions. You test a race condition by deterministically faking the race collision. You do this by designing your code to allow for your test to interact with it deterministically… I just helped someone with EXACTLY this problem by suggesting that they either refactor their code so the race creation condition is in it’s own method and thus overridable for testing OR by adding a `yield if block_given?` in the middle so the test running the method can force the race condition to happen via a block. OR you don’t test it at all, because that way lies madness. Or you design your code to not have the race condition at all (better). Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>