Successfully reported this slideshow.

How to Test Asynchronous Code

2

Share

Loading in …3
×
1 of 43
1 of 43

How to Test Asynchronous Code

2

Share

Download to read offline

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.

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.

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

How to Test Asynchronous Code

  1. 1. How to Test Asynchronous Code by Felix Geisendörfer 05.05.2011 (v1)
  2. 2. @felixge Twitter / 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. TM We 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 times Ask audience to guess the numbers of stacked sheets of papers Ask 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 -> combinatoric explosion
  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.04x Library: 6184 LoC Tests: 12622 LoC Not included: Fixture data, server configuration, customer website, dependencies we developed
  22. 22. Transloadit Lines of code 10.000 7.500 5.000 2.500 0 integration micro integration vs. micro tests: 1x : 4.30x Integration tests: 2254 LoC Micro Tests: 9695 LoC
  23. 23. Transloadit Assertions 3.000 2.250 1.500 750 0 integration micro integration vs. micro tests: 1x : 12.30x Integration tests: 189 asserts Micro 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.35x Library: 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.89x Micro Tests: 1243 LoC Integration tests: 430 LoC
  26. 26. node-mysql Assertions 400 300 200 100 0 integration micro integration vs micro tests: 1x : 8.15x Micro Tests: 375 asserts Integration tests: 46 asserts
  27. 27. Why do you have brakes in a car? Because then you can drive faster. Kevlin Henney probably Kevlin Henney, http://olvemaudal.wordpress.com/2008/04/14/being-agile-no- speeding-please/
  28. 28. There really are only two acceptable models of development: "think and analyze" or "years and years of testing 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?

×