Return-Path: X-Original-To: apmail-lucenenet-commits-archive@www.apache.org Delivered-To: apmail-lucenenet-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4C7CC101E0 for ; Wed, 12 Mar 2014 00:14:49 +0000 (UTC) Received: (qmail 2719 invoked by uid 500); 12 Mar 2014 00:14:48 -0000 Delivered-To: apmail-lucenenet-commits-archive@lucenenet.apache.org Received: (qmail 2671 invoked by uid 500); 12 Mar 2014 00:14:48 -0000 Mailing-List: contact commits-help@lucenenet.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: lucene-net-dev@lucenenet.apache.org Delivered-To: mailing list commits@lucenenet.apache.org Received: (qmail 2664 invoked by uid 99); 12 Mar 2014 00:14:47 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Mar 2014 00:14:47 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 68E30940F0B; Wed, 12 Mar 2014 00:14:47 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: mherndon@apache.org To: commits@lucenenet.apache.org Message-Id: <529640eb753d416ebe372aa5b0957910@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: starting to work on porting randomized testing for the test framework. Date: Wed, 12 Mar 2014 00:14:47 +0000 (UTC) Repository: lucenenet Updated Branches: refs/heads/branch_4x fcc56d705 -> 709ebb325 starting to work on porting randomized testing for the test framework. Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/709ebb32 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/709ebb32 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/709ebb32 Branch: refs/heads/branch_4x Commit: 709ebb3253f79419f91cdc92dc254f53dcfa3841 Parents: fcc56d7 Author: michael herndon Authored: Tue Mar 11 20:14:22 2014 -0400 Committer: michael herndon Committed: Tue Mar 11 20:14:22 2014 -0400 ---------------------------------------------------------------------- test/core/Lucene.Net.Test.csproj | 1 + .../Lucene.Net.TestFramework.csproj | 15 ++- .../Attributes/SeedDecoratorAttribute.cs | 25 ++++ .../test-framework/Randomized/ISeedDecorator.cs | 30 +++++ .../Randomized/IllegalStateException.cs | 61 +++++++++ .../InternalAssumptionViolatedException.cs | 44 ++++++ test/test-framework/Randomized/MurmurHash3.cs | 43 ++++++ .../Randomized/RandomizedContext.cs | 107 +++++++++++++++ .../Randomized/RandomizedRunner.cs | 65 +++++++++ test/test-framework/Randomized/Randomness.cs | 105 +++++++++++++++ .../Randomized/SingleThreadedRandom.cs | 135 +++++++++++++++++++ test/test-framework/Randomized/ThreadGroup.cs | 116 ++++++++++++++++ test/test-framework/Util/LuceneTestCase.cs | 4 +- 13 files changed, 747 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/core/Lucene.Net.Test.csproj ---------------------------------------------------------------------- diff --git a/test/core/Lucene.Net.Test.csproj b/test/core/Lucene.Net.Test.csproj index d92e5fd..b42c779 100644 --- a/test/core/Lucene.Net.Test.csproj +++ b/test/core/Lucene.Net.Test.csproj @@ -271,6 +271,7 @@ + http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Lucene.Net.TestFramework.csproj ---------------------------------------------------------------------- diff --git a/test/test-framework/Lucene.Net.TestFramework.csproj b/test/test-framework/Lucene.Net.TestFramework.csproj index 86cbed0..823f1f7 100644 --- a/test/test-framework/Lucene.Net.TestFramework.csproj +++ b/test/test-framework/Lucene.Net.TestFramework.csproj @@ -54,13 +54,23 @@ + + + + In + + + + + + + + - - @@ -70,7 +80,6 @@ - http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/Attributes/SeedDecoratorAttribute.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/Attributes/SeedDecoratorAttribute.cs b/test/test-framework/Randomized/Attributes/SeedDecoratorAttribute.cs new file mode 100644 index 0000000..b10efe9 --- /dev/null +++ b/test/test-framework/Randomized/Attributes/SeedDecoratorAttribute.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Randomized.Attributes +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] + public class SeedDecoratorAttribute : System.Attribute + { + public IList Decorators { get; set; } + + + public SeedDecoratorAttribute(params Type[] decorators) + { + this.Decorators = new List(); + + foreach (var item in decorators) + { + if (item.GetInterfaces().Contains(typeof(ISeedDecorator))) + this.Decorators.Add(item); + } + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/ISeedDecorator.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/ISeedDecorator.cs b/test/test-framework/Randomized/ISeedDecorator.cs new file mode 100644 index 0000000..5544b9b --- /dev/null +++ b/test/test-framework/Randomized/ISeedDecorator.cs @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Randomized +{ + public interface ISeedDecorator + { + void Initialize(Type type); + int Decorate(int seed); + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/IllegalStateException.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/IllegalStateException.cs b/test/test-framework/Randomized/IllegalStateException.cs new file mode 100644 index 0000000..df8e3fb --- /dev/null +++ b/test/test-framework/Randomized/IllegalStateException.cs @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Randomized +{ + [Serializable] + public class IllegalStateException : Exception + { + private StackTrace trace; + private string traceAsString; + + public override string StackTrace + { + get + { + if(this.trace != null) + return this.trace.ToString(); + + if (!string.IsNullOrEmpty(this.traceAsString)) + return this.traceAsString; + + return base.StackTrace; + } + } + + public IllegalStateException() { } + public IllegalStateException(string message) : base(message) { } + + public IllegalStateException(string message, StackTrace trace) : base(message) { + this.trace = trace; + } + + public IllegalStateException(string message, Exception inner) : base(message, inner) { + this.traceAsString = inner.StackTrace; + } + protected IllegalStateException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) + : base(info, context) { } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/InternalAssumptionViolatedException.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/InternalAssumptionViolatedException.cs b/test/test-framework/Randomized/InternalAssumptionViolatedException.cs new file mode 100644 index 0000000..0d5b744 --- /dev/null +++ b/test/test-framework/Randomized/InternalAssumptionViolatedException.cs @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Randomized +{ + + + [Serializable] + public class InternalAssumptionViolatedException : NUnit.Framework.InconclusiveException + { + + public InternalAssumptionViolatedException(string message) : + base(message ?? "Failed Assumption") { + } + + public InternalAssumptionViolatedException(string message, Exception inner) + : base(message, inner) { } + + protected InternalAssumptionViolatedException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) + : base(info, context) { } + } + +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/MurmurHash3.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/MurmurHash3.cs b/test/test-framework/Randomized/MurmurHash3.cs new file mode 100644 index 0000000..7915869 --- /dev/null +++ b/test/test-framework/Randomized/MurmurHash3.cs @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Randomized +{ + public class MurmurHash3 + { + + private MurmurHash3() { } + + public static int Hash(int k) + { + uint x = (uint)k; + + x ^= x >> 16; + x *= 0x85ebca6b; + x ^= x >> 13; + x *= 0xc2b2ae35; + x ^= x >> 16; + + return (int)x; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/RandomizedContext.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/RandomizedContext.cs b/test/test-framework/Randomized/RandomizedContext.cs new file mode 100644 index 0000000..985b165 --- /dev/null +++ b/test/test-framework/Randomized/RandomizedContext.cs @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using Lucene.Net.Support; + +namespace Lucene.Net.Randomized +{ + public class RandomizedContext : IDisposable + { + private static readonly object globalLock = new object(); + private static readonly object contextLock = new object(); + + private class ThreadResources + { + public ThreadResources() + { + this.Queue = new Queue(); + } + + public Queue Queue { get; private set; } + } + + private static readonly IdentityHashMap contexts = + new IdentityHashMap(); + + private readonly WeakDictionary threadResources + = new WeakDictionary(); + + private readonly ThreadGroup threadGroup; + private Type suiteClass; + private volatile Boolean isDisposed = false; + private RandomizedRunner runner; + + public Type GetTargetType + { + get + { + this.GuardDiposed(); + return this.suiteClass; + } + } + + public int RunnerSeed + { + get + { + return this.runner.Randomness.Seed; + } + } + + private RandomizedContext(ThreadGroup group, Type suiteClass, RandomizedRunner runner) + { + this.threadGroup = group; + this.suiteClass = suiteClass; + this.runner = runner; + } + + + + private static RandomizedContext Context(Thread thread) + { + return null; + } + + private void GuardDiposed() + { + if (this.isDisposed) + throw new ObjectDisposedException("RandomContext is diposed for thread," + Thread.CurrentThread.Name + "."); + } + + + static RandomizedContext Create(ThreadGroup tg, Type suiteClass, RandomizedRunner runner) { + lock(globalLock) { + var context = new RandomizedContext(tg, suiteClass, runner); + contexts.Add(tg, context); + context.threadResources.Add(Thread.CurrentThread, new ThreadResources()); + return context; + } + } + + + + public void Dispose() + { + throw new NotImplementedException(); + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/RandomizedRunner.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/RandomizedRunner.cs b/test/test-framework/Randomized/RandomizedRunner.cs new file mode 100644 index 0000000..42e4b66 --- /dev/null +++ b/test/test-framework/Randomized/RandomizedRunner.cs @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using Lucene.Net.Randomized.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace Lucene.Net.Randomized +{ + public class RandomizedRunner + { + public Randomness Randomness { get; protected set; } + + private Type suiteClass; + + private static int sequence = new int(); + + public RandomizedRunner(Type testClass) + { + this.suiteClass = testClass; + + var list = new List(); + var attrs = this.suiteClass.GetCustomAttributes(typeof(SeedDecoratorAttribute), true).Cast(); + foreach(var attr in attrs) { + foreach(var decoratorType in attr.Decorators) + { + var decorator = (ISeedDecorator)Activator.CreateInstance(decoratorType); + decorator.Initialize(testClass); + list.Add(decorator); + } + } + + + int ticks = (int)System.DateTime.Now.Ticks; + int randomSeed = MurmurHash3.Hash(NextSequence() + ticks); + + int initialSeed = randomSeed; + + this.Randomness = new Randomness(initialSeed, list.ToArray()); + } + + private static int NextSequence() + { + Interlocked.Increment(ref sequence); + return sequence; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/Randomness.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/Randomness.cs b/test/test-framework/Randomized/Randomness.cs new file mode 100644 index 0000000..b141bce --- /dev/null +++ b/test/test-framework/Randomized/Randomness.cs @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace Lucene.Net.Randomized +{ + /// + /// Per-thread, per-lifecycle state randomness defined as an initial seed and + /// the current Random instance for that context. + /// + /// + /// + /// An instance of this class will be typically available from {@link RandomizedContext}. + /// No need to instantiate manually. + /// + /// + /// + /// + public class Randomness : IDisposable + { + private List decorators; + + + public Random Random + { + get { return this.SingleThreadedRandom; } + } + + protected SingleThreadedRandom SingleThreadedRandom { get; set; } + + public int Seed { get; protected set; } + + public Randomness(Thread owner, int seed, params ISeedDecorator[] decorators) + :this(owner, seed, decorators.ToList()) + { + + } + + + public Randomness(int seed, params ISeedDecorator[] decorators) + : this(Thread.CurrentThread, seed, decorators) + { + + } + + protected Randomness(Thread owner, int seed, List decorators) + { + this.Seed = seed; + this.decorators = decorators.ToList(); + + var decoratedSeed = Decorate(seed, this.decorators); + + this.SingleThreadedRandom = new SingleThreadedRandom(owner, + new Random(decoratedSeed) + ); + } + + + + public Randomness Clone(Thread newOwner) + { + return new Randomness(newOwner, this.Seed, this.decorators); + } + + public override string ToString() + { + return "[Randomess, seed=" + this.Seed.ToString() + "]"; + } + + private static int Decorate(int seed, List decorators) + { + var result = seed; + decorators.ForEach(o => result = o.Decorate(result)); + + return result; + } + + + + public void Dispose() + { + if (this.SingleThreadedRandom != null) + this.SingleThreadedRandom.Dispose(); + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/SingleThreadedRandom.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/SingleThreadedRandom.cs b/test/test-framework/Randomized/SingleThreadedRandom.cs new file mode 100644 index 0000000..5ba708f --- /dev/null +++ b/test/test-framework/Randomized/SingleThreadedRandom.cs @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading; + +namespace Lucene.Net.Randomized +{ + /// + /// A random with a delegate, preventing and locked + /// to be used by a single thread. This is the equivelant to AssertRandom + /// + public class SingleThreadedRandom : Random, IDisposable + { + private Random @delegate; + private readonly WeakReference ownerRef; + private readonly string ownerName; + private StackTrace trace; + + + private volatile Boolean isDisposed = true; + + + public SingleThreadedRandom(Thread owner, Random @delegate):base(0) + { + this.@delegate = @delegate; + this.ownerRef = new WeakReference(owner); + this.ownerName = owner.Name; + this.trace = new System.Diagnostics.StackTrace(1); + } + + public override int Next() + { + this.Guard(); + return this.@delegate.Next(); + } + + public override int Next(int maxValue) + { + this.Guard(); + return this.@delegate.Next(maxValue); + } + + public override int Next(int minValue, int maxValue) + { + this.Guard(); + return this.@delegate.Next(minValue, maxValue); + } + + public override void NextBytes(byte[] buffer) + { + this.Guard(); + this.@delegate.NextBytes(buffer); + } + + public override double NextDouble() + { + this.Guard(); + return this.@delegate.NextDouble(); + } + + public override bool Equals(object obj) + { + this.Guard(); + return this.@delegate.Equals(obj); + } + + public override int GetHashCode() + { + this.Guard(); + return this.@delegate.GetHashCode(); + } + + + + private void Guard() + { + /* checkValid(); */ + + if (!this.isDisposed) + throw new ObjectDisposedException( + "This instance of SingleThreadRandom has been disposed. "); + + Thread owner = ownerRef.Target as Thread; + + if(owner == null || owner != Thread.CurrentThread) + { + var message = "The SingleThreadRandom instance was created for thread," + ownerName + + " and must not be shared. The current thread is " + Thread.CurrentThread.Name + "."; + + throw new InvalidOperationException(message, + new IllegalStateException("The instance was illegally accessed", this.trace)); + } + } + + public void Dispose() + { + this.Dispose(true); + + GC.SuppressFinalize(this); + } + + protected void Dispose(bool disposing) + { + if(!this.isDisposed) + { + if (disposing) + { + this.@delegate = null; + this.ownerRef.Target = null; + } + + this.isDisposed = true; + } + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Randomized/ThreadGroup.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/ThreadGroup.cs b/test/test-framework/Randomized/ThreadGroup.cs new file mode 100644 index 0000000..c5ab1e0 --- /dev/null +++ b/test/test-framework/Randomized/ThreadGroup.cs @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace Lucene.Net.Randomized +{ + + public static class ThreadGroupExtensions + { + private static readonly object globalLock = new object(); + + public static ThreadGroup GetThreadGroup(this Thread thread) + { + if(thread.IsAlive) + { + lock(ThreadGroup.GroupLock) + { + foreach(var group in ThreadGroup.Groups) + { + group.Prune(); + foreach (var weak in group) + { + if (thread == (Thread)weak.Target) + return group; + } + } + + ThreadGroup.Root.Add(thread); + return ThreadGroup.Root; + } + } + return null; + } + } + + public class ThreadGroup : IEnumerable, IDisposable + { + private List threads; + internal static readonly object GroupLock = new Object(); + internal static List Groups {get; set;} + + static ThreadGroup() + { + Root = new ThreadGroup("Root"); + Groups = new List(); + } + + public static ThreadGroup Root { get; set; } + + public string Name { get; protected set; } + + public ThreadGroup(string name) + { + this.Name = name; + this.threads = new List(); + lock(GroupLock){ + Groups.Add(this); + } + } + + internal void Add(Thread instance) + { + var threadRef = new WeakReference(instance); + this.threads.Add(threadRef); + + + } + + internal void Prune() + { + var copy = this.threads.ToList(); + foreach (var item in copy) + { + if (!item.IsAlive) + this.threads.Remove(item); + } + } + + + public IEnumerator GetEnumerator() + { + return this.threads.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return this.threads.GetEnumerator(); + } + + public void Dispose() + { + lock(GroupLock){ + Groups.Remove(this); + } + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/709ebb32/test/test-framework/Util/LuceneTestCase.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Util/LuceneTestCase.cs b/test/test-framework/Util/LuceneTestCase.cs index 43ccb94..e937e85 100644 --- a/test/test-framework/Util/LuceneTestCase.cs +++ b/test/test-framework/Util/LuceneTestCase.cs @@ -216,13 +216,15 @@ namespace Lucene.Net.Util // ignoreAfterMaxFailures = new TestRuleIgnoreAfterMaxFailures(maxFailures); //} - bool allowDocsOutOfOrder = true; + //bool allowDocsOutOfOrder = true; public LuceneTestCase() : base() { } + + public LuceneTestCase(System.String name) { }