Skip to content

Tests: add helper Stress attribute for stress testing #18452

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

Merged
merged 16 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions DEVGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ For example:
module TimeCritical =
```

For stress testing async code you can use a custom `FSharp.Test.StressAttribute`.
For example, applied to a single xUnit test case:
```fsharp
[<Theory; Stress(Count = 1000)>]
```
it will start it many times at the same time, and execute in parallel.




### Updating FCS surface area baselines

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,18 +422,19 @@ let ``Cancel running jobs with the same key`` () =
// detach requests from their running computations
cts.Cancel()

// Cancel the Get requests, leaving the jobs running unawaited.
for job in jobsToCancel do assertTaskCanceled job

// Start another request.
let job = cache.Get(key 11, work) |> Async.StartAsTask

// up til now the jobs should have been running unobserved
let current = eventsWhen events (received Requested)
Assert.Equal(0, current |> countOf Canceled)

// waitUntil events (countOf Canceled >> (=) 10)

waitUntil events (received Started)
waitUntil events (countOf Started >> (=) 11)

// Allow the single current request to finish.
jobCanContinue.Set() |> ignore

job.Wait()
Expand Down
8 changes: 8 additions & 0 deletions tests/FSharp.Test.Utilities/XunitHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ open OpenTelemetry.Trace
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Method, AllowMultiple = false)>]
type RunTestCasesInSequenceAttribute() = inherit Attribute()

// Helper for stress testing.
// Runs a test case many times in parallel.
// Example usage: [<Theory; Stress(Count = 1000)>]
type StressAttribute([<ParamArray>] data: obj array) =
inherit DataAttribute()
member val Count = 1 with get, set
override this.GetData _ = Seq.init this.Count (fun i -> [| yield! data; yield box i |])

#if XUNIT_EXTRAS

// To use xUnit means to customize it. The following abomination adds 2 features:
Expand Down