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/).

episode

Aug 21, 2008 - def DrinkCoffee(): self.caffeinated = True. DelegateToIntern(work=Intern().MakeCoffee, callback=DrinkCoffee) self.assertFalse(self.caffeinated ...

135KB Sizes 1 Downloads 172 Views

Recommend Documents

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

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.

this episode
Mar. 6, 2008. Understanding Your Coverage Data ... Statement coverage does not measure the percentage of unique execution paths exercised. Limitations of statement ... recovery when kRecoverableError is returned. With tests that generate ...

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 20 - Annemarie Cross
May 27, 2016 - would say go for it and don't underestimate the power of what it can do .... it's stopping you from capturing and communicating what makes you ...

Episode 22 - Dr. Axe
Tony: You know, I had somewhat of a securities route to my land of fitness. .... we'll go out and buy a new phone, even though the last one we bought was only .... we...there are these companies out there, these fast food companies, these food ...

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 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 19 - Annemarie Cross
May 19, 2016 - Email: [email protected] So many women, men too but women in leadership that's what this show is all about, they struggle to communicate the value that they offer. So whether it is talking to a prospective client or

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 20 - Annemarie Cross
May 27, 2016 - won't, and what a wonderful problem to have and then in the early days ... in I personally sign it for them and then I give it to them and they are ...

Episode 38 - Annemarie Cross
Sep 30, 2016 - Annemarie Cross: You're listening to Women in Leadership podcast episode 38. 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 n

EpisodE 2: REVoLUTioN
The Declaration of Independence is read to jubilant crowds in New. York on July 9, 1776. America's thirteen colonies have taken on the might of the world's ...

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 19 - Annemarie Cross
May 19, 2016 - was an ambulance. I turned around I said, “Mate, this is why I wanted to leave early. “ he laughed at me and he said, “Mom, if we were early, we ...

Episode 16 - Annemarie Cross
Apr 29, 2016 - Joining me on today's show is Tracy Hall and Tracy is a mentor and ... passionate about and that you really want to stand up for and bring this ...

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 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 19 - Annemarie Cross
WomenInLeadershipPodcast.com |Connect with your Host onTwitter:@AnnemarieCoach. Email: [email protected] Episode 19. Published: 19 May 2016. Host: Annemarie Cross. Annemarie Cross: You're listening to Women and Leadership Podcast,

Episode 17 - Annemarie Cross
May 6, 2016 - Web:www.WomenInLeadershipPodcast.com |Connect with your Host onTwitter:@AnnemarieCoach ... own and that certainly is not the case and certainly not the best way to go about it. I think in terms of my experience ...

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.