Skip to content

[duplicate-code] Duplicate Code: Repeated Linked Request-Timeout Wiring in AzureDevOpsTestResultsClient #12736

Backlog

Description

@github-actions

Analysis of recent commits to the Azure DevOps reporter, in particular 05f375894 (Azure Test Plans with MTP, #12686), which added AzureDevOpsTestResultsClient.TestPlans.cs.

Summary

Seven call sites across three partial files of AzureDevOpsTestResultsClient repeat the same request-timeout wiring: create a linked CancellationTokenSource from the user token, call CancelAfter(RequestTimeout) (or requestTimeout), then call SendCoreAsync with AttemptTimeout. Each copy must also keep the timeout source alive while the response body is read, which is the subtle part that is easy to get wrong when copying.

Duplication Details

Pattern: Linked request-timeout source wrapped around SendCoreAsync

  • Severity: Medium

  • Occurrences: 7 across 3 files

  • Locations:

    • src/MTP/Microsoft.Testing.Extensions.AzureDevOpsReport/AzureDevOpsTestResultsClient.Http.cs (lines 66-68, SendAsync<TResponse>)
    • src/MTP/Microsoft.Testing.Extensions.AzureDevOpsReport/AzureDevOpsTestResultsClient.Http.cs (lines 77-79, SendAsync)
    • src/MTP/Microsoft.Testing.Extensions.AzureDevOpsReport/AzureDevOpsTestResultsClient.Http.cs (lines 84-86, SendAsync with requestTimeout)
    • src/MTP/Microsoft.Testing.Extensions.AzureDevOpsReport/AzureDevOpsTestResultsClient.TestPlans.cs (lines 156-158, GetPagedValuesAsync)
    • src/MTP/Microsoft.Testing.Extensions.AzureDevOpsReport/AzureDevOpsTestResultsClient.TestPlans.cs (lines 186-188, SendForDocumentAsync)
    • src/MTP/Microsoft.Testing.Extensions.AzureDevOpsReport/AzureDevOpsTestResultsClient.cs (lines 120-122, PublishTestResultsWithSubResultsAsync)
    • src/MTP/Microsoft.Testing.Extensions.AzureDevOpsReport/AzureDevOpsTestResultsClient.cs (lines 147-149, UpdateTestResultsWithSubResultsAsync)
  • Code Sample (representative, from SendForDocumentAsync):

    using var requestTimeoutSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
    requestTimeoutSource.CancelAfter(RequestTimeout);
    using HttpResponseMessage response = await SendCoreAsync(request, requestTimeoutSource.Token, cancellationToken, AttemptTimeout).ConfigureAwait(false);
    return ParseDocument(await ReadAsStringAsync(response.Content, requestTimeoutSource.Token).ConfigureAwait(false));

The call sites differ in what they do with the response (parse JSON, read a page, return the parsed results), and some pass throwOnUnexpectedContentType: false. That variation is why a callback is a better fit than a helper that only returns the response.

Impact Analysis

  • Maintainability: A change to the timeout policy (for example the linked-token setup or AttemptTimeout handling) has to be applied at seven sites.
  • Bug Risk: Copying the block and disposing the timeout source before the body is read would cut off slow responses. The body read has to stay inside the source's lifetime.
  • Code Bloat: About 21 duplicated lines in total.

Refactoring Recommendations

  1. Add one helper that owns the timeout source for the whole read

    • In AzureDevOpsTestResultsClient.Http.cs, add private async Task<T> SendWithRequestTimeoutAsync<T>(HttpRequestMessage request, CancellationToken cancellationToken, TimeSpan requestTimeout, Func<HttpResponseMessage, CancellationToken, Task<T>> readResponseAsync, bool throwOnUnexpectedContentType = true). It creates the linked source, calls CancelAfter(requestTimeout), awaits SendCoreAsync, and passes the response and requestTimeoutSource.Token to the callback.
    • Keep the body read inside the callback so the token stays alive during the read.
    • Estimated effort: about 1-2 hours including tests.
    • Benefits: one place for timeout policy, and the lifetime rule is documented once.
  2. Keep the variation at the call site

    • Callers that need throwOnUnexpectedContentType: false (the results publish path) pass it through the helper rather than inlining the block.
    • Do not change AttemptTimeout versus requestTimeout semantics. The helper must take the timeout as a parameter so each site keeps its current value.

Behavior-preservation note: Callers that use SendAsync (void) discard the response, so the callback should dispose it the same way the current using HttpResponseMessage ignoredResponse does. Verify with the existing timeout and retry tests in AzureDevOpsClientBehaviorTests.cs (for example Request_UserCancellationDoesNotBecomeInternalTimeout) and AzureDevOpsTestResultsClientHandlerTests.cs.

Implementation Checklist

  • Review duplication findings
  • Add the shared timeout-scoped helper in AzureDevOpsTestResultsClient.Http.cs
  • Migrate the seven call sites, keeping each site's timeout value and throwOnUnexpectedContentType setting
  • Run Microsoft.Testing.Extensions.UnitTests Azure DevOps tests
  • Verify no behavior change in timeout, cancellation, and retry paths

Analysis Metadata

  • Analyzed Files: 3 partial client files in AzureDevOpsReport, plus recent new files in that folder for context
  • Detection Method: Semantic code analysis
  • Commit: f3a6a9e (window: last 10 commits)
  • Analysis Date: 2026-12-06

Assignee: @copilot

🤖 Automated content by GitHub Copilot. Generated by the Duplicate Code Detector workflow. · copilot · routed: haiku55 xhigh · ~33 AIC · ⌖ ~2 AIC · ⊞ 5.2K · history

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/duplicate-code-detector.md@main
  • expires on Dec 8, 2026, 4:24 AM UTC

Activity

  1. added
    type/tech-debt
    type/automation
    on Dec 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/automationtype/tech-debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions