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

How to Test Asynchronous Code - Amazon Web …felixge.s3.amazonaws.com/11/how-to-test-async-code.pdfby Felix Geisendörfer 05.05.2011 (v1) @felixge Twitter / GitHub / IRC Core Contributor

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

How to Test Asynchronous Code

by Felix Geisendörfer

05.05.2011 (v1)

@felixge

Twitter / GitHub / IRC

Core Contributor

&

Module Author

node-mysql node-formidable

There really are only two acceptable models of development: "think and analyze" or "years and years of testing on thousands of machines".

Linus Torvalds

Story Time

~ June 2009

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

We found node

We found node

TM

We fell in love

We had fun

We crashed

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

We had no tests

Time to rewrite

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 }); }); });});

Some old news

How to Test Asynchronous

Code

Make it “synchronous”

Version 2

Well, almost : )

Transloadit v2

• Processed many TB of data

• Very low bug rate

• Clean code base, ~99% test coverage

Now

How to MicrotestNode.js Code

Rule #1: No I/O

Rule #2: TDD

Rule #3: Discipline

Libraries

felixge/node-fake

felixge/node-microtest

node-microtest

• Load test files in separate v8 context

• node-fake as mixin

• Basic test.describe(name, fn) framework

felixge/node-far

Numbers

Transloadit

Transloadit

0

3.250

6.500

9.750

13.000

library tests

Lines of code

library vs test code: 1x : 2.04x

Transloadit

0

2.500

5.000

7.500

10.000

integration micro

integration vs. micro tests: 1x : 4.30x

Lines of code

Transloadit

0

750

1.500

2.250

3.000

integration micro

Assertions

integration vs. micro tests: 1x : 12.30x

node-mysql

node-mysql

0

425

850

1.275

1.700

library tests

Lines of code

library vs test code: 1x : 1.35x

node-mysql

0

325

650

975

1.300

integration micro

integration vs micro tests: 1x : 2.89x

Lines of code

node-mysql

0

100

200

300

400

integration micro

Assertions

integration vs micro tests: 1x : 8.15x

Why do you have brakes in a car? Because then you can drive faster.

Kevlin Henney

tl;dr

• Don’t use integration tests to show the basic correctness of your software.

• Write more microtests.

Questions?

Questions?

Thank You

Thank You