Skip to content

[TT-14369] Extending tests duration_test.go #7293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

radkrawczyk
Copy link
Contributor

@radkrawczyk radkrawczyk commented Aug 11, 2025

User description

TT-14369
Summary [OAS] ReadableDuration does not handle time values that "mix" subsecond units during migration from Classic
Type Bug Bug
Status In Dev
Points N/A
Labels codilime_refined

Description

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning why it's required
  • I would like a code coverage CI quality gate exception and have explained why

PR Type

Tests


Description

Add unit tests for complex durations
Cover mixed subsecond unit formatting
Validate nanosecond and millisecond combos
Extend edge cases for normalization


Diagram Walkthrough

flowchart LR
  tests["duration_test.go"] -- "add cases" --> format["ReadableDuration.format() behavior"]
Loading

File Walkthrough

Relevant files
Tests
duration_test.go
Add edge-case tests for duration formatting                           

internal/time/duration_test.go

  • Add tests for 71m + 10ms normalization
  • Add duplicate minute case with expected 11ms
  • Add ms+ns and s+ns formatting checks
  • Extend complex duration coverage
+12/-0   

@buger
Copy link
Member

buger commented Aug 11, 2025

Knock Knock! 🔍

Just thought I'd let you know that your PR title and story title look quite different. PR titles that closely resemble the story title make it easier for reviewers to understand the context of the PR.

An easy-to-understand PR title a day makes the reviewer review away! 😛⚡️
Story Title [OAS] ReadableDuration does not handle time values that "mix" subsecond units during migration from Classic
PR Title Extending unit tests

Check out this guide to learn more about PR best-practices.

Copy link
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Conflicting Expectations

Two consecutive tests construct the exact same duration (71 minutes + 10 milliseconds) but assert two different formatted outputs, which cannot both be correct. One of these assertions likely has a typo or the intended input value is wrong.

duration = ReadableDuration(71*time.Minute + 10*time.Millisecond)
assert.Equal(t, "1m11s10ms", duration.format())

duration = ReadableDuration(71*time.Minute + 10*time.Millisecond)
assert.Equal(t, "1m11s11ms", duration.format())

Copy link
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix incorrect expected duration

The expected string appears incorrect for 71 minutes and 10 milliseconds; 71 minutes
equals 1h11m, not 1m11s. Update the expected to match the correct hour and minute
formatting to avoid a false failing test.

internal/time/duration_test.go [53-54]

 duration = ReadableDuration(71*time.Minute + 10*time.Millisecond)
-assert.Equal(t, "1m11s10ms", duration.format())
+assert.Equal(t, "1h11m10ms", duration.format())
Suggestion importance[1-10]: 8

__

Why: The test expects "1m11s10ms" for 71 minutes, but 71 minutes should format as 1h11m, indicating the expectation is likely incorrect; fixing prevents a misleading failing test.

Medium
General
Correct duplicate test expectation

This duplicated setup has a mismatched expected string and likely a typo in
milliseconds ("11ms"). Correct the expected to reflect 1 hour 11 minutes and 10
milliseconds, or remove the duplicate if unnecessary.

internal/time/duration_test.go [56-57]

 duration = ReadableDuration(71*time.Minute + 10*time.Millisecond)
-assert.Equal(t, "1m11s11ms", duration.format())
+assert.Equal(t, "1h11m10ms", duration.format())
Suggestion importance[1-10]: 7

__

Why: This appears to duplicate the previous case with an inconsistent expected string ("1m11s11ms"); correcting or removing it improves test accuracy, though impact is slightly lower due to duplication.

Medium

Copy link
Contributor

API Changes

no api changes detected

Copy link
Contributor

📦 Impact Review Snapshot

Effort Downstream Updates Compatibility Docs TL;DR
🔵⚪⚪ 🟢 📖 Test-only changes for ReadableDuration formatting that don't affect API behavior
## Impact Assessment

This PR only adds unit tests for the ReadableDuration.format() method to cover edge cases with mixed subsecond units. The implementation itself isn't changing, just test coverage is being improved. Since this is a test-only change, there's no direct impact on downstream repositories like tyk-operator, tyk-charts, portal, or tyk-sink.

The ReadableDuration type is used extensively in the OAS definitions for time-related fields across the codebase, but since the actual implementation remains unchanged, existing behavior is preserved.

