Skip to content
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

Fixes 4 failing tests in .NET Framework/x86 #338

Merged
merged 4 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/Lucene.Net.Sandbox/Queries/FuzzyLikeThisQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using JCG = J2N.Collections.Generic;

namespace Lucene.Net.Sandbox.Queries
Expand Down Expand Up @@ -363,6 +364,9 @@ public ScoreTermQueue(int size)
/// (non-Javadoc)
/// <see cref="Util.PriorityQueue{T}.LessThan(T, T)"/>
/// </summary>
#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing score equality fails in x86 on .NET Framework with optimizations enabled
#endif
protected internal override bool LessThan(ScoreTerm termA, ScoreTerm termB)
{
if (termA.Score == termB.Score)
Expand Down
4 changes: 4 additions & 0 deletions src/Lucene.Net.Sandbox/Queries/SlowFuzzyTermsEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Lucene.Net.Util;
using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace Lucene.Net.Sandbox.Queries
{
Expand Down Expand Up @@ -120,6 +121,9 @@ public LinearFuzzyTermsEnum(SlowFuzzyTermsEnum outerInstance)
/// where distance is the Levenshtein distance for the two words.
/// </para>
/// </summary>
#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled. Fixes TestTokenLengthOpt.
#endif
protected override sealed AcceptStatus Accept(BytesRef term)
{
if (StringHelper.StartsWith(term, prefixBytesRef))
Expand Down
3 changes: 1 addition & 2 deletions src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,8 +1269,7 @@ public static Type GetTestClass()
return testClass;

// 2nd attempt - try scanning the referenced assemblies to see if we can find the class by fullname
var referencedAssemblies = AssemblyUtils.GetReferencedAssemblies();
testClass = referencedAssemblies.SelectMany(a => a.GetTypes().Where(t => t.FullName == testClassName)).FirstOrDefault();
testClass = AssemblyUtils.GetReferencedAssemblies().SelectMany(a => a.GetTypes().Where(t => t.FullName == testClassName)).FirstOrDefault();
if (testClass != null)
return testClass;
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/Lucene.Net/Search/FuzzyTermsEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using JCG = J2N.Collections.Generic;

namespace Lucene.Net.Search
Expand Down Expand Up @@ -381,6 +382,9 @@ public AutomatonFuzzyTermsEnum(FuzzyTermsEnum outerInstance, TermsEnum tenum, Co

/// <summary>
/// Finds the smallest Lev(n) DFA that accepts the term. </summary>
#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled, and is causing the TestTokenLengthOpt test to fail
#endif
protected override AcceptStatus Accept(BytesRef term)
{
//System.out.println("AFTE.accept term=" + term);
Expand Down
15 changes: 14 additions & 1 deletion src/Lucene.Net/Search/TopScoreDocCollector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using System;
using System.Runtime.CompilerServices;

namespace Lucene.Net.Search
{
Expand Down Expand Up @@ -46,6 +47,9 @@ internal InOrderTopScoreDocCollector(int numHits)
{
}

#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled
#endif
public override void Collect(int doc)
{
float score = scorer.GetScore();
Expand Down Expand Up @@ -89,6 +93,9 @@ internal InOrderPagingScoreDocCollector(ScoreDoc after, int numHits)
this.after = after;
}

#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled
#endif
public override void Collect(int doc)
{
float score = scorer.GetScore();
Expand Down Expand Up @@ -146,6 +153,9 @@ internal OutOfOrderTopScoreDocCollector(int numHits)
{
}

#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled
#endif
public override void Collect(int doc)
{
float score = scorer.GetScore();
Expand Down Expand Up @@ -189,6 +199,9 @@ internal OutOfOrderPagingScoreDocCollector(ScoreDoc after, int numHits)
this.after = after;
}

#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled
#endif
public override void Collect(int doc)
{
float score = scorer.GetScore();
Expand Down Expand Up @@ -302,7 +315,7 @@ protected override TopDocs NewTopDocs(ScoreDoc[] results, int start)
// it means the largest element is already in results, use its score as
// maxScore. Otherwise pop everything else, until the largest element is
// extracted and use its score as maxScore.
float maxScore = float.NaN;
float maxScore/* = float.NaN*/; // LUCENENET: Removed unnecessary assignment
if (start == 0)
{
maxScore = results[0].Score;
Expand Down
8 changes: 4 additions & 4 deletions src/Lucene.Net/Support/AssemblyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal class AssemblyUtils
/// Microsoft assemblies here.
/// </summary>
/// <returns></returns>
public static IList<Assembly> GetReferencedAssemblies()
public static IEnumerable<Assembly> GetReferencedAssemblies()
{
// .NET Port Hack: We do a 2-level deep check here because if the assembly you're
// hoping would be loaded hasn't been loaded yet into the app domain,
Expand All @@ -55,9 +55,9 @@ public static IList<Assembly> GetReferencedAssemblies()
.Distinct();
var assembliesLoaded = LoadAssemblyFromName(assemblyNames);
#endif
assembliesLoaded = assembliesLoaded.Where(x => !DotNetFrameworkFilter.IsFrameworkAssembly(x)).ToArray();
var nonFrameworkAssembliesLoaded = assembliesLoaded.Where(x => !DotNetFrameworkFilter.IsFrameworkAssembly(x));

var referencedAssemblies = assembliesLoaded
var referencedAssemblies = nonFrameworkAssembliesLoaded
.SelectMany(assembly =>
{
return assembly
Expand All @@ -68,7 +68,7 @@ public static IList<Assembly> GetReferencedAssemblies()
.Where(x => x != null)
.Distinct();

return assembliesLoaded.Concat(referencedAssemblies).Distinct().ToList();
return nonFrameworkAssembliesLoaded.Concat(referencedAssemblies).Distinct();
}

#if !FEATURE_APPDOMAIN_GETASSEMBLIES
Expand Down
39 changes: 22 additions & 17 deletions src/Lucene.Net/Util/SPIClassIterator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JCG = J2N.Collections.Generic;

namespace Lucene.Net.Util
Expand Down Expand Up @@ -32,29 +33,33 @@ namespace Lucene.Net.Util
/// </summary>
public class SPIClassIterator<S> : IEnumerable<Type>
{
private static JCG.HashSet<Type> types = LoadTypes();
private static readonly JCG.HashSet<Type> types = LoadTypes();

private static JCG.HashSet<Type> LoadTypes() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006)
{
types = new JCG.HashSet<Type>();
var types = new JCG.HashSet<Type>();

var assembliesToExamine = Support.AssemblyUtils.GetReferencedAssemblies();

// LUCENENET HACK:
// Tests such as TestImpersonation.cs expect that the assemblies
// are probed in a certain order. NamedSPILoader, lines 68 - 75 adds
// the first item it sees with that name. So if you have multiple
// codecs, it may not add the right one, depending on the order of
// the assemblies that were examined.
// This results in many test failures if Types from Lucene.Net.Codecs
// are examined and added to NamedSPILoader first before
// Lucene.Net.TestFramework.
var testFrameworkAssembly = assembliesToExamine.FirstOrDefault(x => string.Equals(x.GetName().Name, "Lucene.Net.TestFramework", StringComparison.Ordinal));
if (testFrameworkAssembly != null)
{
assembliesToExamine.Remove(testFrameworkAssembly);
assembliesToExamine.Insert(0, testFrameworkAssembly);
}
// LUCENENET NOTE: The following hack is not required because we are using abstract factories
// and pure DI to ensure the order of the codecs are always correct during testing.

//// LUCENENET HACK:
//// Tests such as TestImpersonation.cs expect that the assemblies
//// are probed in a certain order. NamedSPILoader, lines 68 - 75 adds
//// the first item it sees with that name. So if you have multiple
//// codecs, it may not add the right one, depending on the order of
//// the assemblies that were examined.
//// This results in many test failures if Types from Lucene.Net.Codecs
//// are examined and added to NamedSPILoader first before
//// Lucene.Net.TestFramework.
//var testFrameworkAssembly = assembliesToExamine.FirstOrDefault(x => string.Equals(x.GetName().Name, "Lucene.Net.TestFramework", StringComparison.Ordinal));
//if (testFrameworkAssembly != null)
//{
// //assembliesToExamine.Remove(testFrameworkAssembly);
// //assembliesToExamine.Insert(0, testFrameworkAssembly);
// assembliesToExamine = new Assembly[] { testFrameworkAssembly }.Concat(assembliesToExamine.Where(a => !testFrameworkAssembly.Equals(a)));
//}

foreach (var assembly in assembliesToExamine)
{
Expand Down