The day I started believing in Unit Tests

Right after getting my degree, I was lucky to be hired into a small but decent R&D department as an embedded software engineer. My first task was to create a Unit Test project and have it run in our build pipeline after compilation. I was not particularly excited about this task, it seemed like busywork to me. I believed that Unit Tests are largely worthless throwaway code. They probably didn't want me to immediately dive into production code. Fair enough, I thought. I'm the newcomer and this is something that is requested by engineers who are way more experienced than I am. So for now, I should just go along with it, even if I believe it's a waste of time.

So, ahead I went. I implemented the test suite and some basic tests, including some that I ported from our old, deprecated test framework to googletest. The test was started by our build pipeline after compilation, it actually transferred the test binary to a physical device which was (and still is) situated in our office and ran the QNX operating system to perform on-target tests. This was necessary because the code used many QNX primitives and needed to be tested on that operating system. So, the test was something between a Unit Test and an Integration Test. The mechanism worked well and it ran multiple times per day. Since the test coverage was quite small and nobody ever touched that code anyway, it always succeeded - another reason why I thought it's a waste of time. Also, it was rare for new tests to be added. It's very hard to write Unit Tests for embedded firmware and hardware abstractions because the very thing you want to test is the interaction with the hardware, which is precisely what you can't do in pure code. Because the test ran automatically and always succeeded, we all soon forgot about it and moved on to other things.

Fast forward about a year. The test ran hundreds if not thousands of times successfully. What a waste of time... But then, one day, we started observing test failures. Not many, maybe three over the course of a few weeks. The test actually crashed with a Segmentation Fault, so it was clear that it was a severe error. Interestingly, none of the code under test had actually changed. Well, that's definitely something we had to investigate! I spare you the details of the search for the error, but eventually, I was able to reproduce the problem while a debugger was attached, so the entire context of the problem was handed to me on a silver platter.

The problem had to do with how our threading abstraction, which worked with inheritance, was used in the test framework. There is a base class that starts the thread with a virtual function and the user of the abstraction is supposed to override that function, kind of like this:

/* Library code: */ class Thread { public: Thread() { /* ... */ } virtual ~Thread() { stop(); } void stop() { /* join the thread if running */ } protected: virtual void singlepassThreadWork() = 0; }; /* User code: */ class MyThread : public Thread { protected: void singlepassThreadWork() override { /* do stuff with foobar */ } private: std::vector<int> foobar; };

Now, what happens when MyThread::singlepassThreadWork() uses a member variable of MyThread like foobar and we delete the MyThread object while the thread is still running? The destruction sequence is such that MyThread is deleted first and after that, the destructor of its parent object Thread runs and the thread is joined. Thus, there is a race condition: We risk accessing the vector foobar in singlepassThreadWork() after it was already deleted. We can fix the user code by explicitly stopping the thread in its destructor:

/* User code: */ class MyThread : public Thread { public: ~MyThread() { stop() } protected: void singlepassThreadWork() override { /* do stuff with foobar */ } private: std::vector<int> foobar; };

My disappointment was immeasurable. The bug was in the test framework itself, not in the code under test. Unit Tests really are worthless and this is a waste of time... Right? But then I had to think of a comic that I had found on the internet and I had actually printed out this comic and put it on the wall in our office:

Even though this comic is older than I am, from a long bygone hacker era, it resonated with me a lot because the wisdom within it still holds true. Whenever you encounter a bug, ask yourself the following three questions:

  1. Have I made this error anywhere else?
  2. What happens when I fix the bug?
  3. How can I change my ways to make this bug impossible?

It seems so simple, but it's a really powerful methodology to prevent further errors and raise code quality. With this comic in mind, I asked myself whether this error existed anywhere else in the code - and boy did I find a boatload of instances of this race condition. It was everywhere. A co-worker and I combed through the entire code base and fixed this type of error in a few large commits, adding a stop() call in the destructor of the class that was the lowest in the inheritance hierarchy for each thread. Furthermore, we made all the developers aware of this pitfall and kept an eye on new code that could be affected. We never observed this bug again in the years since. In addition, we were now aware that this inheritance-based abstraction is defective by design, since most of its uses suffer from a race condition that has to be mitigated manually by the programmer. Designs of new abstractions would not be subject to such pitfalls as we encouraged developers to use composition and dependency injection over inheritance, which raised the overall code quality significantly.

We never found out why the race condition suddenly started causing crashes after a year's worth of successful runs. As mentioned, none of the involved code was changed. As far as I know, the operating system on the test system was not modified during that time period. The crashes must have been caused by subtle differences in how the threads were scheduled. Perhaps the addition of unrelated tests made the Unit Test binary larger and caused side-effects regarding CPU caches and timings? We'll never know.