## Required Updates

No downstream updates are required as this is a test-only change that doesn't modify the behavior of the ReadableDuration.format() method. The PR simply adds test cases to verify correct handling of mixed subsecond units during migration from Classic to OAS.

The PR does contain what appears to be conflicting test assertions (same input with different expected outputs), which should be addressed before merging, but this doesn't affect downstream dependencies.

## Compatibility Concerns

No compatibility concerns exist as this PR only adds tests without changing implementation. The ReadableDuration type is used in API definitions, rate limiting configurations, uptime tests, and other time-related fields in the OAS schema, but since the behavior isn't changing, all existing integrations will continue to work as before.

The PR reviewer noted conflicting test assertions that should be fixed, but these are isolated to the test file and don't affect runtime behavior.

## Summary & Recommendations
  • The PR is test-only and doesn't require any downstream repository updates
  • The conflicting test assertions noted in the PR review should be fixed before merging
  • No documentation updates are needed as this doesn't change any user-facing behavior
  • No suggestions to provide – change LGTM.

Tip: Mention me again using /dependency <request>.
Powered by Probe AI
Tyk Gateway Dependency Impact Reviewer

Copy link
Contributor

🚦 Connectivity Review Snapshot

Effort Tests Security Perf TL;DR
2/5 ⚠️ 🔒 none 🟢 Test cases for ReadableDuration have incorrect expected values
## Connectivity Assessment
  • Redis Connections: No impact - this PR only adds test cases for duration formatting.
  • RPC Connections: No impact - changes are limited to unit tests only.
  • Synchronization Mechanisms: No impact - this is a test-only change for time formatting.
## Test Coverage Validation
  • Redis Tests: Not applicable - this PR focuses on ReadableDuration formatting.
  • RPC Tests: Not applicable - no RPC functionality is being tested.
  • Failure Scenario Tests: The PR attempts to add test cases for mixed subsecond units, but contains errors in the expected output values. The first two test cases incorrectly expect "1m11s10ms" and "1m11s11ms" for 71 minutes + 10ms, when it should be "1h11m10ms" based on the existing implementation.
## Security & Performance Impact
  • Authentication Changes: None - this is a test-only change.
  • Performance Considerations: None - no runtime code is modified.
  • Error Handling: The PR introduces inconsistent test expectations that would fail with the current implementation. The same input (71min+10ms) is tested twice with different expected outputs.
## Summary & Recommendations
  • The PR adds test cases for ReadableDuration's handling of mixed subsecond units, but contains incorrect expectations.
  • The first two test cases should be corrected to expect "1h11m10ms" instead of "1m11s10ms" and "1m11s11ms".
  • The duplicate test case with the same input but different expected output should be removed or modified to test a different scenario.
  • The last two test cases for millisecond+nanosecond and second+nanosecond combinations look correct.

Tip: Mention me again using /connectivity <request>.
Powered by Probe AI
Connectivity Issues Reviewer Prompt for Tyk Gateway

Copy link
Contributor

Performance Impact Review: Extending Unit Tests for ReadableDuration

🚀 Performance Snapshot

Effort Perf Risk Hot Paths Benchmarks TL;DR
Low 🟢 Test-only changes with no performance impact
## Performance Impact Analysis

This PR only adds unit tests to the internal/time/duration_test.go file without modifying any implementation code. Since it's purely extending test coverage for the ReadableDuration.format() method, there is no performance impact on runtime code.

## Critical Areas

The ReadableDuration type is used throughout the codebase for representing human-readable time durations in various configurations:

  • API rate limiting
  • Webhook event cool-down periods
  • Custom key lifetimes
  • Traffic log retention
  • Uptime test configurations

However, since this PR only adds tests without changing implementation, no critical paths are affected.

## Optimization Recommendations

Based on the PR comments, there appears to be an issue with the test expectations. Two consecutive tests construct the same duration (71 minutes + 10 milliseconds) but assert different formatted outputs:

  1. One test expects "1m11s10ms"
  2. Another test expects "1m11s11ms"

Both expectations are incorrect. The correct format for 71 minutes + 10 milliseconds should be "1h11m10ms" because:

  • 71 minutes normalizes to 1 hour and 11 minutes
  • The format method processes units in descending order (hours → minutes → seconds → milliseconds)

