Skip to content

Tie the Revit connection to the process and apply the parallel limit - #109

Draft
sweco-sealqg wants to merge 1 commit into
Nice3point:mainfrom
sweco-sealqg:fix/process-lifetime-revit-thread
Draft

Tie the Revit connection to the process and apply the parallel limit#109
sweco-sealqg wants to merge 1 commit into
Nice3point:mainfrom
sweco-sealqg:fix/process-lifetime-revit-thread

Conversation

@sweco-sealqg

Copy link
Copy Markdown

Resolves #108.

What changes

Connection lifetime. InitializeRevitConnection injects Revit the first time it is called in the process and is a no-op afterwards, so any number of test sessions share one connection. RevitSessionCleanup no longer runs as a hook; it stays callable and is marked obsolete. The new RevitApplicationLifetime (ITestHostApplicationLifetime) ejects on the Revit thread from AfterRunAsync, which both ConsoleTestHost and ServerTestHost call after the last session and before runtime shutdown. It is wired into the consumer's generated entry point by build/Nice3point.TUnit.Revit.props through a TestingPlatformBuilderHook item, the mechanism TUnit.Engine uses for its own hook, so consuming projects need no change. The test project imports the same props because a project reference does not deliver them.

Why not eject on the last After(TestSession): there is no way to know it is the last one in a server-mode host. Why not AppDomain.ProcessExit: the runtime's shutdown flag is already set, so the WPF dispatcher aborts anything queued to the Revit thread. Ejecting from another thread throws and leaves a one-thread zombie; skipping the eject makes the native teardown stall for minutes.

Parallel limit. RevitApplicationTest carries [ParallelLimiter<RevitCountParallelLimit>], which TUnit's generator copies onto every derived test class. RevitCountParallelLimit becomes public for that. RevitThreadExecutor.OnTestRegistered stays as is, with a remark that TUnit does not forward the event.

Tests

  • ProcessLifetimeTests.RevitSessionSetup_SecondSessionInSameProcess_ReusesConnection runs the session hook a second time in the same process and expects the same application back.
  • ProcessLifetimeTests.Limiter_RevitTest_RunsOneAtATime expects TestContext.Current.Parallelism.Limiter to be RevitCountParallelLimit with limit 1.

Verification

The changes come from a consumer suite (37 Revit database tests on the published 2026.1.1 package) where the same two fixes are applied on top of the package. With them the suite passes twice in one server-mode host, passes standalone with default parallelism, and the host exits with code 0 in both cases; before, standalone only passed with --maximum-parallel-tests 1 and every Test Explorer run after the first failed.

I could not run this repository's own test project: Nice3point.Revit.Injector is only on the Nice3point GitHub Packages feed, which I cannot access. The source and test projects compile for Debug.R26 (net8.0-windows) and Debug.R24 (net48) against a stub of the injector's public surface, and the generated code was checked for the builder hook call in both targets and the inherited limiter attribute on the test classes. The two new tests have not been executed here; please run the suite before merging.

Docs: README (Visual Studio section), CHANGELOG (Unreleased), AGENTS.md (the inject/eject rule now describes the process-level pairing).

… limit

Revit's core cannot be loaded into a process twice, but a host such as
Visual Studio Test Explorer in testing platform server mode starts a new
test session per run in one process, so every run after the first failed
in RevitSessionSetup with the injector's catch-all message
"Attempted to write protected memory".

- InitializeRevitConnection injects once per process; later sessions
  reuse the connection. RevitSessionCleanup no longer runs as a hook and
  is obsolete. RevitApplicationLifetime, an ITestHostApplicationLifetime
  registered through build/Nice3point.TUnit.Revit.props, ejects on the
  Revit thread when the test application finishes: the last point where
  the dispatcher still accepts work in both the console and server hosts.
- RevitApplicationTest carries ParallelLimiter<RevitCountParallelLimit>.
  TUnit's TestExecutorAttribute does not forward OnTestRegistered to the
  executor, so the executor's own limit was never applied and tests
  interleaved on the Revit thread.

Tests cover a second session setup in the same process and the limiter.
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.

"Attempted to write protected memory" in RevitSessionSetup: Test Explorer re-injects per session, and the Revit parallel limit is never applied

1 participant