Testing on the Toilet

August 21, 2008

Sleeping != Synchronization You've got some code that uses threads, and it's making your tests flaky and slow. How do you fix it? First, most of the code is probably still single-threaded: test those parts separately. But how to test the threading behavior itself? Often, threaded tests start out using sleeps to wait for something to happen. This test is trying to verify that DelegateToIntern spawns its work argument into a parallel thread and invokes a callback when it's done. def testInternMakesCoffee(self): self.caffeinated = False def DrinkCoffee(): self.caffeinated = True DelegateToIntern(work=Intern().MakeCoffee, callback=DrinkCoffee) self.assertFalse(self.caffeinated, "I watch YouTubework; intern brews") time.sleep(60) # 1min should be long enough to make coffee, right? self.assertTrue(self.caffeinated, "Where's mah coffee?!?")

Aside from abusing your intern every time you run the test, this test takes a minute longer than it needs to, and it may even fail when the machine (or intern!) is loaded in odd ways. You should always be skeptical of sleep statements, especially in tests. How can we make the test more reliable? The answer is to explicitly control when things happen within DelegateToIntern with a threading.Event in Python, a Notification in C++, or a CountDownLatch(1) in Java. def testInternMakesCoffee(self): is_started, can_finish, is_done = Event(), Event(), Event() def FakeCoffeeMaker(): is_started.set() # Allow is_started.wait() to return. # Wait up to 1min for can_finish.set() to be called. The timeout # prevents failures from hanging, but doesn't delay a passing test. can_finish.wait(timeout=60) # .await() in Java DelegateToIntern(work=FakeCoffeeMaker, callback=lambda:is_done.set()) is_started.wait(timeout=60) self.assertTrue(is_started.isSet(), "FakeCoffeeMaker should have started") self.assertFalse(is_done.isSet(), "Don't bug me before coffee's made") can_finish.set() # Now let FakeCoffeeMaker return. is_done.wait(timeout=60) self.assertTrue(is_done.isSet(), "Intern should ping when coffee's ready")

Now we're guaranteed that no part of the test runs faster than we expect, and the test passes very quickly. It could run slowly when it fails, but you can easily lower the timeouts while you're debugging it. We'll look at testing for race conditions in a future episode. No interns were harmed in the making of this TotT.

More information, discussion, and archives: http://googletesting.blogspot.com Copyright © 2007 Google, Inc. Licensed under a Creative Commons Attribution–ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).


Aug 21, 2008 - More information, discussion, and archives: http://googletesting.blogspot.com. Copyright © 2007 Google, Inc. Licensed under a Creative ...

135KB Sizes 0 Downloads 174 Views

Recommend Documents

this episode
Mar 6, 2008 - Code coverage (also called test coverage) measures which lines of source code have been ... Check coverage results from your tests.

Episode 17 - Annemarie Cross
May 6, 2016 - Katy you've been really fortunate throughout your career. You've had ... Unequivocally, I think as I've grown in my career and I think this is really.

Episode 38 - Annemarie Cross
Sep 30, 2016 - WomenInLeadershipPodcast.com |Connect with your Host onTwitter:@AnnemarieCoach. Email: [email protected] ... they're working in their own business and they would love to have their book. To get the time and get thoug

Episode 44 - Annemarie Cross
Nov 11, 2016 - You've set up and run your own awards and your conference. ..... want to bring that approach into how you do business and you're consistent with that. Alli: Yes. .... device. Anyway have a fantastic week. Thanks again Alli. Alli:.

Episode 16 - Annemarie Cross
Apr 29, 2016 - She is committed to giving back to the industry that has provided her income and ..... culture and the name that you want in your business. So very, very .... Why I mention that is that as leaders we can set example. Our attitudes.

Episode 16 - Annemarie Cross
Apr 29, 2016 - Joining me on today's show is Tracy Hall and Tracy is a mentor and trainer. She's ... people see the direct sales industry as an ethical business.

[Full Episode] Dance Moms Season 4, Episode 13 Online.pdf ...
[Full Episode] Dance Moms Season 4, Episode 13 Online.pdf. [Full Episode] Dance Moms Season 4, Episode 13 Online.pdf. Open. Extract. Open with. Sign In.

Episode 22 - Dr. Axe
I've been following Tony for years, watching his fitness. DVDs ... I had done a weightlifting class in college. ... the one thing about California is, is that it's sunny.

Episode 17 - Annemarie Cross
May 6, 2016 - necessarily mean chaos. It means large complex solutions and global corporate. It ... today would like to get in contact. What's the best way?

Episode 38 - Annemarie Cross
30 Sep 2016 - Hi, I'm your host. Annemarie Cross branding communication strategist also known as the podcasting queen. Joining me on today's show is Stacey Copas, and she not let ... Instead, she has used her life experience and personal philosophies

Episode 44 - Annemarie Cross
Nov 11, 2016 - earth can I ever possibly stand out from what they're doing and offering? But ... You've set up and run your own awards and your conference.

GirlsDoPorn Episode 203
Itcould be niceto think that which forevermoreshall bea person such has the. knightshould exist inAmericatoday. ... Thestranger book is_safe:1.Rock and roll pdf.