27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Overview 29<br />

That 0.0 sample right after 100.0 looks suspicious. Perhaps there is a poor connection<br />

at the sensor? But a few minutes later we see:<br />

2006-11-07 16:10:21Z Temperature server at port 8023 reports 99.8 F<br />

2006-11-07 16:10:31Z Temperature server at port 8023 reports 99.9 F<br />

2006-11-07 16:10:41Z Temperature server at port 8023 reports 100.0 F<br />

2006-11-07 16:10:51Z Temperature server at port 8023 reports 99.0 F<br />

2006-11-07 16:11:01Z Temperature server at port 8023 reports 999.0 F<br />

2006-11-07 16:11:11Z Temperature server at port 8023 reports 899.0 F<br />

That doesn’t look like a bad connection. Investigating at the embedded computer<br />

confirms that the sensor and server program are working correctly.<br />

It seems significant that troubles appear after the temperature reaches 100. But<br />

our unit test for 212 passed. What could be wrong?<br />

2.10 Failures explained<br />

After some investigation we discover the defect: the incorrect code that caused the<br />

failures. 3 The client’s receive buffer is only four bytes long (BUFLEN in the Client<br />

class, Figure 2.5). <strong>We</strong> intended 40, not 4: a typographical error that was not caught by<br />

the compiler, by inspection, or by unit testing. When the server sends more than four<br />

characters, for example, 100.0, the client only reads the first four, 100. here, and then<br />

finds the remaining character(s) in the buffer the next time it calls Receive. In this<br />

way, a single sample can cause the client to become unsynchronized with the server,<br />

resulting in incorrect readings on one or more subsequent samples. 4 The unit test of<br />

Section 2.8 passed because the client Receive method uses Double.Parse to convert<br />

the buffer contents to a floating point number. This returns the same result when<br />

trailing zeroes are omitted, so the assertion did not detect that the final character was<br />

missing from the sample. This test only acquired one sample; subsequent samples<br />

that would have caused the assertion to fail were not requested.<br />

You might object that this example is naive and unrealistic. In fact, it is a simplified<br />

account of a real experience that was too complicated to describe here in full detail.<br />

2.11 Lessons learned<br />

Some defects can only be revealed by realistic scenarios that resemble actual application<br />

program runs (as in Section 2.9), where we execute long sequences of<br />

3 <strong>We</strong> distinguish defects (flaws in the code) from failures (occurrences where the program does<br />

the wrong thing). Both are sometimes called bugs.<br />

4 You can observe this by instrumenting Server.Send and Client.Receive with code that<br />

shows the contents of sendBuf and receiveBuf, respectively.<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!