Skip to content

Commit

Permalink
Lucene.Net.Util.RandomizedContext: Lazily-load random seed string onl…
Browse files Browse the repository at this point in the history
…y if it is required
  • Loading branch information
NightOwl888 committed Jan 17, 2025
1 parent 8bc1b38 commit ff6b991
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class RandomizedContext
private readonly Test currentTest;
private readonly Assembly currentTestAssembly;
private readonly long randomSeed;
private readonly string randomSeedAsHex;
private volatile string? randomSeedAsString;
private readonly long testSeed;

/// <summary>
Expand All @@ -71,7 +71,6 @@ public RandomizedContext(Test currentTest, Assembly currentTestAssembly, long ra
this.currentTest = currentTest ?? throw new ArgumentNullException(nameof(currentTest));
this.currentTestAssembly = currentTestAssembly ?? throw new ArgumentNullException(nameof(currentTestAssembly));
this.randomSeed = randomSeed;
this.randomSeedAsHex = SeedUtils.FormatSeed(randomSeed);
this.testSeed = testSeed;
this.randomGenerator = new ThreadLocal<Random>(() => new J2N.Randomizer(this.testSeed));
}
Expand All @@ -84,7 +83,7 @@ public RandomizedContext(Test currentTest, Assembly currentTestAssembly, long ra
/// <summary>
/// Gets the initial seed as a hexadecimal string for display/configuration purposes.
/// </summary>
public string RandomSeedAsHex => randomSeedAsHex;
public string RandomSeedAsString => randomSeedAsString ??= SeedUtils.FormatSeed(randomSeed);

/// <summary>
/// The current test for this context.
Expand Down
8 changes: 4 additions & 4 deletions src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ public virtual void TearDown()
Apply the following assembly-level attributes:
[assembly: Lucene.Net.Util.RandomSeed("{{RandomizedContext.CurrentContext.RandomSeedAsHex}}")]
[assembly: Lucene.Net.Util.RandomSeed("{{RandomizedContext.CurrentContext.RandomSeedAsString}}")]
[assembly: NUnit.Framework.SetCulture("{{Thread.CurrentThread.CurrentCulture.Name}}")]
Option 2:
Expand All @@ -929,7 +929,7 @@ public virtual void TearDown()
<RunSettings>
<TestRunParameters>
<Parameter name="tests:seed" value="{{RandomizedContext.CurrentContext.RandomSeedAsHex}}" />
<Parameter name="tests:seed" value="{{RandomizedContext.CurrentContext.RandomSeedAsString}}" />
<Parameter name="tests:culture" value="{{Thread.CurrentThread.CurrentCulture.Name}}" />
</TestRunParameters>
</RunSettings>
Expand All @@ -940,15 +940,15 @@ public virtual void TearDown()
{
"tests": {
"seed": "{{RandomizedContext.CurrentContext.RandomSeedAsHex}}",
"seed": "{{RandomizedContext.CurrentContext.RandomSeedAsString}}",
"culture": "{{Thread.CurrentThread.CurrentCulture.Name}}"
}
}
Fixture Test Values
=================
Random Seed: {{RandomizedContext.CurrentContext.RandomSeedAsHex}}
Random Seed: {{RandomizedContext.CurrentContext.RandomSeedAsString}}
Culture: {{ClassEnvRule.locale.Name}}
Time Zone: {{ClassEnvRule.timeZone.DisplayName}}
Default Codec: {{ClassEnvRule.codec.Name}} ({{ClassEnvRule.codec.GetType().Name}})
Expand Down

0 comments on commit ff6b991

Please sign in to comment.