how to test asynchronous code
DESCRIPTION
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.TRANSCRIPT
![Page 1: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/1.jpg)
How to Test Asynchronous Code
by Felix Geisendörfer
05.05.2011 (v1)
![Page 2: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/2.jpg)
@felixge
Twitter / GitHub / IRC
![Page 3: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/3.jpg)
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
![Page 4: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/4.jpg)
Story Time
![Page 5: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/5.jpg)
~ June 2009
![Page 6: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/6.jpg)
File uploading & processing as an infrastructure service for web & mobile applications.
- The app we run in production
![Page 7: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/7.jpg)
We found node
![Page 8: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/8.jpg)
We found node
TM
![Page 9: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/9.jpg)
We fell in love
![Page 10: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/10.jpg)
We had fun
![Page 11: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/11.jpg)
We crashed- Or more specifically: node crashed
![Page 12: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/12.jpg)
Transloadit v1
• Complete failure
• Node randomly crashed with:
(evcom) recv() Success
• A mess of code, hard to maintain
~1.5 Years ago
* 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 ...
![Page 13: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/13.jpg)
We had no tests
![Page 14: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/14.jpg)
Time to rewrite- RailsConf 09: Robert Martin, "What Killed Smalltalk Could Kill Ruby, Too
![Page 15: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/15.jpg)
How to Test Asynchronous
Code
- We looked at asynchronous testing frameworks- We really wanted to do TDD- But everything we tried was hard
![Page 16: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/16.jpg)
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?
![Page 17: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/17.jpg)
Some old news
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 -> combinatoric explosion
![Page 18: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/18.jpg)
How to Test Asynchronous
Code
![Page 19: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/19.jpg)
Make it “synchronous”
![Page 20: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/20.jpg)
Version 2
![Page 21: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/21.jpg)
Well, almost : )
![Page 22: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/22.jpg)
Transloadit v2
• Processed many TB of data
• Very low bug rate
• Clean code base, ~99% test coverage
Now
![Page 23: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/23.jpg)
How to MicrotestNode.js Code
Term microtest vs Unit Test
![Page 24: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/24.jpg)
Rule #1: No I/O
![Page 25: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/25.jpg)
Rule #2: TDD
![Page 26: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/26.jpg)
Rule #3: Discipline
![Page 27: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/27.jpg)
Libraries
![Page 28: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/28.jpg)
Numbers
![Page 29: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/29.jpg)
Transloadit
![Page 30: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/30.jpg)
Transloadit
0
3.250
6.500
9.750
13.000
library tests
Lines of code
library vs test code: 1x : 2.04x
Library: 6184 LoCTests: 12622 LoC
Not included: Fixture data, server configuration, customer website, dependencies we developed
![Page 31: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/31.jpg)
Transloadit
0
2.500
5.000
7.500
10.000
integration micro
integration vs. micro tests: 1x : 4.30x
Lines of code
Integration tests: 2254 LoCMicro Tests: 9695 LoC
![Page 32: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/32.jpg)
Transloadit
0
750
1.500
2.250
3.000
integration micro
Assertions
integration vs. micro tests: 1x : 12.30x
Integration tests: 189 assertsMicro Tests: 2324 asserts
![Page 33: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/33.jpg)
node-mysql
![Page 34: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/34.jpg)
node-mysql
0
425
850
1.275
1.700
library tests
Lines of code
library vs test code: 1x : 1.35x
Library: 1240 LoC (+600 LoC MySql constants)Tests:Tests: 1673 LoC
![Page 35: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/35.jpg)
node-mysql
0
325
650
975
1.300
integration micro
integration vs micro tests: 1x : 2.89x
Lines of code
Micro Tests: 1243 LoCIntegration tests: 430 LoC
![Page 36: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/36.jpg)
node-mysql
0
100
200
300
400
integration micro
Assertions
integration vs micro tests: 1x : 8.15x
Micro Tests: 375 assertsIntegration tests: 46 asserts
![Page 37: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/37.jpg)
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/
![Page 38: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/38.jpg)
There really are only two acceptable models of development: "think and analyze" or "years and years of testing on thousands of machines".
Linus Torvalds
![Page 39: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/39.jpg)
tl;dr
• Don’t use integration tests to show the basic correctness of your software.
• Write more microtests.
![Page 40: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/40.jpg)
Questions?
![Page 41: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/41.jpg)
Questions?
☝
![Page 42: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/42.jpg)
Thank You
![Page 43: How to Test Asynchronous Code](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b794c04a79591d4a8b46b8/html5/thumbnails/43.jpg)
Thank You