The tests should be corrected to use the proper expected output.

## Summary
  • No performance impact as this PR only adds test coverage
  • The PR addresses an important edge case for duration formatting with mixed subsecond units
  • The test expectations need correction to properly validate the behavior
  • No suggestions to provide regarding performance - change LGTM from a performance perspective

Tip: Mention me again using /performance <request>.
Powered by Probe AI
Performance Impact Reviewer Prompt

Copy link
Contributor

🛡️ Security Snapshot

Effort Risk Level Tests Compliance TL;DR
Low 🟢 ✔️ Test-only changes with no security impact
## Security Impact Analysis

This PR adds test cases for the ReadableDuration.format() function to validate handling of mixed subsecond units during migration from Classic to OAS. The changes are test-only and do not modify any production code. The tests specifically target edge cases for duration formatting, including combinations of minutes with milliseconds, and mixed subsecond units (milliseconds with nanoseconds, seconds with nanoseconds).

## Identified Vulnerabilities

No security vulnerabilities were identified in this PR. The changes are limited to test code and do not modify any production functionality.

However, there are inconsistencies in the test cases that should be addressed:

  1. Two identical test cases for 71*time.Minute + 10*time.Millisecond expect different outputs ("1m11s10ms" vs "1m11s11ms")
  2. The test cases for 1*time.Millisecond + 1*time.Nanosecond and 1*time.Second + 1*time.Nanosecond expect outputs that don't match the current implementation (which would include "0us" for zero microseconds)
## Security Recommendations

While there are no security issues, the test inconsistencies should be fixed:

  1. Resolve the conflicting expectations for the same input (71m + 10ms)
  2. Ensure test expectations match the actual implementation behavior for subsecond unit formatting
  3. Consider adding a comment explaining the expected normalization behavior for complex durations
## OWASP Compliance

This PR has no impact on OWASP compliance as it only adds test cases. The ReadableDuration type is used in various parts of the API Gateway including rate limiting, webhooks, and uptime tests, but this change doesn't modify its behavior or introduce any security risks related to OWASP Top 10 categories.

## Summary
  • This PR only adds test cases for the ReadableDuration.format() function
  • The tests focus on edge cases for mixed subsecond unit formatting
  • There are inconsistencies in the test expectations that should be fixed
  • No security issues identified – change LGTM

Tip: Mention me again using /security <request>.
Powered by Probe AI
Security Impact Reviewer Prompt

@radkrawczyk radkrawczyk force-pushed the TT-14369-OAS-ReadableDuration-does-not-handle-time-values-that-mix-subsecond-units-during-migration-from-Classic-tests branch from aa562ae to 2df04b5 Compare August 11, 2025 11:33
Copy link
Contributor

Analysis of PR #7293: Extending Unit Tests for ReadableDuration

This PR adds unit tests for the ReadableDuration.format() method to cover edge cases with mixed subsecond units. The tests focus on validating the correct formatting of complex durations, particularly when combining different time units.

Test Cases Added

The PR adds three new test cases:

  1. 71*time.Second + 10*time.Millisecond → expected: "1m11s10ms"
  2. 1*time.Millisecond + 1*time.Nanosecond → expected: "1ms1ns"
  3. 1*time.Second + 1*time.Nanosecond → expected: "1s1ns"

Issues Identified

There's a discrepancy between the PR description, the actual code changes, and the review comments:

  1. The PR description mentions "Add tests for 71m + 10ms normalization" (suggesting 71 minutes), but the actual test uses 71 seconds.

  2. The PR description also mentions "Add duplicate minute case with expected 11ms", but no such test appears in the diff.

  3. A review comment mentions two conflicting test cases with 71 minutes that assert different outputs ("1m11s10ms" vs "1m11s11ms"), but these don't match what's in the actual diff.

Correct Expectations

Based on the format() implementation, which processes durations in descending order (hours, minutes, seconds, etc.):

  1. For 71 seconds + 10ms: The correct format is "1m11s10ms" (as in the diff)
  2. For 71 minutes + 10ms: The correct format would be "1h11m10ms" (not "1m11s10ms" or "1m11s11ms")

Recommendation

The test cases in the diff are correctly implemented, but the PR description and review comments contain inaccuracies. Before merging, the PR description should be updated to accurately reflect the actual test cases being added, and any confusion about 71 minutes vs. 71 seconds should be clarified.