The day I found this race condition thanks to Unit Tests was the day I truly started to believe in their value. I kept expanding the test project and even achieved 100% test coverage of a crucial library we depended on, which later prevented disaster when a code modification introduced a subtle but critical bug that was caught by the test suite. The effort spent on Unit Tests is worth it.

This event also taught me not to reject concepts and development methodologies based on half-knowledge and prejudice. When in doubt, just try it. What's the worst that could happen? You wasted a bit of time. On the other hand, the potential upside is that you added a useful tool to your toolbox for the rest of your life.

Post comment

CAPTCHA
* required field

Comments

Riyachadha wrote on 22 February 2024
Your article is fascinating. , you have enriched your craft delightfully. It rouses individuals a ton. If the subject is acceptable, at that point it is likewise enjoyable to peruse and this is the thing that individuals need these days, your post is truly intriguing.
https://www.thebangaloreescort.com/location/kempegowda-nagar.html
https://www.thebangaloreescort.com/location/kamala-nagar.html
https://www.thebangaloreescort.com/location/richmond-town.html
https://www.thebangaloreescort.com/location/chikkalasandra.html
https://www.thebangaloreescort.com/location/kalena-agrahara.html
https://www.thebangaloreescort.com/location/bidadi.html
https://www.thebangaloreescort.com/location/kasavanahalli.html
https://www.thebangaloreescort.com/location/varthur.html
https://www.thebangaloreescort.com/location/hombegowda-nagar.html
https://www.thebangaloreescort.com/location/dommasandra.html
reply

Andheri Call Girls wrote on 28 February 2024
I like to read your blog. You shared a wondeful information about

reply

Escorts Services In Mumbai wrote on 28 February 2024
I am very impressed with your writing I couldn't think of this, but it's amazing! I wrote several posts similar to this one, but please come and see!

reply

Mumbai Female Escorts wrote on 28 February 2024
You seem to be very professional in the way you write.

reply

Mumbai Escorts Services wrote on 28 February 2024
You made some decent points there. I looked on the web for the problem and discovered most people may go together with with the web site.

reply

Escorts In Mumbai wrote on 28 February 2024
Anjali Ahuja is offering sexy Call Girls in Mumbai Service at Low rates. We also provides top Independent Escorts for all over Mumbai.
reply

Navi Mumbai Call Girls wrote on 28 February 2024
I can’t really help but admire your blog, your blog is so adorable and nice ,

reply

Vashi Call Girls wrote on 28 February 2024
Great job, beautiful site with great informational content. This is a really interesting and informative post.

reply

Mumbai Escorts Agency wrote on 28 February 2024
Hi, I just ran across your weblog via Bing. Your viewpoint is truly pertinent to my life right now, and I’m really pleased I discovered your website.

reply

Mumbai Escorts Services wrote on 28 February 2024
I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!..

reply

Mumbai Escorts wrote on 28 February 2024
I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up..

reply

