001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.stats.counter;
005:
006: import java.util.Random;
007:
008: import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
009: import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef;
010: import junit.framework.TestCase;
011:
012: public class CounterTest extends TestCase {
013:
014: public void testInitialValue() {
015: Counter counter = new CounterImpl();
016: assertEquals(0L, counter.getValue());
017:
018: counter = new CounterImpl(42L);
019: assertEquals(42L, counter.getValue());
020: }
021:
022: public void testConcurrency() throws InterruptedException {
023: final Counter counter = new CounterImpl();
024: final SynchronizedLong local = new SynchronizedLong(0L);
025: final SynchronizedRef error = new SynchronizedRef(null);
026:
027: Thread[] threads = new Thread[10];
028: for (int i = 0; i < threads.length; i++) {
029: threads[i] = new Thread() {
030: public void run() {
031: try {
032: Random random = new Random(hashCode());
033: for (int n = 0; n < 100000; n++) {
034: int operation = random.nextInt(4);
035:
036: switch (operation) {
037: case 0: {
038: local.decrement();
039: counter.decrement();
040: break;
041: }
042: case 1: {
043: local.increment();
044: counter.increment();
045: break;
046: }
047: case 2: {
048: long amount = random.nextLong();
049: local.subtract(amount);
050: counter.decrement(amount);
051: break;
052: }
053: case 3: {
054: long amount = random.nextLong();
055: local.add(amount);
056: counter.increment(amount);
057: break;
058: }
059: default: {
060: throw new RuntimeException("operation "
061: + operation);
062: }
063: }
064: }
065: } catch (Throwable t) {
066: t.printStackTrace();
067: error.set(t);
068: }
069: }
070: };
071: threads[i].start();
072: }
073:
074: for (int i = 0; i < threads.length; i++) {
075: threads[i].join();
076: }
077:
078: if (error.get() != null) {
079: fail(error.get().toString());
080: }
081:
082: assertEquals(local.get(), counter.getValue());
083: }
084:
085: public void testMinMax() {
086: Counter counter = new CounterImpl();
087: assertEquals(0, counter.getMinValue());
088: assertEquals(0, counter.getMaxValue());
089:
090: counter.increment();
091: assertEquals(0, counter.getMinValue());
092: assertEquals(1, counter.getMaxValue());
093:
094: counter.decrement();
095: assertEquals(0, counter.getMinValue());
096: assertEquals(1, counter.getMaxValue());
097: counter.decrement();
098: assertEquals(-1, counter.getMinValue());
099: assertEquals(1, counter.getMaxValue());
100:
101: counter.increment(10);
102: assertEquals(-1, counter.getMinValue());
103: assertEquals(9, counter.getMaxValue());
104:
105: counter.decrement(20);
106: assertEquals(-11, counter.getMinValue());
107: assertEquals(9, counter.getMaxValue());
108:
109: counter.setValue(100);
110: assertEquals(-11, counter.getMinValue());
111: assertEquals(100, counter.getMaxValue());
112:
113: counter.setValue(-100);
114: assertEquals(-100, counter.getMinValue());
115: assertEquals(100, counter.getMaxValue());
116:
117: counter.getAndSet(5);
118: assertEquals(-100, counter.getMinValue());
119: assertEquals(100, counter.getMaxValue());
120: }
121:
122: public void test() {
123: Counter counter = new CounterImpl();
124: assertEquals(0L, counter.getValue());
125:
126: counter.decrement();
127: assertEquals(-1L, counter.getValue());
128:
129: counter.increment();
130: assertEquals(0L, counter.getValue());
131:
132: counter.decrement(10L);
133: assertEquals(-10L, counter.getValue());
134:
135: counter.increment(10L);
136: assertEquals(0L, counter.getValue());
137:
138: counter.increment(-10L);
139: assertEquals(-10L, counter.getValue());
140:
141: counter.decrement(-10L);
142: assertEquals(0L, counter.getValue());
143:
144: counter.setValue(Long.MAX_VALUE);
145: assertEquals(Long.MAX_VALUE, counter.getValue());
146:
147: counter.setValue(Long.MIN_VALUE);
148: assertEquals(Long.MIN_VALUE, counter.getValue());
149:
150: counter.setValue(0L);
151: assertEquals(0L, counter.getValue());
152:
153: counter.increment();
154: counter.increment();
155: counter.increment();
156: long value = counter.getAndSet(42L);
157: assertEquals(3L, value);
158: assertEquals(42L, counter.getValue());
159: }
160:
161: }
|