Tip: Mention me again using /connectivity <request>.
Powered by Probe AI
Connectivity Issues Reviewer Prompt for Tyk Gateway

Copy link
Contributor

Analysis of PR #7293: Extending Unit Tests for ReadableDuration

This PR adds test cases for the ReadableDuration.format() method to ensure it correctly handles mixed subsecond units during migration from Classic to OAS (Open API Specification).

Key Findings

  1. Test Case Discrepancies: The PR has inconsistencies between the raw diff and the current state of the file:

    • The raw diff shows 71*time.Second + 10*time.Millisecond with expected output "1m11s10ms"
    • The current file shows 71*time.Minute + 10*time.Millisecond with expected output "1h11m10ms" (repeated twice)
  2. Implementation Analysis: The format() method correctly breaks down durations into components (hours, minutes, seconds, milliseconds, microseconds, nanoseconds) and formats them in descending order.

  3. Bug Context: The JIRA ticket (TT-14369) indicates a bug where ReadableDuration doesn't properly handle mixed subsecond units during migration from Classic to OAS.

Recommendations

  1. Fix Duplicate Test Case: Remove the duplicate test for 71*time.Minute + 10*time.Millisecond.

  2. Correct Test Expectations: Ensure test expectations match the actual implementation behavior:

    • For 71*time.Second + 10*time.Millisecond, the correct output is "1m11s10ms"
    • For 71*time.Minute + 10*time.Millisecond, the correct output is "1h11m10ms"
  3. Add Comment: Consider adding a comment explaining the purpose of these tests in relation to the bug being fixed.

The PR correctly adds test coverage for mixed subsecond units (milliseconds + nanoseconds, seconds + nanoseconds), which is valuable for ensuring proper formatting during OAS migration.


Tip: Mention me again using /security <request>.
Powered by Probe AI
Security Impact Reviewer Prompt

Copy link
Contributor

PR Review: Extending Unit Tests for ReadableDuration

Summary

This PR adds unit tests for the ReadableDuration.format() method to cover edge cases with mixed subsecond units. The tests are valuable for ensuring correct behavior during migration from Classic to OAS, but there are issues with some of the test expectations that need to be addressed.

Issues Identified

1. Incorrect Duration Normalization

Based on the PR description and reviewer comments, the PR adds tests for 71*time.Minute + 10*time.Millisecond, but the expected outputs are incorrect:

  • One test expects "1m11s10ms"
  • Another test expects "1m11s11ms"

Both expectations are wrong. According to the ReadableDuration.format() implementation, 71 minutes should be normalized to 1 hour and 11 minutes, resulting in "1h11m10ms". The implementation processes durations in descending order (hours → minutes → seconds → milliseconds), so 71 minutes would be converted to 1 hour and 11 minutes.

2. Duplicate Test Case with Inconsistent Expectations

The PR includes two test cases with identical inputs (71*time.Minute + 10*time.Millisecond) but different expected outputs. This is logically inconsistent as the same input should always produce the same output.

3. Mixed Subsecond Unit Tests

The tests for millisecond+nanosecond and second+nanosecond combinations are valuable additions that address the core issue described in the JIRA ticket: "[OAS] ReadableDuration does not handle time values that 'mix' subsecond units during migration from Classic". These tests will help ensure that the ReadableDuration.format() method correctly handles mixed subsecond units.

Technical Analysis

The ReadableDuration.format() implementation processes duration units in descending order through the convertCases slice:

var convertCases = []convertCase{
    {time.Hour, EndingHour},
    {time.Minute, EndingMinute},
    {time.Second, EndingSecond},
    {time.Millisecond, EndingMillisecond},
    {time.Microsecond, EndingMicrosecond},
    {time.Nanosecond, EndingNanosecond},
}

For each unit, it calculates how many of that unit fit into the remaining duration, appends that to the output string, and subtracts it from the remaining duration.

For 71*time.Minute + 10*time.Millisecond:

  • First, it processes hours: 71 minutes = 1 hour + 11 minutes, so it adds "1h"
  • Then, it processes minutes: 11 minutes remain, so it adds "11m"
  • Then, it processes seconds: 0 seconds remain, so it skips
  • Then, it processes milliseconds: 10 milliseconds remain, so it adds "10ms"
  • The correct output is "1h11m10ms"

