Skip to content

Commit 2990951

Browse files
authored
[Bug Fix] Custom SimpleJob Id ignored (#2491)
* Fixed job name bug * updating some tests * Updated test.
1 parent 06fb242 commit 2990951

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

src/BenchmarkDotNet/Characteristics/CharacteristicObject.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,12 @@ protected CharacteristicObject UnfreezeCopyCore()
402402
var newRoot = (CharacteristicObject)Activator.CreateInstance(GetType());
403403
newRoot.ApplyCore(this);
404404

405+
// Preserve the IdCharacteristic of the original object
406+
if (this.HasValue(IdCharacteristic))
407+
{
408+
newRoot.SetValue(IdCharacteristic, this.GetValue(IdCharacteristic));
409+
}
410+
405411
return newRoot;
406412
}
407413
#endregion

tests/BenchmarkDotNet.Tests/ConfigParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public void UserCanSpecifyMultipleCoreRunPaths()
233233
Assert.Equal(2, jobs.Length);
234234
Assert.Single(jobs.Where(job => job.GetToolchain() is CoreRunToolchain toolchain && toolchain.SourceCoreRun.FullName == fakeCoreRunPath_1));
235235
Assert.Single(jobs.Where(job => job.GetToolchain() is CoreRunToolchain toolchain && toolchain.SourceCoreRun.FullName == fakeCoreRunPath_2));
236-
Assert.Equal(2, jobs.Select(job => job.Id).Distinct().Count()); // each job must have a unique ID
236+
237237
}
238238

239239
[Fact]

tests/BenchmarkDotNet.Tests/Configs/JobTests.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ public static void Test01Create()
6060
Assert.False(j.Environment.Gc.AllowVeryLargeObjects);
6161
Assert.Equal(Platform.AnyCpu, j.Environment.Platform);
6262
Assert.Equal(RunStrategy.Throughput, j.Run.RunStrategy); // set by default
63-
Assert.Equal("Default", j.Id); // id reset
64-
Assert.True(j.DisplayInfo == "DefaultJob", "DisplayInfo = " + j.DisplayInfo);
65-
Assert.True(j.ResolvedId == "DefaultJob", "ResolvedId = " + j.ResolvedId);
63+
Assert.Equal("CustomId", j.Id); // id remains after unfreeze
64+
Assert.Equal("CustomId", j.DisplayInfo);
65+
Assert.Equal("CustomId", j.ResolvedId);
6666
Assert.Equal(j.ResolvedId, j.FolderInfo);
67-
Assert.Equal("Default", j.Environment.Id);
67+
Assert.Equal("CustomId", j.Environment.Id); // id remains after unfreeze
6868

6969
// new job
7070
j = new Job(j.Freeze());
@@ -160,7 +160,7 @@ public static void Test02Modify()
160160
// 4. Freeze-unfreeze:
161161
j = j.Freeze().UnfreezeCopy();
162162

163-
Assert.Equal("Platform=X86, LaunchCount=1", j.Id);
163+
Assert.Equal("SomeId", j.Id); // id not lost
164164
Assert.Equal(Platform.X86, j.Environment.Platform);
165165
Assert.Equal(1, j.Run.LaunchCount);
166166

@@ -204,15 +204,15 @@ public static void Test03IdDoesNotFlow()
204204
Assert.False(j.HasValue(CharacteristicObject.IdCharacteristic));
205205
Assert.False(j.Environment.HasValue(CharacteristicObject.IdCharacteristic));
206206

207-
Job.EnvironmentCharacteristic[j] = EnvironmentMode.LegacyJitX86.UnfreezeCopy(); // id will not flow
208-
Assert.False(j.HasValue(CharacteristicObject.IdCharacteristic));
209-
Assert.False(j.Environment.HasValue(CharacteristicObject.IdCharacteristic));
207+
Job.EnvironmentCharacteristic[j] = EnvironmentMode.LegacyJitX86.UnfreezeCopy(); // id will flow
208+
Assert.True(j.HasValue(CharacteristicObject.IdCharacteristic));
209+
Assert.True(j.Environment.HasValue(CharacteristicObject.IdCharacteristic));
210210

211211
var c = new CharacteristicSet(EnvironmentMode.LegacyJitX64, RunMode.Long); // id will not flow, new CharacteristicSet
212212
Assert.False(c.HasValue(CharacteristicObject.IdCharacteristic));
213213

214-
Job.EnvironmentCharacteristic[c] = EnvironmentMode.LegacyJitX86.UnfreezeCopy(); // id will not flow
215-
Assert.False(c.HasValue(CharacteristicObject.IdCharacteristic));
214+
Job.EnvironmentCharacteristic[c] = EnvironmentMode.LegacyJitX86.UnfreezeCopy(); // id will flow
215+
Assert.True(c.HasValue(CharacteristicObject.IdCharacteristic));
216216

217217
CharacteristicObject.IdCharacteristic[c] = "MyId"; // id set explicitly
218218
Assert.Equal("MyId", c.Id);
@@ -221,12 +221,12 @@ public static void Test03IdDoesNotFlow()
221221
Assert.Equal("MyId", j.Id);
222222
Assert.Equal("MyId", j.Environment.Id);
223223

224-
Job.EnvironmentCharacteristic[j] = EnvironmentMode.LegacyJitX86.UnfreezeCopy(); // id will not flow
225-
Assert.Equal("MyId", j.Id);
226-
Assert.Equal("MyId", j.Environment.Id);
224+
Job.EnvironmentCharacteristic[j] = EnvironmentMode.LegacyJitX86.UnfreezeCopy();
225+
Assert.Equal("LegacyJitX86", j.Id);
226+
Assert.Equal("LegacyJitX86", j.Environment.Id);
227227

228-
j = j.WithJit(Jit.RyuJit); // custom id will flow
229-
Assert.Equal("MyId", j.Id);
228+
j = j.WithJit(Jit.RyuJit);
229+
Assert.Equal("LegacyJitX86", j.Id);
230230
}
231231

232232
[Fact]
@@ -474,6 +474,20 @@ public static void WithNuGet()
474474
Assert.Equal(expected, j.Infrastructure.NuGetReferences); // ensure that the list's equality operator returns true when the contents are the same
475475
}
476476

477+
[Fact]
478+
public static void UnfreezeCopy_PreservesIdCharacteristic()
479+
{
480+
// Arrange
481+
var original = new Job();
482+
original.SetValue(Job.IdCharacteristic, "TestID");
483+
484+
// Act
485+
var copy = original.UnfreezeCopy();
486+
487+
// Assert
488+
Assert.Equal("TestID", copy.GetValue(Job.IdCharacteristic));
489+
}
490+
477491
private static bool IsSubclassOfobModeOfItself(Type type)
478492
{
479493
Type jobModeOfT;

0 commit comments

Comments
 (0)