How to Test Asynchronous Code

4,037 views

Published on

My nodeconf talk from May 5th. Reworked / better slides are available in my second version of the talk at the BerlinJS group from May 19th.

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,037
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
12
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

How to Test Asynchronous Code

  1. 1. How to TestAsynchronous Code by Felix Geisendörfer 05.05.2011 (v1)
  2. 2. @felixgeTwitter / GitHub / IRC
  3. 3. Core Contributor & Module Author node-mysql node-formidable- Joined the mailing list in June 26, 2009- When I joined there where #24 people- Now mailing list has close to 4000 members members- First patch in September 2009
  4. 4. File uploading & processing as an infrastructure service for web & mobile applications.- The app we run in production
  5. 5. We found node
  6. 6. TMWe found node
  7. 7. We fell in love
  8. 8. We had fun
  9. 9. We crashed- Or more specifically: node crashed
  10. 10. ~1 .5 Ye Transloadit v1 a rs ag o • Complete failure • Node randomly crashed with: (evcom) recv() Success • A mess of code, hard to maintain * http://transloadit.com/blog/2010/04/to-launch-or-not- 0.1.18- New version of node took out promises- So we thought about refactoring, but ...
  11. 11. We had no tests
  12. 12. Time to rewrite- RailsConf 09: Robert Martin, "What Killed Smalltalk Could Kill Ruby, Too
  13. 13. - We looked at asynchronous testing frameworks- We really wanted to do TDD- But everything we tried was hard
  14. 14. db.query(SELECT A, function() { db.query(SELECT B, function() { db.query(SELECT C, function() { db.query(SELECT D, function() { // WTF }); }); }); });- Who here is running node in production?- Raise hands if you have a test suite for your node.js projects- Raise hands if you are happy with it- But why is it so difficult?
  15. 15. Fold news paper 7 timesAsk audience to guess the numbers of stacked sheets of papersAsk person to guess right (128) to come up front to rip through the folded paper.What’s this about?- Newspaper can be thought of as a simple computer program managing 7 booleans- Each boolean has a meaningful impact on the behavior of the Software- This means that ripping through it at once, with a single, becomes very difficult- The problem becomes even worse when your variables have more than 2 states-> the euquation turns from being exponential into combinatoric one -> combinatoricexplosion
  16. 16. Make it “synchronous”
  17. 17. Version 2
  18. 18. Well, almost : )
  19. 19. N Transloadit v2 ow• Processed many TB of data• Very low bug rate• Clean code base, ~99% test coverage
  20. 20. Term microtest vs Unit Test
  21. 21. Transloadit Lines of code 13.000 9.750 6.500 3.250 0 library tests library vs test code: 1x : 2.04xLibrary: 6184 LoCTests: 12622 LoCNot included: Fixture data, server configuration, customer website, dependencies wedeveloped
  22. 22. Transloadit Lines of code 10.000 7.500 5.000 2.500 0 integration micro integration vs. micro tests: 1x : 4.30xIntegration tests: 2254 LoCMicro Tests: 9695 LoC
  23. 23. Transloadit Assertions 3.000 2.250 1.500 750 0 integration micro integration vs. micro tests: 1x : 12.30xIntegration tests: 189 assertsMicro Tests: 2324 asserts
  24. 24. node-mysql Lines of code 1.700 1.275 850 425 0 library tests library vs test code: 1x : 1.35xLibrary: 1240 LoC (+600 LoC MySql constants)Tests:Tests: 1673 LoC
  25. 25. node-mysql Lines of code 1.300 975 650 325 0 integration micro integration vs micro tests: 1x : 2.89xMicro Tests: 1243 LoCIntegration tests: 430 LoC
  26. 26. node-mysql Assertions 400 300 200 100 0 integration micro integration vs micro tests: 1x : 8.15xMicro Tests: 375 assertsIntegration tests: 46 asserts
  27. 27. Why do you have brakes in a car? Because then you can drive faster. Kevlin Henneyprobably Kevlin Henney, http://olvemaudal.wordpress.com/2008/04/14/being-agile-no-speeding-please/
  28. 28. There really are only two acceptablemodels of development: "think andanalyze" or "years and years oftesting on thousands of machines". Linus Torvalds
  29. 29. tl;dr• Don’t use integration tests to show the basic correctness of your software.• Write more microtests.
  30. 30. Questions?
  31. 31. Questions?

×