01: /**
02: * Copyright 2003-2007 Luck Consulting Pty Ltd
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */package net.sf.ehcache.constructs.concurrent;
16:
17: import junit.framework.TestCase;
18: import net.sf.ehcache.CacheException;
19: import org.apache.commons.logging.Log;
20: import org.apache.commons.logging.LogFactory;
21:
22: /**
23: * Isolation tests for ConcurrencyUtil
24: *
25: * @author Greg Luck
26: * @version $Id: ConcurrencyUtilTest.java 519 2007-07-27 07:11:45Z gregluck $
27: */
28: public class ConcurrencyUtilTest extends TestCase {
29:
30: private static final Log LOG = LogFactory
31: .getLog(ConcurrencyUtilTest.class.getName());
32:
33: /**
34: * Tests that stripes are evently distributed
35: */
36: public void testStripingDistribution() {
37:
38: int[] lockIndexes = new int[2048];
39: for (int i = 0; i < 20480 * 3; i++) {
40: String key = "" + i * 3 / 2 + i;
41: key += key.hashCode();
42: int lock = ConcurrencyUtil.selectLock(key, 2048);
43: lockIndexes[lock]++;
44: }
45:
46: int outliers = 0;
47: for (int i = 0; i < 2048; i++) {
48: if (20 <= lockIndexes[i] && lockIndexes[i] <= 40) {
49: continue;
50: }
51: LOG.info(i + ": " + lockIndexes[i]);
52: outliers++;
53: }
54: assertTrue(outliers <= 128);
55: }
56:
57: /**
58: * Tests edge conditions for striping mechanism.
59: */
60: public void testNullKey() {
61: ConcurrencyUtil.selectLock(null, 2048);
62: ConcurrencyUtil.selectLock("", 2048);
63: }
64:
65: /**
66: * Tests edge conditions for striping mechanism.
67: */
68: public void testEvenLockNumber() {
69: try {
70: ConcurrencyUtil.selectLock("anything", 100);
71: } catch (CacheException e) {
72: //expected
73: }
74: }
75:
76: }
|