Morning. I made coffee, settled into the chair I'd set up just right, and opened my laptop. Yesterday's task was done. Everything in TeamCity was green. I was ready to start the next one.
Then I saw the message in the team Slack channel. A colleague had spent the evening trying to get his changes through CI. The build kept failing on shouldSaveTransaction.
I'd written that test the day before. It passed during the day. It passed again that evening. I was already wondering if my code was going to break production when my boss wrote in the thread, “Run the tests before pushing to main.” I'd done exactly that. I wanted to disappear.
I ran it locally. I usually barely notice how long the project takes to compile. That morning, every module felt like another accusation.
Green. For a second I could breathe again. I checked out the exact commit that failed and ran it again. Green. I ran the whole test package in case the tests affected each other, then triggered the same TeamCity configuration by hand. By the time that one passed, I knew what I wanted to see. A red test. At least I'd have something to work with.
This is a composite story drawn from CI failures I've hit myself and similar cases I've seen since.
Jump to Compare Builds →In this story
It was a small JUnit 5 test. Move 100 from one account to another, then look for that transaction in the sender's list for today. I must have read it a hundred times that week. I kept coming back to the line that moves the money. If anything tricky was going on, I figured it had to be in there.
@Test
void shouldSaveTransaction() {
Account sender = Account.createAccountFor("sender");
Account recipient = Account.createAccountFor("recipient");
sender.transfer(recipient, 100);
assertEquals(1, sender.getTransactions(LocalDate.now()).size());
}
The test went green while I was watching
My first guess was that the query couldn't see the transaction we'd just saved. I added two Log4j messages at DEBUG. When the test failed again, I wanted to know whether transfer() had returned and what date the query had used.
private static final Logger log = LogManager.getLogger(TransactionsTest.class);
@Test
void shouldSaveTransaction() {
Account sender = Account.createAccountFor("sender");
Account recipient = Account.createAccountFor("recipient");
sender.transfer(recipient, 100);
log.debug("transferred 100: sender -> recipient");
LocalDate today = LocalDate.now();
List<Transaction> transactions = sender.getTransactions(today);
log.debug("today={} found={}", today, transactions.size());
assertEquals(1, transactions.size());
}
I wrote the test logs to a separate file and had TeamCity keep them as an artifact, somewhere I could find them in the morning without searching the entire build log.
// .teamcity/settings.kts
object Build : BuildType({
name = "Build"
artifactRules = "target/logs/test-*.log => test-logs.zip"
})
Two green nights in a row. Fine, it fixed itself. Then the admins asked me to take the logs out. This configuration ran all day, and my ZIP files were starting to eat up disk space. I turned off the logging.
Red the next morning. Damn it. Schrödinger's test. I'd open TeamCity every morning thinking I'd finally know whether it was fixed. A green build just meant I'd be back in the same spot tomorrow.
I turned logging back on and told the admins I'd clean up the artifacts myself. This time I got a failure and a log on the same night.
DEBUG transferred 100: sender -> recipient
DEBUG today=2026-08-14 found=0
So transfer() returned and the query for August 14 came back empty. I still couldn't tell whether there was a row to find. On the next run I counted all the sender's transactions before and after the transfer, with no date filter. That would tell me where to look next.
int before = sender.getAllTransactions().size();
sender.transfer(recipient, 100);
int after = sender.getAllTransactions().size();
log.debug("before={} after={}", before, after);
DEBUG before=0 after=1
DEBUG today=2026-08-14 found=0
There was my transaction. The moment I asked for August 14, it disappeared. I could finally stop worrying about whether the write had happened. I wanted to know what date was on the row and what was different about the run that made the query miss it.
Two builds, two agents
Back then I had to open the build pages and collect the agent, revision, parameters, and start time by hand. Years later, when I built Compare Builds in TeamCity, this was the kind of morning I had in mind. Here's how I'd check now, with the failed build and the nearest green one from the same configuration.
- Open the green build #250 → Actions (“…”) → Select for comparison…
- Open the failed build #249 → Actions → Add to comparison.
The header puts the start time, agent, and trigger next to the build result. In the screenshot, the two builds started 24 minutes apart. Both say main in the Branch row. In the Agent row, though, there are two different machines. I'd already checked the commit and the configuration. The machine was one thing I hadn't checked.
The suspects are in the diff
A build can have hundreds of parameters. In Parameters, I switch from Show all data to Only different. A few rows are left, including a time zone I'd never paid attention to.
teamcity.agent.jvm.user.timezone Etc/UTC → Etc/GMT+2
That parameter belongs to the agent's JVM. The JVM running the test can use a different setting, so I add one line to the test itself.
log.debug("testJvmZone={}", ZoneId.systemDefault());
I can check that during the day. I run the build on the same agent and get testJvmZone=Etc/GMT+2. The read uses the JVM's date. Where does the write get its date?
I follow transfer() into TransactionRepository.
jdbcTemplate.update(
"INSERT INTO transactions (sender, recipient, amount, created) VALUES (?, ?, ?, CURRENT_DATE)",
sender, recipient, amount);
CURRENT_DATE. That's my SQL. I'd spent days adding logs to a test while the database was picking the date all along.
Two dates for one transaction
With the database session in UTC, I can reproduce the failure with Maven. The window starts at midnight UTC and closes at 02:00, when the test JVM catches up to the same calendar date.
mvn test -Dtest=TransactionsTest#shouldSaveTransaction \
-Duser.timezone=Etc/GMT+2
After fixing it, I write a short post for our internal blog. I'd like the next person to spend less than a week on this.
Two todays
Hi everyone! Remember the transaction test that kept failing? At 00:30 UTC on August 15, the database saves the row under August 15. The test's JVM still thinks it's August 14, so that's the date it asks for.
INSERT CURRENT_DATE UTC August 15 READ LocalDate.now() UTC−2 August 14
Yes, Etc/GMT+2 means UTC−2. The sign in Etc/GMT±N is reversed. I had to read that twice, too.
I could have let the database own the date and used its date for the lookup too. Here I chose Java. I read the date once from an injected Clock and pass it to both the save and the lookup. Even if midnight comes between those calls, they agree on what “today” means.
LocalDate today = LocalDate.now(clock);
transactionRepository.save(transaction, today);
List<Transaction> transactions = sender.getTransactions(today);
In the repository, the last SQL parameter is now the date we passed in.
void save(Transaction tx, LocalDate date) {
jdbcTemplate.update(
"INSERT INTO transactions (sender, recipient, amount, created) VALUES (?, ?, ?, ?)",
tx.sender(), tx.recipient(), tx.amount(), date);
}
To test the fix, I give the code a fixed clock set to 00:30 UTC, right in the old failure window. The test can run at lunchtime.
Instant instant = Instant.parse("2026-08-15T00:30:00Z");
Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
LocalDate today = LocalDate.now(clock); // 2026-08-15
Someone replied, “We had the same thing.” A few teams checked their code and found more places where Java and the database picked the date separately. They even found tests that had been muted because nobody could explain why they failed. I'd started the week wishing the team would forget I'd written the test. Now I was proud that other teams could use what I'd found.
What else I check in Compare Builds
Only different works in the other tabs too. Which one I open depends on the question I have.
- Tests. Which tests were added, went missing, or show up as a new failure?
- Dependencies. Which upstream build supplied the artifact? If it's older than I expected, I check the dependency rules and triggers.
- Revisions. Both builds say
main. Did they actually use the same commit? - Statistics. The build got slower. Do any of the recorded metrics tell me where to start looking?
The diff has whatever TeamCity recorded about the runs. For things like a network timeout or state left on the machine by an earlier build, I go back to the logs or the agent itself.
Stop diffing logs by eye. Diff the builds.
The next morning
The daytime run passed. So did the nightly build, and then the one after that. I still wish I could send Compare Builds back to myself on the morning I read that first Slack message.
Morning. Coffee, chair, laptop. There's an unread message in Slack.
FAQ
What is a flaky test?
A test that sometimes passes and sometimes fails on the same code. Test order, shared state, random data, async timing, and time zones can all change the result between runs.
How do I compare two builds in TeamCity?
Pick two builds from the same build configuration. In the first build, open Actions and choose Select for comparison. In the second, open Actions and choose Add to comparison. Turn on Only different to see what changed. Compare Builds first appeared in TeamCity 2019.2.