"Fossies" - the Fresh Open Source Software Archive 
Member "groovy-4.0.12/src/test/org/codehaus/groovy/util/AbstractConcurrentMapSegmentTest.groovy" (31 Jan 1980, 5381 Bytes) of package /linux/misc/apache-groovy-src-4.0.12.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.codehaus.groovy.util
20
21 import org.junit.Before
22 import org.junit.Test
23
24 class AbstractConcurrentMapSegmentTest {
25 private static final Integer INITIAL_SEGMENT_SIZE = 100
26 private static final Integer SEGMENT_THRESHOLD = 0.75f * INITIAL_SEGMENT_SIZE
27
28 // Incrementing counter used to generate unique key names for TestEntry objects
29 // across all test methods in this class
30 private static int keyId
31
32 TestSegment segment
33 List<TestEntry> entries = []
34 int rehashCount = 0
35
36 @Before
37 public void setUp() throws Exception {
38 segment = new TestSegment(INITIAL_SEGMENT_SIZE)
39 }
40
41 @Test
42 public void testSegmentWillNotRehash() {
43 whenIAddElements(50)
44 thenRehashHappenedTimes(0)
45 thenSegmentExpands(false)
46 }
47
48 @Test
49 public void testSegmentWillNotRehashEdgeCase() {
50 whenIAddElements(SEGMENT_THRESHOLD + 1)
51 thenRehashHappenedTimes(0)
52 thenSegmentExpands(false)
53 }
54
55 @Test
56 public void testSegmentWillRehashAndExpand() {
57 whenIAddElements(SEGMENT_THRESHOLD + 2)
58 thenRehashHappenedTimes(1)
59 thenSegmentExpands(true)
60 }
61
62 @Test
63 public void testSegmentWillRehashAndExpandManyTimes() {
64 int elementCount = (SEGMENT_THRESHOLD + 1 ) * 6
65 whenIAddElements(elementCount)
66 //456 elements fit into segment of size 800, which is 100 * 2 * 2 * 2
67 thenSegmentSizeIs(INITIAL_SEGMENT_SIZE * 2 * 2 * 2)
68 thenRehashHappenedTimes(3)
69 }
70
71 @Test
72 public void testSegmentWillRehashWithNoExpansion() {
73 whenIAddElements(SEGMENT_THRESHOLD)
74 whenISetElementsAsInvalid(50)
75 whenIAddElements(50)
76 thenRehashHappenedTimes(1)
77 thenSegmentExpands(false)
78 }
79
80 @Test
81 public void testSegmentWillRehashAndEventuallyExpand() {
82 whenIAddElements(SEGMENT_THRESHOLD)
83
84 // 1-st rehash
85 whenISetElementsAsInvalid(50)
86 whenIAddElements(50)
87 thenSegmentExpands(false)
88
89 // 2-nd rehash
90 whenISetElementsAsInvalid(30)
91 whenIAddElements(30)
92 thenSegmentExpands(false)
93
94 // 3-nd rehash
95 whenISetElementsAsInvalid(20)
96 whenIAddElements(20)
97 thenSegmentExpands(false)
98
99 // 4-th rehash with none invalid => expansion: segment * 2
100 whenIAddElements(SEGMENT_THRESHOLD)
101
102 thenRehashHappenedTimes(4)
103 thenSegmentSizeIs(INITIAL_SEGMENT_SIZE * 2)
104 }
105
106 private void whenIAddElements(int count) {
107 count.times {
108 String key = "k:${++keyId}-${it}"
109 segment.put(key, key.hashCode(), "v${it}")
110 }
111 }
112
113 private void whenISetElementsAsInvalid(int count) {
114 List<TestEntry> validEntries = entries.findAll { it.isValid() }
115 count.times {
116 validEntries.get(it).setValid(false)
117 }
118 }
119
120 private void thenRehashHappenedTimes(int expectedRehashCount) {
121 assert rehashCount == expectedRehashCount
122 }
123
124 private void thenSegmentSizeIs(int expectedSize) {
125 assert segment.table.length == expectedSize
126 }
127
128 private void thenSegmentExpands(boolean truth) {
129 assert segment.table.length > INITIAL_SEGMENT_SIZE == truth
130 }
131
132 class TestSegment extends org.codehaus.groovy.util.AbstractConcurrentMap.Segment {
133
134 protected TestSegment(int initialCapacity) {
135 super(initialCapacity)
136 }
137
138 @Override
139 protected org.codehaus.groovy.util.AbstractConcurrentMap.Entry createEntry(Object key, int hash, Object value) {
140 TestEntry entry = new TestEntry(key, hash, value)
141 entries.add(entry)
142 return entry
143 }
144
145 @Override
146 void rehash() {
147 rehashCount++
148 super.rehash()
149 }
150 }
151 }
152
153 class TestEntry implements org.codehaus.groovy.util.AbstractConcurrentMap.Entry {
154 Object key
155 Object value
156 int hash
157 boolean valid = true;
158
159 public TestEntry(Object key, int hash, Object value) {
160 this.key = key
161 this.hash = hash
162 this.value = value
163 }
164
165 @Override
166 boolean isEqual(Object key, int hash) {
167 return hash == this.hash && key.equals(this.key)
168 }
169
170 @Override
171 Object getValue() {
172 return value
173 }
174
175 @Override
176 void setValue(Object value) {
177 this.value = value
178 }
179
180 @Override
181 int getHash() {
182 return hash
183 }
184
185 @Override
186 boolean isValid() {
187 return valid
188 }
189
190 public void setValid(boolean valid) {
191 this.valid = valid
192 }
193 }