Skip to content

Improve CronExpression tests to cover daylight saving and scheduling logic#4081

Open
ramanathan1504 wants to merge 6 commits intoapache:2.xfrom
ramanathan1504:issue-3660
Open

Improve CronExpression tests to cover daylight saving and scheduling logic#4081
ramanathan1504 wants to merge 6 commits intoapache:2.xfrom
ramanathan1504:issue-3660

Conversation

@ramanathan1504
Copy link
Copy Markdown
Contributor

@ramanathan1504 ramanathan1504 commented Mar 25, 2026

Fixes #3660

Root cause (what was going wrong)

There were three related edge cases:

  1. Strict-after boundary in getTimeAfter

    • getTimeAfter must move to the next second before evaluation.
    • Using +1ms can collapse back to the same second after millisecond truncation and return the same fire time again.
  2. Millisecond input handling in getPrevFireTime

    • On rollover/restart paths, file timestamps often include milliseconds (e.g. ...00.201).
    • Without compensating for that, getPrevFireTime could resolve to the previous day around DST fallback boundaries, which can trigger an extra rollover.
  3. Spring-forward (23-hour day) reverse-search step

    • Day-level reverse search used a fixed 24h step.
    • On 23h DST days, that can skip over a valid previous fire time.

Fix implemented

In log4j-core/src/main/java/org/apache/logging/log4j/core/util/CronExpression.java:

  • getTimeAfter(...): keep strict next-second behavior (+1000 ms).
  • getPrevFireTime(...): for inputs with millisecond fraction, shift by +999ms before delegating to getTimeBefore(...).
  • findMinIncrement(): for day-level fallback increment, use 23h minimum step to remain DST-safe on spring-forward days.

Validation

log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java now includes/contains DST cases for:

  • fallback at 2AM (Australia/Sydney)
  • fallback at midnight (America/Santiago)
  • spring-forward 23h day (Australia/Sydney)

Current result locally:

  • CronExpressionTest: 16 run, 0 failures

This addresses the “twice-a-year” DST rollover corruption path you described, including both fallback ambiguity and spring-forward short-day behavior.

@vy vy changed the title [LOG4J2-issue-3660] Add comprehensive unit tests for CronExpression daylight saving and scheduling logic Improve CronExpression tests to cover daylight saving and scheduling logic Apr 23, 2026
Copy link
Copy Markdown
Member

@vy vy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed with @ppkarwasz — changes look good. I've triggered the CI, keep an eye on it and address its failures, please. Could you also add a changelog entry referencing to both the Issue and PR IDs?

@github-project-automation github-project-automation Bot moved this to Changes requested in Log4j pull request tracker Apr 23, 2026
@github-actions
Copy link
Copy Markdown

Job Requested goals Build Tool Version Build Outcome Build Scan®
build-macos-latest clean install 3.9.8 Build Scan PUBLISHED
build-ubuntu-latest clean install 3.9.8 Build Scan PUBLISHED
build-windows-latest clean install 3.9.8 Build Scan PUBLISHED
Generated by gradle/develocity-actions

@ramanathan1504
Copy link
Copy Markdown
Contributor Author

ramanathan1504 commented Apr 23, 2026

Reviewed with @ppkarwasz — changes look good. I've triggered the CI, keep an eye on it and address its failures, please. Could you also add a changelog entry referencing to both the Issue and PR IDs?

@vy
ok

Enhance CronExpression to handle Daylight Saving Time transitions in fire time calculations
@github-project-automation github-project-automation Bot moved this from Changes requested to Approved in Log4j pull request tracker Apr 24, 2026
@github-project-automation github-project-automation Bot moved this from Approved to Changes requested in Log4j pull request tracker Apr 24, 2026
@github-project-automation github-project-automation Bot moved this from Changes requested to Approved in Log4j pull request tracker Apr 24, 2026
@vy vy enabled auto-merge (squash) April 24, 2026 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

Logfiles content lost on rotation at midnight

2 participants