For mixed subsecond units like millisecond+nanosecond and second+nanosecond, the implementation should correctly handle these by including both units in the output.

Recommendations

  1. Fix the normalization expectations:

    • Change the expected output for 71*time.Minute + 10*time.Millisecond to "1h11m10ms"
    • Remove the duplicate test case or modify it to test a different scenario
  2. Verify other test cases:

    • Ensure the millisecond+nanosecond and second+nanosecond test cases have correct expectations based on the current implementation
  3. Add a comment:

    • Consider adding a comment explaining the normalization behavior for complex durations to make the tests more understandable

Conclusion

This PR addresses an important edge case for duration formatting with mixed subsecond units, which is valuable for ensuring correct behavior during migration from Classic to OAS. Once the test expectations are corrected, this PR will improve test coverage for the ReadableDuration type and help prevent regressions in handling mixed subsecond units.


Tip: Mention me again using /performance <request>.
Powered by Probe AI
Performance Impact Reviewer Prompt

Copy link
Contributor

Review of PR #7293: Extending Unit Tests for ReadableDuration

Summary

This PR adds test cases for the ReadableDuration.format() method to verify correct handling of mixed subsecond units during migration from Classic to OAS. The changes are test-only and don't modify any implementation code.

Test Cases Added

The PR adds four new test cases to TestReadableDuration_Format:

  1. Minute normalization with milliseconds:

    duration = ReadableDuration(71*time.Minute + 10*time.Millisecond)
    assert.Equal(t, "1h11m10ms", duration.format())
  2. Minute normalization with different milliseconds:

    duration = ReadableDuration(71*time.Minute + 11*time.Millisecond)
    assert.Equal(t, "1h11m11ms", duration.format())
  3. Millisecond with nanosecond:

    duration = ReadableDuration(1*time.Millisecond + 1*time.Nanosecond)
    assert.Equal(t, "1ms1ns", duration.format())
  4. Second with nanosecond:

    duration = ReadableDuration(1*time.Second + 1*time.Nanosecond)
    assert.Equal(t, "1s1ns", duration.format())

Technical Analysis

The test cases correctly validate the format() method's behavior:

  1. Unit normalization: The tests verify that 71 minutes is correctly normalized to 1 hour and 11 minutes, following the implementation's approach of processing units in descending order (hours → minutes → seconds → milliseconds → microseconds → nanoseconds).

  2. Mixed subsecond units: The tests confirm that combinations of different time units (ms+ns, s+ns) are formatted correctly without losing precision.

  3. Edge cases: The tests cover important edge cases where multiple units need to be represented in the formatted string, ensuring the implementation handles these correctly.

The implementation processes each unit by:

  • Calculating how many of that unit fit in the remaining duration
  • Subtracting that amount from the remaining duration
  • Adding the formatted unit to the result string if the count is non-zero

Conclusion

This PR appropriately extends test coverage for the ReadableDuration.format() method, focusing on edge cases with mixed subsecond units. The tests are well-structured and correctly validate the expected behavior of the method.

There was some confusion in the PR comments about expected outputs, but the actual test code has correct expectations that match the implementation's behavior. The PR helps ensure that the ReadableDuration type correctly handles time values that mix subsecond units during migration from Classic to OAS.

The PR is ready to merge as the test expectations are correct and properly validate the implementation.


Tip: Mention me again using /dependency <request>.
Powered by Probe AI
Tyk Gateway Dependency Impact Reviewer

@radkrawczyk radkrawczyk force-pushed the TT-14369-OAS-ReadableDuration-does-not-handle-time-values-that-mix-subsecond-units-during-migration-from-Classic-tests branch from 2df04b5 to 2e22452 Compare August 11, 2025 11:46
Copy link
Contributor

Analysis of PR #7293: Extending Unit Tests for ReadableDuration

Overview

This PR adds test cases for the ReadableDuration.format() method to verify correct handling of mixed subsecond units during migration from Classic to OAS. The changes are test-only and don't modify any implementation code.

Technical Analysis

ReadableDuration Implementation

The ReadableDuration.format() method processes duration units in descending order:

  1. Hours
  2. Minutes
  3. Seconds
  4. Milliseconds
  5. Microseconds
  6. Nanoseconds

