Repository: lucenenet Updated Branches: refs/heads/branch_4x 709ebb325 -> ea99b79bd
adding seed utils, threadgroup, seedattribute, initial work on RandomizedContext. Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/ea99b79b Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/ea99b79b Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/ea99b79b Branch: refs/heads/branch_4x Commit: ea99b79bdfa39320691bf6cc55f83171f96020e4 Parents: 709ebb3 Author: michael herndon <[email protected]> Authored: Sun Mar 23 15:25:03 2014 -0400 Committer: michael herndon <[email protected]> Committed: Sun Mar 23 15:25:03 2014 -0400 ---------------------------------------------------------------------- .../Randomized/Attributes/SeedAttribute.cs | 18 +++++ .../Attributes/SeedDecoratorAttribute.cs | 19 ++++- .../Attributes/ThreadLeakScopeAttribute.cs | 26 +++++++ .../Randomized/RandomizedContext.cs | 42 ++++++++++- test/test-framework/Randomized/SeedUtils.cs | 76 ++++++++++++++++++++ test/test-framework/Randomized/ThreadGroup.cs | 11 +++ 6 files changed, 189 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ea99b79b/test/test-framework/Randomized/Attributes/SeedAttribute.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/Attributes/SeedAttribute.cs b/test/test-framework/Randomized/Attributes/SeedAttribute.cs new file mode 100644 index 0000000..a2b8c27 --- /dev/null +++ b/test/test-framework/Randomized/Attributes/SeedAttribute.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Randomized.Attributes +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] + public class SeedAttribute : System.Attribute + { + public string Value { get; protected set; } + + public SeedAttribute(string value) + { + this.Value= value; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ea99b79b/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 index b10efe9..11d6140 100644 --- a/test/test-framework/Randomized/Attributes/SeedDecoratorAttribute.cs +++ b/test/test-framework/Randomized/Attributes/SeedDecoratorAttribute.cs @@ -1,4 +1,21 @@ -using System; +/* + * 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; http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ea99b79b/test/test-framework/Randomized/Attributes/ThreadLeakScopeAttribute.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/Attributes/ThreadLeakScopeAttribute.cs b/test/test-framework/Randomized/Attributes/ThreadLeakScopeAttribute.cs new file mode 100644 index 0000000..5cbe83b --- /dev/null +++ b/test/test-framework/Randomized/Attributes/ThreadLeakScopeAttribute.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Randomized.Attributes +{ + public enum ThreadLeakScopes + { + Test, + Suite, + None + } + + [AttributeUsage(AttributeTargets.Class)] + public class ThreadLeakScopeAttribute : System.Attribute + { + + public ThreadLeakScopes Scope { get; protected set; } + + public ThreadLeakScopeAttribute(ThreadLeakScopes scope) + { + this.Scope = scope; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ea99b79b/test/test-framework/Randomized/RandomizedContext.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/RandomizedContext.cs b/test/test-framework/Randomized/RandomizedContext.cs index 985b165..1eb6365 100644 --- a/test/test-framework/Randomized/RandomizedContext.cs +++ b/test/test-framework/Randomized/RandomizedContext.cs @@ -27,7 +27,7 @@ namespace Lucene.Net.Randomized public class RandomizedContext : IDisposable { private static readonly object globalLock = new object(); - private static readonly object contextLock = new object(); + protected readonly object contextLock = new object(); private class ThreadResources { @@ -78,7 +78,45 @@ namespace Lucene.Net.Randomized private static RandomizedContext Context(Thread thread) { - return null; + var group = thread.GetThreadGroup(); + + RandomizedContext context; + + lock(globalLock) + { + + + while (true) + { + context = contexts[group]; + if (context == null && group.Parent != null) + group = group.Parent; + else + break; + } + } + + if(contexts == null) + { + // TODO: revist + var message = "No context information for thread," + thread.Name + ". " + + "Is this thread running under a " + typeof(RandomizedRunner).Name + " context? "; + + throw new IllegalStateException(message); + } + + lock (context.contextLock) + { + if (!context.threadResources.ContainsKey(thread)) + { + var resources = new ThreadResources(); + resources.Queue.Enqueue(context.runner.Randomness.Clone(thread)); + + context.threadResources.Add(thread, resources); + } + } + + return context; } private void GuardDiposed() http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ea99b79b/test/test-framework/Randomized/SeedUtils.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/SeedUtils.cs b/test/test-framework/Randomized/SeedUtils.cs new file mode 100644 index 0000000..975aa16 --- /dev/null +++ b/test/test-framework/Randomized/SeedUtils.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace Lucene.Net.Randomized +{ + public static class SeedUtils + { + private readonly static char[] HEX = "0123456789ABCDEF".ToCharArray(); + + public static int ParseSeed(string seed) + { + int result = 0; + foreach(var item in seed.ToCharArray()) + { + var character = Char.ToLower(item); + result = result << 4; + if (character >= '0' && character <= '9') + result |= (character - '0'); + else if (character >= 'a' && character <= 'f') + result |= (character - 'a' + 10); + else + throw new ArgumentException("Expected the seed to be in a hexadecimal format: " + seed); + } + + return result; + } + + public static string FormatSeed(int seed) + { + var sb = new StringBuilder(); + do + { + sb.Append(HEX[(int)(seed & 0xf)]); + seed = seed >> 4; + } while (seed != 0); + + return sb.ToString().Reverse().ToString(); + } + + public static int[] ParseSeedChain(String chain) + { + if(chain == null) + throw new ArgumentNullException("chain"); + + chain = chain.Replace("[", "").Replace("]", ""); + var matches = Regex.Matches("[0-9A-Fa-f\\:]+", chain); + if (matches.Count == 0) + throw new ArgumentException("Not a valid seed chain: " + chain, "chain"); + + var parts = chain.Split(":".ToCharArray()); + int[] result = new int[parts.Length]; + + for (int i = 0; i < parts.Length; i++) + result[i] = ParseSeed(parts[i]); + + return result; + } + + public static string FormatStringChain(params Randomness[] values) + { + var sb = new StringBuilder(); + sb.Append("["); + for (int i = 0; i < values.Length; i++) + { + if (i > 0) + sb.Append(":"); + sb.Append(FormatSeed(values[i].Seed)); + } + + return sb.ToString(); + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ea99b79b/test/test-framework/Randomized/ThreadGroup.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/Randomized/ThreadGroup.cs b/test/test-framework/Randomized/ThreadGroup.cs index c5ab1e0..cf67783 100644 --- a/test/test-framework/Randomized/ThreadGroup.cs +++ b/test/test-framework/Randomized/ThreadGroup.cs @@ -68,8 +68,17 @@ namespace Lucene.Net.Randomized public string Name { get; protected set; } + public ThreadGroup Parent { get; protected set; } + public ThreadGroup(string name) + : this(name, null) + { + + } + + public ThreadGroup(string name, ThreadGroup parent) { + this.Parent = parent; this.Name = name; this.threads = new List<WeakReference>(); lock(GroupLock){ @@ -77,6 +86,8 @@ namespace Lucene.Net.Randomized } } + + internal void Add(Thread instance) { var threadRef = new WeakReference(instance);
