Skip to content

Commit 0f27b69

Browse files
perf: make TUnitMessageBus sync and fix array allocation (#4360)
* perf: make `TUnitMessageBus` sync and fix array allocation * perf: make `TUnitMessageBus` sync and fix array allocation
1 parent 1d661e3 commit 0f27b69

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

TUnit.Engine/TUnitMessageBus.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,54 @@ namespace TUnit.Engine;
1616

1717
internal class TUnitMessageBus(IExtension extension, ICommandLineOptions commandLineOptions, VerbosityService verbosityService, IServiceProvider serviceProvider, ExecuteRequestContext context) : ITUnitMessageBus, IDataProducer
1818
{
19+
private static readonly Type[] _dataTypesProduced = [typeof(TestNodeUpdateMessage), typeof(SessionFileArtifact)];
20+
1921
private readonly SessionUid _sessionSessionUid = context.Request.Session.SessionUid;
2022

2123
private bool? _isConsole;
2224
private bool IsConsole => _isConsole ??= serviceProvider.GetClientInfo().Id.Contains("console", StringComparison.InvariantCultureIgnoreCase);
2325

24-
public async ValueTask Discovered(TestContext testContext)
26+
public ValueTask Discovered(TestContext testContext)
2527
{
2628
if (testContext.IsNotDiscoverable)
2729
{
28-
return;
30+
return ValueTask.CompletedTask;
2931
}
3032

31-
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
33+
return new ValueTask(context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
3234
sessionUid: _sessionSessionUid,
3335
testNode: testContext.ToTestNode(DiscoveredTestNodeStateProperty.CachedInstance)
34-
));
36+
)));
3537
}
3638

37-
public async ValueTask InProgress(TestContext testContext)
39+
public ValueTask InProgress(TestContext testContext)
3840
{
39-
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
41+
return new ValueTask(context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
4042
sessionUid: _sessionSessionUid,
4143
testNode: testContext.ToTestNode(InProgressTestNodeStateProperty.CachedInstance)
42-
));
44+
)));
4345
}
4446

45-
public async ValueTask Passed(TestContext testContext, DateTimeOffset start)
47+
public ValueTask Passed(TestContext testContext, DateTimeOffset start)
4648
{
4749
if (!testContext.ReportResult)
4850
{
49-
return;
51+
return ValueTask.CompletedTask;
5052
}
5153

5254
var testNode = testContext.ToTestNode(PassedTestNodeStateProperty.CachedInstance);
5355

54-
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
56+
return new ValueTask(context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
5557
sessionUid: _sessionSessionUid,
5658
testNode: testNode
57-
));
59+
)));
5860
}
5961

60-
public async ValueTask Failed(TestContext testContext, Exception exception, DateTimeOffset start)
62+
public ValueTask Failed(TestContext testContext, Exception exception, DateTimeOffset start)
6163
{
6264
if (!testContext.ReportResult)
6365
{
64-
return;
66+
return ValueTask.CompletedTask;
6567
}
6668

6769
exception = SimplifyStacktrace(exception);
@@ -72,10 +74,10 @@ public async ValueTask Failed(TestContext testContext, Exception exception, Date
7274

7375
var testNode = testContext.ToTestNode(updateType);
7476

75-
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
77+
return new ValueTask(context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
7678
sessionUid: _sessionSessionUid,
7779
testNode: testNode
78-
));
80+
)));
7981
}
8082

8183
private Exception SimplifyStacktrace(Exception exception)
@@ -97,36 +99,36 @@ private Exception SimplifyStacktrace(Exception exception)
9799
return exception;
98100
}
99101

100-
public async ValueTask Skipped(TestContext testContext, string reason)
102+
public ValueTask Skipped(TestContext testContext, string reason)
101103
{
102104
var testNode = testContext.ToTestNode(new SkippedTestNodeStateProperty(reason));
103105

104-
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
106+
return new ValueTask(context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
105107
sessionUid: _sessionSessionUid,
106108
testNode: testNode
107-
));
109+
)));
108110
}
109111

110-
public async ValueTask Cancelled(TestContext testContext, DateTimeOffset start)
112+
public ValueTask Cancelled(TestContext testContext, DateTimeOffset start)
111113
{
112114
var testNode = testContext.ToTestNode(new CancelledTestNodeStateProperty());
113115

114-
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
116+
return new ValueTask(context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
115117
sessionUid: _sessionSessionUid,
116118
testNode: testNode
117-
));
119+
)));
118120
}
119121

120-
public async ValueTask SessionArtifact(Artifact artifact)
122+
public ValueTask SessionArtifact(Artifact artifact)
121123
{
122-
await context.MessageBus.PublishAsync(this,
124+
return new ValueTask(context.MessageBus.PublishAsync(this,
123125
new SessionFileArtifact(
124126
context.Request.Session.SessionUid,
125127
artifact.File,
126128
artifact.DisplayName,
127129
artifact.Description
128130
)
129-
);
131+
));
130132
}
131133

132134
private static TestNodeStateProperty GetFailureStateProperty(TestContext testContext, Exception e, TimeSpan duration)
@@ -159,5 +161,5 @@ public Task<bool> IsEnabledAsync()
159161

160162
public string Description => extension.Description;
161163

162-
public Type[] DataTypesProduced => [typeof(TestNodeUpdateMessage), typeof(SessionFileArtifact)];
164+
public Type[] DataTypesProduced => _dataTypesProduced;
163165
}

0 commit comments

Comments
 (0)