Pooja Goyal wrote on 28 February 2024
I like to read your blog. You shared a wondeful information about
https://www.poojagoyal.com/
https://www.poojagoyal.com/gallery.html
https://www.poojagoyal.com/about.html
https://www.poojagoyal.com/services.html
https://www.poojagoyal.com/rates.html
https://www.poojagoyal.com/contact.html
https://www.poojagoyal.com/andheri-escorts.html
https://www.poojagoyal.com/ambernath-escorts.html
https://www.poojagoyal.com/airoli-escorts.html
https://www.poojagoyal.com/bandra-escorts.html
https://www.poojagoyal.com/bhayander-escorts.html
https://www.poojagoyal.com/bhiwandi-escorts.html
https://www.poojagoyal.com/bhoiwada-escorts.html
https://www.poojagoyal.com/boisar-escorts.html
https://www.poojagoyal.com/borivali-escorts.html
https://www.poojagoyal.com/byculla-escorts.html
https://www.poojagoyal.com/charni-road-escorts.html
https://www.poojagoyal.com/chembur-escorts.html
https://www.poojagoyal.com/chinchpokli-escorts.html
https://www.poojagoyal.com/churchgate-escorts.html
https://www.poojagoyal.com/colaba-escorts.html
https://www.poojagoyal.com/dadar-escorts.html
https://www.poojagoyal.com/dahisar-escorts.html
https://www.poojagoyal.com/diva-escorts.html
https://www.poojagoyal.com/dombivali-escorts.html
https://www.poojagoyal.com/dongri-escorts.html
https://www.poojagoyal.com/govandi-escorts.html
https://www.poojagoyal.com/ghansoli-escorts.html
https://www.poojagoyal.com/ghatkopar-escorts.html
https://www.poojagoyal.com/goregaon-escorts.html
https://www.poojagoyal.com/juhu-escorts.html
https://www.poojagoyal.com/jogeshwari-escorts.html
https://www.poojagoyal.com/kalbadevi-escorts.html
https://www.poojagoyal.com/kalwa-escorts.html
https://www.poojagoyal.com/kandivali-escorts.html
https://www.poojagoyal.com/kanjurmarg-escorts.html
https://www.poojagoyal.com/karjat-escorts.html
https://www.poojagoyal.com/kasara-escorts.html
https://www.poojagoyal.com/khar-road-escorts.html
https://www.poojagoyal.com/khandeshwar-escorts.html
https://www.poojagoyal.com/khopoli-escorts.html
https://www.poojagoyal.com/kopar-khairane-escorts.html
https://www.poojagoyal.com/kurla-escorts.html
https://www.poojagoyal.com/lokhandwala-escorts.html
https://www.poojagoyal.com/malad-escorts.html
https://www.poojagoyal.com/marine-lines-escorts.html
https://www.poojagoyal.com/mira-road-escorts.html
https://www.poojagoyal.com/mansarovar-escorts.html
https://www.poojagoyal.com/mulund-escorts.html
https://www.poojagoyal.com/mumbai-airport-escorts.html
https://www.poojagoyal.com/mumbai-call-girls.html
https://www.poojagoyal.com/mumbai-hotel-escorts.html
https://www.poojagoyal.com/mumbai-russian-escorts.html
https://www.poojagoyal.com/nalasopara-escorts.html
https://www.poojagoyal.com/naigaon-escorts.html
https://www.poojagoyal.com/navi-mumbai-escorts.html
https://www.poojagoyal.com/nariman-point-escorts.html
https://www.poojagoyal.com/oshiwara-escorts.html
https://www.poojagoyal.com/palghar-escorts.html
https://www.poojagoyal.com/panvel-escorts.html
https://www.poojagoyal.com/powai-escorts.html
https://www.poojagoyal.com/ranchi-escorts.html
https://www.poojagoyal.com/sakinaka-escorts.html
https://www.poojagoyal.com/santa-cruz-escorts.html
https://www.poojagoyal.com/sion-escorts.html
https://www.poojagoyal.com/surat-escorts.html
https://www.poojagoyal.com/thane-escorts.html
https://www.poojagoyal.com/titwala-escorts.html
https://www.poojagoyal.com/tilak-nagar-escorts.html
https://www.poojagoyal.com/vasai-escorts.html
https://www.poojagoyal.com/vashi-escorts.html
https://www.poojagoyal.com/versova-escorts.html
https://www.poojagoyal.com/vikhroli-escorts.html
https://www.poojagoyal.com/vile-parle-escorts.html
https://www.poojagoyal.com/virar-escorts.html
https://www.poojagoyal.com/worli-escorts.html
https://www.poojagoyal.com/index.html
Really informative and inoperative, Thanks for the post and effort! Please keep sharing more such blog.
https://www.pallawi.com/
https://www.poojanehwal.com/
https://www.niyatikaur.com/
https://www.poojagoyal.com/
https://www.seona.in/
https://www.arpitagoyal.com/
https://www.anjali-ahuja.com/
https://www.prity.in/
https://www.payalmehta.com/
https://www.rimpimalhotra.com/
https://www.poorbigupta.com/


reply

Pune Escorts wrote on 26 February 2024
If you want to reduce your stress a bit then look for Pune escorts service. We organize night parties and erotic services for entertainment. If you really want to enjoy our service then visit our website.
https://www.nehatondon.in

reply

Riyachadha wrote on 22 February 2024
Escorts surpass limits, For boys who are hungry from inside their bodies to make love with girls. Love beautiful girls that make you feel increased Stimulating thirst.
https://www.nightangels.in/escorts/rewari
reply

Stable wrote on 31 December 2023
Just read your post about the unit testing epiphany. It really illustrates the importance of a well-placed stop() call in a class destructor. It’s a classic example of those small details that can make or break a project. Happy new Year! ;-)
reply

Tom Van Vleck wrote on 21 December 2023
Nice story. I wrote https://multicians.org/thvv/threeq.html in 1989 and it was published in ACM SIGSOFT Software Engineering Notes (before the WWW).

reply

D-Coder wrote on 20 December 2023
This article made the news! Hacker News: https://news.ycombinator.com/item?id=38695938
reply

yashchester wrote on 19 December 2023
Love this comments section and great article! Woop woop
reply

Christopher Smith wrote on 19 December 2023
Was there a change in the toolchain, especially the compiler itself, that might have surfaced this bit of "personality"?
reply

Steven wrote on 19 December 2023
Hello I'm Steven I enjoy programming
reply

Bobby wrote on 19 December 2023
Wow I right test now on dev program. I have a persanal project check it out
reply