how to test asynchronous code

43
How to Test Asynchronous Code by Felix Geisendörfer 05.05.2011 (v1)

Upload: felix-geisendoerfer

Post on 15-Jan-2015

2.580 views

Category:

Technology


5 download

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

How to Test Asynchronous Code

by Felix Geisendörfer

05.05.2011 (v1)

Page 2: How to Test Asynchronous Code

@felixge

Twitter / GitHub / IRC

Page 3: How to Test Asynchronous Code

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

Story Time

Page 5: How to Test Asynchronous Code

~ June 2009

Page 6: How to Test Asynchronous Code

File uploading & processing as an infrastructure service for web & mobile applications.

- The app we run in production

Page 7: How to Test Asynchronous Code

We found node

Page 8: How to Test Asynchronous Code

We found node

TM

Page 9: How to Test Asynchronous Code

We fell in love

Page 10: How to Test Asynchronous Code

We had fun

Page 11: How to Test Asynchronous Code

We crashed- Or more specifically: node crashed

Page 12: How to Test Asynchronous Code

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

We had no tests

Page 14: How to Test Asynchronous Code

Time to rewrite- RailsConf 09: Robert Martin, "What Killed Smalltalk Could Kill Ruby, Too

Page 15: How to Test Asynchronous Code

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

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

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

How to Test Asynchronous

Code

Page 19: How to Test Asynchronous Code

Make it “synchronous”

Page 20: How to Test Asynchronous Code

Version 2

Page 21: How to Test Asynchronous Code

Well, almost : )

Page 22: How to Test Asynchronous Code

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

How to MicrotestNode.js Code

Term microtest vs Unit Test

Page 24: How to Test Asynchronous Code

Rule #1: No I/O

Page 25: How to Test Asynchronous Code

Rule #2: TDD

Page 26: How to Test Asynchronous Code

Rule #3: Discipline

Page 27: How to Test Asynchronous Code

Libraries

Page 28: How to Test Asynchronous Code

Numbers

Page 29: How to Test Asynchronous Code

Transloadit

Page 30: How to Test Asynchronous Code

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

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

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

node-mysql

Page 34: How to Test Asynchronous Code

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

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

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

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

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

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

Questions?

Page 41: How to Test Asynchronous Code

Questions?

Page 42: How to Test Asynchronous Code

Thank You

Page 43: How to Test Asynchronous Code

Thank You