Browse Source

fix: Improve test stability for date/time functions

**Problem:**
- CURRENT_TIMESTAMP test had timing race condition that could cause flaky failures
- CURRENT_DATE test could fail if run exactly at midnight boundary
- Tests were too strict about timing precision without accounting for system variations

**Root Cause:**
- Test captured before/after timestamps and expected function result to be exactly between them
- No tolerance for clock precision differences, NTP adjustments, or system timing variations
- Date boundary race condition around midnight transitions

**Solution:**
 **CURRENT_TIMESTAMP test**: Added 100ms tolerance buffer to account for:
  - Clock precision differences between time.Now() calls
  - System timing variations and NTP corrections
  - Microsecond vs nanosecond precision differences

 **CURRENT_DATE test**: Enhanced to handle midnight boundary crossings:
  - Captures date before and after function call
  - Accepts either date value in case of midnight transition
  - Prevents false failures during overnight test runs

**Testing:**
- Verified with repeated test runs (5x iterations) - all pass consistently
- Full test suite passes - no regressions introduced
- Tests are now robust against timing edge cases

**Impact:**
🚀 **Eliminated flaky test failures** while maintaining function correctness validation
🔧 **Production-ready testing** that works across different system environments
 **CI/CD reliability** - tests won't fail due to timing variations
pull/7185/head
chrislu 1 month ago
parent
commit
6fcc573709
  1. 26
      weed/query/engine/datetime_functions_test.go

26
weed/query/engine/datetime_functions_test.go

@ -11,7 +11,10 @@ func TestDateTimeFunctions(t *testing.T) {
engine := NewTestSQLEngine()
t.Run("CURRENT_DATE function tests", func(t *testing.T) {
before := time.Now()
result, err := engine.CurrentDate()
after := time.Now()
if err != nil {
t.Errorf("CurrentDate failed: %v", err)
}
@ -27,10 +30,13 @@ func TestDateTimeFunctions(t *testing.T) {
return
}
// Check format (YYYY-MM-DD)
today := time.Now().Format("2006-01-02")
if stringVal.StringValue != today {
t.Errorf("Expected current date %s, got %s", today, stringVal.StringValue)
// Check format (YYYY-MM-DD) with tolerance for midnight boundary crossings
beforeDate := before.Format("2006-01-02")
afterDate := after.Format("2006-01-02")
if stringVal.StringValue != beforeDate && stringVal.StringValue != afterDate {
t.Errorf("Expected current date %s or %s (due to potential midnight boundary), got %s",
beforeDate, afterDate, stringVal.StringValue)
}
})
@ -56,9 +62,15 @@ func TestDateTimeFunctions(t *testing.T) {
timestamp := time.UnixMicro(timestampVal.TimestampValue.TimestampMicros)
// Check that timestamp is within reasonable range
if timestamp.Before(before) || timestamp.After(after) {
t.Errorf("Timestamp %v should be between %v and %v", timestamp, before, after)
// Check that timestamp is within reasonable range with small tolerance buffer
// Allow for small timing variations, clock precision differences, and NTP adjustments
tolerance := 100 * time.Millisecond
beforeWithTolerance := before.Add(-tolerance)
afterWithTolerance := after.Add(tolerance)
if timestamp.Before(beforeWithTolerance) || timestamp.After(afterWithTolerance) {
t.Errorf("Timestamp %v should be within tolerance of %v to %v (tolerance: %v)",
timestamp, before, after, tolerance)
}
})

Loading…
Cancel
Save