Skip to content

V10.5.3/replace httpbin with mockerapi#165

Merged
gimlichael merged 5 commits into
mainfrom
v10.5.3/replace-httpbin-with-mockerapi
Jun 4, 2026
Merged

V10.5.3/replace httpbin with mockerapi#165
gimlichael merged 5 commits into
mainfrom
v10.5.3/replace-httpbin-with-mockerapi

Conversation

@gimlichael
Copy link
Copy Markdown
Member

This pull request refactors the TimeMeasureTest unit tests to improve code readability and maintainability. The main change is the introduction of a helper method, AssertElapsedAround, which standardizes the way elapsed time assertions are made across all test cases. Additionally, the jitter logic is updated to use separate lower and upper bounds, allowing for more flexible time assertions.

Key changes:

Test Assertion Refactoring:

  • Introduced the AssertElapsedAround helper method to replace repeated Assert.InRange logic, providing a single place to manage elapsed time assertions. This method uses LowerJitter and UpperJitter for more flexible boundaries.
  • Updated all test cases for WithAction and WithFunc (with 0 to 10 arguments) to use AssertElapsedAround instead of directly calling Assert.InRange. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21]

Asynchronous Test Updates:

  • Modified all relevant asynchronous test cases to use AssertElapsedAround for consistency with the synchronous tests. [1] [2] [3] [4] [5]

Test Configuration Improvement:

  • Replaced the single Jitter value with LowerJitter and UpperJitter to allow for asymmetric tolerance in timing assertions, making the tests more robust to timing fluctuations.

@gimlichael gimlichael self-assigned this Jun 4, 2026
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Jun 4, 2026

Greptile Summary

This PR replaces external httpbin.org HTTP calls in UriExtensionsTest with an in-process StatusCodeHttpClientFactory mock, and refactors timing assertions in TimeMeasureTest and cache-state assertions in SlimMemoryCacheTest into shared helper methods.

  • UriExtensionsTest.cs: Eliminates the external httpbin dependency by introducing an inner StatusCodeHttpClientFactory/StatusCodeHttpMessageHandler pair that returns configurable status codes in-process; adds a RequestCount assertion to verify the factory was invoked the expected number of times.
  • TimeMeasureTest.cs: Replaces repeated Assert.InRange calls with a single AssertElapsedAround helper that uses asymmetric LowerJitter (250 ms) / UpperJitter (500 ms) bounds.
  • SlimMemoryCacheTest.cs: Introduces AssertNamespaceIsLogicallyExpired, AssertNamespaceIsPhysicallyPresent, and AssertNamespaceIsPhysicallyRemoved helpers — using SpinWait.SpinUntil with a CleanupTimeout of 15 s instead of fixed Thread.Sleep — to make the two-phase cache-state assertions more resilient.

Confidence Score: 5/5

Safe to merge — all changes are confined to test code with no production logic affected.

The PR touches only test files. The httpbin replacement with an in-process mock is well-structured and thread-safe. The timing-assertion and cache-assertion refactors reduce duplication while preserving the same logical coverage. No new issues were found beyond what earlier review threads already discussed.

No files require special attention beyond the timing discussion already captured in earlier review comments on SlimMemoryCacheTest.cs.

Important Files Changed

Filename Overview
test/Cuemon.Diagnostics.Tests/TimeMeasureTest.cs Extracted AssertElapsedAround helper and split Jitter into LowerJitter (250 ms) / UpperJitter (500 ms); straightforward DRY refactor with no logic changes.
test/Cuemon.Extensions.Net.Tests/Http/UriExtensionsTest.cs Replaced httpbin.org calls with an in-process StatusCodeHttpClientFactory mock; request counting is thread-safe via Interlocked; DefaultHttpClientFactory is now set per-test rather than in the constructor.
test/Cuemon.Runtime.Caching.Tests/SlimMemoryCacheTest.cs Added three helper methods with SpinWait-based polling; AssertNamespaceIsPhysicallyPresent remains a point-in-time check with no retry, which can race with the background cleanup sweep.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Thread.Sleep - wait for expiry] --> B[AssertNamespaceIsLogicallyExpired\nSpinWait up to 15 s\n_cache.Count == 0]
    B -->|true| C[AssertNamespaceIsPhysicallyPresent\nInstantaneous snapshot\nWhere.Count == NumberOfItemsToCache]
    B -->|timeout| FAIL1[Test Fails\nLogical expiry not observed]
    C -->|pass| D[AssertNamespaceIsPhysicallyRemoved\nSpinWait up to 15 s\n!_cache.Any]
    C -->|fail| FAIL2[Test Fails\nPhysical entries unexpectedly absent\npossible race with cleanup sweep]
    D -->|true| PASS[Test Passes]
    D -->|timeout| FAIL3[Test Fails\nPhysical removal not observed]

    style FAIL1 fill:#f66,color:#fff
    style FAIL2 fill:#f66,color:#fff
    style FAIL3 fill:#f66,color:#fff
    style PASS fill:#6a6,color:#fff
Loading

Reviews (3): Last reviewed commit: "🔄 replace slimhttpclientfactory with st..." | Re-trigger Greptile

Comment thread test/Cuemon.Diagnostics.Tests/TimeMeasureTest.cs Outdated
Comment thread test/Cuemon.Runtime.Caching.Tests/SlimMemoryCacheTest.cs
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f613adad2a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


private void AssertNamespaceIsLogicallyExpired(string ns)
{
Assert.True(SpinWait.SpinUntil(() => _cache.Count(ns) == 0, CleanupTimeout),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Require proof that expiration fixtures were populated

When either expiration-verification test is run by itself, or if one of the earlier priority-ordered population tests is skipped or fails before adding entries, this predicate is already true for an empty namespace. The deleted NumberOfItemsToCache checks were the only proof that the namespace still contained the 1000 expired entries before cleanup, so these tests can now pass without exercising logical expiration or the background removal path at all.

Useful? React with 👍 / 👎.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.19%. Comparing base (36e50ca) to head (b78f625).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #165       +/-   ##
===========================================
+ Coverage   82.80%   94.19%   +11.39%     
===========================================
  Files         602      602               
  Lines       19157    19186       +29     
  Branches     2001     2009        +8     
===========================================
+ Hits        15863    18073     +2210     
+ Misses       3216     1049     -2167     
+ Partials       78       64       -14     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

aicia-bot and others added 2 commits June 4, 2026 17:15
Add assertion helper method to verify cache entries remain physically present before cleanup. Reduce jitter tolerance in TimeMeasure test to make timing assertions more deterministic.
@gimlichael gimlichael merged commit cf81e7e into main Jun 4, 2026
624 of 627 checks passed
@gimlichael gimlichael deleted the v10.5.3/replace-httpbin-with-mockerapi branch June 4, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants