Sometimes, removing a single line of code takes you much further down the rabbit hole than you initially anticipated.
While contributing to the Kubernetes Java client, I picked up what appeared to be a relatively straightforward task: removing an unnecessary Thread.sleep() from a unit test.
What followed was an investigation involving Windows, different JDK versions, native operating system APIs, clock resolution, and a little exploration of the OpenJDK source code.
It all started with Thread.sleep(100)
The Kubernetes Java client contains an event correlation mechanism responsible for aggregating Kubernetes events.
One of its tests, EventCorrelatorTest, relied on the following instruction:
Thread.sleep(100); Sleeping inside unit tests is generally something worth investigating. It introduces unnecessary delays and can hide assumptions about timing that aren't immediately obvious.
Removing the sleep and making the test deterministic seemed like a reasonable improvement.
And everything worked.
Until Windows entered the conversation.
It works on my machine™
The tests passed locally on Linux, but Windows CI exposed failures that weren't occurring on Linux or macOS.
Interestingly, the behavior also differed between JDK versions.
At this point, the problem was no longer simply about removing a sleep. Something in the test depended on the behavior of the underlying system clock.
The event aggregation logic was obtaining timestamps using:
OffsetDateTime.now() The implicit assumption was that successive calls would produce distinguishable timestamps.
But is that actually guaranteed?
How precise is your clock?
Consider the following code:
OffsetDateTime first = OffsetDateTime.now(); OffsetDateTime second = OffsetDateTime.now(); System.out.println(first); System.out.println(second); System.out.println(first.equals(second)); Depending on your operating system, JVM implementation, and the effective resolution of the system clock, both timestamps may be identical.
This brings us to an important distinction between three concepts:
- Precision: how finely a timestamp can be represented.
- Resolution: the smallest observable change in the clock's value.
- Accuracy: how closely the clock represents the actual time.
Java's OffsetDateTime supports nanosecond precision.
That doesn't mean the underlying clock provides a new timestamp every nanosecond.
A timestamp can represent nine fractional decimal digits while the clock supplying that timestamp updates at much larger intervals.
To understand what was happening, I decided to investigate how Java actually obtains the current time.
Down the OpenJDK rabbit hole
Following the implementation through OpenJDK took me across several layers:
OffsetDateTime.java ↓ Clock.java ↓ VM.java ↓ JVM native implementation ↓ os_windows.cpp ↓ Windows system clock While investigating the Windows implementations in OpenJDK 11 and 17, I encountered the following Windows API:
GetSystemTimeAsFileTime(...) Windows represents this time using a FILETIME structure, which contains a 64-bit value expressed in 100-nanosecond intervals since January 1, 1601 (UTC).
100 nanoseconds sounds quite impressive.
However, the representation of the value doesn't guarantee that the underlying clock actually updates every 100 nanoseconds.
Windows also provides another API:
GetSystemTimePreciseAsFileTime(...) This API is specifically intended to provide higher-precision system time.
The distinction is interesting because, as Java developers, we rarely need to think about what happens underneath a simple call to now().
The abstraction is convenient, but it doesn't eliminate the characteristics of the underlying operating system.
Reproducing the problem without Windows
I set up a Windows virtual machine and tested the behavior using Temurin JDK 11, 17 and 21.
Several experiments involving different timestamp increments produced interesting results.
| Timestamp increment | Observed result |
|---|---|
| 0 nanoseconds | Reproduced the failure |
| 10 nanoseconds | Intermittent failures |
| 1,000 nanoseconds | Passed five uncached test runs |
| 1 millisecond | Passed across the three tested JDK versions |
These results were useful, but increasing the interval wasn't necessarily the right solution.
More importantly, I wanted to reproduce the underlying failure without depending on Windows.
By deliberately truncating the timestamps generated by the event aggregator to seconds, I was able to reproduce the aggregation failure on Linux.
The important condition wasn't Windows itself. It was the possibility of multiple events receiving identical timestamps.
Windows had simply exposed an assumption that was already present in the code.
How does the Kubernetes Go client handle this?
Before deciding how to address the issue, I looked at the Kubernetes Go client.
One interesting difference was its use of injectable clock abstractions, including SimpleIntervalClock.
Rather than having the implementation depend directly on the system clock, the clock could be supplied externally.
This makes it possible to control time during testing without changing the behavior of the production implementation.
Could we apply a similar approach to the Java client without introducing unnecessary complexity?
Making time deterministic
Java already provides a convenient functional interface for this purpose:
Supplier<OffsetDateTime> The idea was to allow the event aggregator to receive a timestamp supplier while preserving the existing production behavior:
OffsetDateTime::now During testing, however, we could supply deterministic timestamps, advancing them explicitly:
timestamp.plusSeconds(1) No sleeping.
No dependency on the effective resolution of the operating system clock.
No assumptions about how quickly consecutive instructions execute.
The tests could now focus on verifying event aggregation rather than relying on incidental timing behavior.
The final implementation preserved the existing test assertions, and the full Bazel test suite passed, covering 113 targets.
The pull request was subsequently merged.
What about System.nanoTime()?
You might be wondering why not simply use:
System.nanoTime() Java provides this method specifically for measuring elapsed time. Unlike a wall-clock timestamp, its value isn't intended to represent a particular date or time.
However, Kubernetes events require actual timestamps.
Replacing wall-clock time with a monotonic time source wouldn't satisfy the same requirements.
Choosing the correct clock depends on what you're trying to measure:
- Use wall-clock time to establish when something happened.
- Use a monotonic clock to measure elapsed time.
- Use an injectable time source when testing behavior that depends on time.
And remember that even System.nanoTime() doesn't guarantee nanosecond resolution.
Final thoughts
What started as an attempt to remove a 100-millisecond sleep from a unit test ended up being an exploration of Java's time API, the JVM, native Windows APIs, and the way different Kubernetes clients handle time.
This is one of the things I particularly enjoy about contributing to open source.
Even a seemingly insignificant improvement can take you into parts of the software stack you wouldn't normally explore.
And sometimes the most interesting part of fixing a bug isn't the fix itself, but everything you learn along the way.

