BlockHound can save you from waking up at midnight
Tags: java, reactive, programming
Published: 2021-11-06 14:50:30

---
title: BlockHound can save you from waking up at midnight
published: true
date: 2021-08-29 00:00:00 UTC
tags: java, reactive, programming
canonical_url: https://blog.wpanas.eu/posts/blockhound-can-save-you-from-waking-up-at-midnight/
---

## BlockHound detects problems that take down your reactive applications

It might not be that self-evident, but your non-blocking reactive application can block and eventually run out of computing power. It's simple to make that error.
 
All you have to do is use a blocking operation, and everything
falls apart like a house of cards. It can disguise itself as an undocumented method with an unintended side-effect or a misused dependency.

[BlockHound] can save you, so use it. All you need to do is to add it to your tests suite. Just like that.

```kotlin
class SuperImportantServiceTest {
    @BeforeAll
    fun setupAll() {
        BlockHound.install();
    }

    ...
}
```

For more information on adding a dependency to your project, you can check an [official getting starter guide].

## BlockHound works with Project Reactor & Kotlin coroutines

BlockHound is a tool created by [Project Reactor's] team, but you can use it with many different technologies. Here you can see examples of using BlockHound with [Reactor] & [Kotlin coroutines]. It supports [the JVM's SPI mechanism] for easy integration with any technology.

Sadly, RxJava 3 is [unsupported] at the moment. BlockHound still supports [RxJava 2].

Nonetheless, BlockHound's versatile, and there is no reason you can't use it right now. Unless you use RxJava 3 😉

## Together with Testcontainers can even detect problems with your external dependencies

Some blocking operations might hide inside dependencies that you use. Let's say that you use blocking MongoDB operations by mistake. If you only mock operations on a database, then BlockHound won't help you.

Use [Testcontainers] to solve this problem. It's a tool that spawns containers with dependencies during running tests. In doing so, you connect to an authentic MongoDB instance just for your tests. BlockHound instantly catches that you use blocking API and warns you about it.

## Summary

Putting it all together, go and use [BlockHound] immediately. It's an essential tool for every software developer that creates reactive applications. 

BlockHound is:
 - easy to use & saves your good night sleep
 - works with the most popular non-blocking JVM technologies
 - together with Testcontainers is a bulletproof solution


[Project Reactor's]: https://projectreactor.io/
[BlockHound]: https://github.com/reactor/BlockHound
[Reactor]: https://github.com/reactor/BlockHound/blob/master/example/src/test/java/com/example/ReactorTest.java
[RxJava 2]: https://github.com/reactor/BlockHound/blob/master/example/src/test/java/com/example/RxJavaTest.java
[Kotlin coroutines]: https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-debug/test/BlockHoundTest.kt
[Testcontainers]: https://www.testcontainers.org/
[official getting starter guide]: https://github.com/reactor/BlockHound#getting-it
[unsupported]: https://github.com/ReactiveX/RxJava/pull/6692
[the JVM's SPI mechanism]: https://github.com/reactor/BlockHound/blob/master/docs/custom_integrations.md