For each unit, it calculates how many of that unit fit in the remaining duration, adds that to the result string, and subtracts it from the remaining duration.

Test Cases Being Added

Based on the PR description, the following test cases are being added:

  1. Duration normalization test: Testing that 71 minutes + 10 milliseconds is correctly formatted as "1h11m10ms". This verifies that the implementation properly normalizes 71 minutes to 1 hour and 11 minutes.

  2. Mixed subsecond units tests: Testing that combinations like 1 millisecond + 1 nanosecond and 1 second + 1 nanosecond are correctly formatted as "1ms1ns" and "1s1ns" respectively.

Potential Issues

There appears to be confusion in the PR comments about the expected output for 71 minutes + 10 milliseconds:

  • Some comments mention "1m11s10ms" or "1m11s11ms" as expected outputs
  • Based on the implementation, the correct output should be "1h11m10ms"

If the PR includes test cases with incorrect expectations, they would fail with the current implementation. The correct expectations should be:

  1. For 71 minutes + 10 milliseconds: "1h11m10ms"
  2. For 1 millisecond + 1 nanosecond: "1ms1ns"
  3. For 1 second + 1 nanosecond: "1s1ns"

Recommendations

  1. Ensure correct test expectations: The test for 71 minutes + 10 milliseconds should expect "1h11m10ms", not "1m11s10ms" or "1m11s11ms".

  2. Remove duplicate tests: If there are duplicate test cases with the same input but different expected outputs, one of them should be removed or modified to test a different scenario.

  3. Add comments: Consider adding comments explaining the normalization behavior for complex durations to make the tests more understandable.

Conclusion

This PR addresses an important edge case for duration formatting with mixed subsecond units, which is valuable for ensuring correct behavior during migration from Classic to OAS. The tests for millisecond+nanosecond and second+nanosecond combinations are particularly important as they directly address the issue described in the JIRA ticket.

However, the test expectations for the 71-minute case should be verified to ensure they match the actual implementation behavior. Once any incorrect expectations are fixed, this PR will improve test coverage for the ReadableDuration type and help prevent regressions in handling mixed subsecond units.


Tip: Mention me again using /performance <request>.
Powered by Probe AI
Performance Impact Reviewer Prompt

Copy link
Contributor

Analysis of PR #7293: Extending Unit Tests for ReadableDuration

This PR adds test cases for the ReadableDuration.format() method to verify correct handling of mixed subsecond units during migration from Classic to OAS (Open API Specification).

Test Cases Added

The PR adds four new test cases to the TestReadableDuration_Format function:

  1. Minute normalization with milliseconds:

    duration = ReadableDuration(71*time.Minute + 10*time.Millisecond)
    assert.Equal(t, "1h11m10ms", duration.format())
  2. Minute normalization with different milliseconds:

    duration = ReadableDuration(71*time.Minute + 11*time.Millisecond)
    assert.Equal(t, "1h11m11ms", duration.format())
  3. Millisecond with nanosecond (mixed subsecond units):

    duration = ReadableDuration(1*time.Millisecond + 1*time.Nanosecond)
    assert.Equal(t, "1ms1ns", duration.format())
  4. Second with nanosecond (mixed units):

    duration = ReadableDuration(1*time.Second + 1*time.Nanosecond)
    assert.Equal(t, "1s1ns", duration.format())

Technical Analysis

The test cases correctly validate the format() method's behavior:

  1. Unit normalization: The tests verify that 71 minutes is correctly normalized to 1 hour and 11 minutes, following the implementation's approach of processing units in descending order.

  2. Mixed subsecond units: The tests confirm that combinations of different time units (ms+ns, s+ns) are formatted correctly without losing precision.

  3. Edge cases: The tests cover important edge cases where multiple units need to be represented in the formatted string, ensuring the implementation handles these correctly.

Bug Context

The PR addresses the issue described in TT-14369: "[OAS] ReadableDuration does not handle time values that 'mix' subsecond units during migration from Classic". The added tests ensure that:

  1. Time values with multiple units are correctly normalized (e.g., 71 minutes → 1h11m)
  2. Mixed subsecond units (milliseconds + nanoseconds) are properly formatted
  3. Combinations of seconds and nanoseconds are handled correctly

These tests are particularly important for ensuring compatibility during migration from Classic API definitions to OAS, where duration formatting needs to be consistent and accurate.

