Skip to content

fix(benchmarks): make SetupTeardownTests JSON tests Native AOT compatible#6434

Merged
thomhurst merged 1 commit into
mainfrom
fix/benchmark-setupteardown-aot-json
Jul 16, 2026
Merged

fix(benchmarks): make SetupTeardownTests JSON tests Native AOT compatible#6434
thomhurst merged 1 commit into
mainfrom
fix/benchmark-setupteardown-aot-json

Conversation

@thomhurst

Copy link
Copy Markdown
Owner

Problem

The README benchmark table shows for TUnit (AOT) on the Setup/teardown lifecycle row. The AOT benchmark run for SetupTeardownTests crashes:

  • JsonOperationTest / AsyncJsonOperationTest serialize an anonymous type with System.Text.Json.JsonSerializer.Serialize(data).
  • PublishAot=true sets JsonSerializerIsReflectionEnabledByDefault=false, so reflection-based STJ throws InvalidOperationException at runtime.
  • 2 failing tests → non-zero exit → CliWrap ExecuteBufferedAsync throws in RuntimeBenchmarks.TUnit_AOT → BenchmarkDotNet records NA → README renders .

This is a benchmark-suite bug, not a TUnit hooks bug — [Before(Test)]/[After(Test)] work fine under AOT.

Fix

Replace the anonymous type with a concrete JsonPayload record serialized via a source-generated JsonSerializerContext. Works under Native AOT and compiles for all four framework variants (shared file).

Verification

No MSVC linker on this machine, so full AOT link wasn''t possible locally. Instead, reproduced the exact AOT failure mode by building the TUnit variant with -p:JsonSerializerIsReflectionEnabledByDefault=false (the same feature switch PublishAot flips) and running --treenode-filter "/*/*/SetupTeardownTests/*":

  • Confirmed "System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault": false in runtimeconfig.json
  • 22/22 tests pass

Also compile-checked XUNIT3, NUNIT, MSTEST variants — all build clean.

After merge, the next weekly Speed Comparison run should fill in the missing AOT cell.

…ible

JsonOperationTest and AsyncJsonOperationTest serialized an anonymous type
via reflection-based System.Text.Json, which is disabled under PublishAot
(JsonSerializerIsReflectionEnabledByDefault=false). Both tests threw at
runtime, failing the run, so BenchmarkDotNet reported NA for TUnit AOT in
the Setup/teardown lifecycle scenario and the README showed a dash.

Replace the anonymous type with a concrete record and a source-generated
JsonSerializerContext, which works under Native AOT and all four
frameworks. Verified by running the suite with the reflection feature
switch disabled: 22/22 pass.
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 1 minor

Alerts:
⚠ 1 issue (≤ 0 issues of at least minor severity)

Results:
1 new issue

Category Results
CodeStyle 1 minor

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@thomhurst
thomhurst enabled auto-merge (squash) July 16, 2026 14:06
@claude

claude Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

Summary: This PR replaces an anonymous-type JSON payload in tools/speed-comparison/UnifiedTests/SetupTeardownTests.cs with a concrete JsonPayload record serialized via a source-generated JsonSerializerContext, fixing a Native AOT crash (reflection-based System.Text.Json is disabled under PublishAot). Verified the fix is correctly scoped (benchmark tooling, not core engine — CLAUDE.md's Dual-Mode/Snapshot Testing rules don't apply here), directly satisfies the AOT Compatible rule, compiles cleanly, and preserves the original serialization semantics (same field names/values, no behavioral regression).

@thomhurst
thomhurst disabled auto-merge July 16, 2026 14:08
@thomhurst
thomhurst merged commit 21301a6 into main Jul 16, 2026
12 of 13 checks passed
@thomhurst
thomhurst deleted the fix/benchmark-setupteardown-aot-json branch July 16, 2026 14:08
This was referenced Jul 16, 2026
This was referenced Jul 17, 2026
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.

1 participant