Conclusion

This PR improves test coverage for the ReadableDuration type by adding tests for complex durations and mixed subsecond units. The tests are well-structured and correctly validate the expected behavior of the implementation. Since this is a test-only change, there's no risk of breaking existing functionality, and it helps ensure the reliability of duration formatting in the Tyk Gateway.


Tip: Mention me again using /dependency <request>.
Powered by Probe AI
Tyk Gateway Dependency Impact Reviewer

Copy link
Contributor

Analysis of PR #7293: Extending Unit Tests for ReadableDuration

This PR adds test cases for the ReadableDuration.format() method to cover edge cases with mixed subsecond units. The changes are test-only and don't modify any implementation code.

Test Cases Added

The PR adds three new test cases to the "complex duration" test run:

  1. 71*time.Second + 10*time.Millisecond → expected: "1m11s10ms"
  2. 1*time.Millisecond + 1*time.Nanosecond → expected: "1ms1ns"
  3. 1*time.Second + 1*time.Nanosecond → expected: "1s1ns"

Technical Analysis

The ReadableDuration.format() method processes duration units in descending order (hours → minutes → seconds → milliseconds → microseconds → nanoseconds). For each unit, it:

  1. Calculates how many of that unit fit into the remaining duration
  2. Subtracts that amount from the remaining duration
  3. Adds the formatted unit to the result string if the count is non-zero

The test cases verify three important behaviors:

  1. Unit normalization: 71 seconds is correctly normalized to 1 minute and 11 seconds
  2. Mixed subsecond units: Combinations of milliseconds and nanoseconds are formatted correctly
  3. Skipping zero units: When formatting "1s1ns", the method correctly skips the zero milliseconds and microseconds

Bug Context

The PR addresses a bug (TT-14369) where ReadableDuration doesn't properly handle time values that mix subsecond units during migration from Classic to OAS. The added tests ensure that the implementation correctly handles these edge cases.

Conclusion

This PR appropriately extends test coverage for the ReadableDuration.format() method, focusing on edge cases with mixed subsecond units. The tests are well-structured and correctly validate the expected behavior of the method.

The PR is ready to merge as the test expectations match the implementation's behavior and properly validate the handling of mixed subsecond units.


Tip: Mention me again using /connectivity <request>.
Powered by Probe AI
Connectivity Issues Reviewer Prompt for Tyk Gateway

Copy link
Contributor

🛡️ Security Snapshot

Effort Risk Level Tests Compliance TL;DR
Low 🟢 ✔️ Test-only changes for ReadableDuration formatting with no security impact
## Security Impact Analysis

This PR adds test cases for the ReadableDuration.format() method to validate handling of mixed subsecond units and time unit normalization. The changes are test-only and don't modify any implementation code. The ReadableDuration type is used throughout the codebase for representing human-readable time durations in various configurations like API rate limiting, webhook event cool-down periods, and uptime test configurations.

## Identified Vulnerabilities

No security vulnerabilities were identified in this PR. The changes are limited to test code and do not modify any production functionality. The tests validate that the existing implementation correctly handles mixed subsecond units (milliseconds with nanoseconds, seconds with nanoseconds) and properly normalizes time units (converting 71 seconds to 1 minute and 11 seconds).

## Security Recommendations

No security recommendations are necessary for this PR. The tests appropriately validate the behavior of the ReadableDuration.format() method for edge cases with mixed subsecond units, which is valuable for ensuring correct behavior during migration from Classic to OAS.

## OWASP Compliance

This PR has no impact on OWASP compliance as it only adds test cases. The ReadableDuration type is used in various parts of the API Gateway including rate limiting, webhooks, and uptime tests, but this change doesn't modify its behavior or introduce any security risks related to OWASP Top 10 categories.

## Summary
  • This PR only adds test cases for the ReadableDuration.format() function
  • The tests focus on edge cases for mixed subsecond unit formatting and time unit normalization
  • The changes are well-structured and correctly validate the expected behavior
  • No security issues identified – change LGTM

Tip: Mention me again using /security <request>.
Powered by Probe AI
Security Impact Reviewer Prompt

Copy link

@radkrawczyk radkrawczyk changed the title Extending unit tests [TT-14369] Extending tests duration_test.go Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants