001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.util;
006:
007: import EDU.oswego.cs.dl.util.concurrent.CountDown;
008:
009: import com.tc.object.ObjectID;
010:
011: import junit.framework.TestCase;
012:
013: public class SyncObjectIdSetImplTest extends TestCase {
014:
015: private SyncObjectIdSetImpl set;
016: private int threadCnt;
017: private CountDown preMon;
018: private CountDown postMon;
019: private ObjectID id1;
020: private ObjectID id2;
021:
022: protected void setUp() throws Exception {
023: id1 = new ObjectID(1);
024: id2 = new ObjectID(2);
025: set = new SyncObjectIdSetImpl();
026: threadCnt = 5;
027: preMon = new CountDown(threadCnt);
028: postMon = new CountDown(threadCnt);
029: }
030:
031: public void testRemove() throws Throwable {
032: set.add(id1);
033: set.add(id2);
034: assertEquals(2, set.size());
035:
036: // test non-blocking...
037: set.remove(id1);
038: assertEquals(1, set.size());
039:
040: // test blocking...
041: checkBlocking(new RemoveCaller(id2));
042: assertEquals(0, set.size());
043: }
044:
045: public void testNonBlockingContains() throws Throwable {
046: final ObjectID id3 = new ObjectID(3);
047: set.add(id1);
048: set.add(id2);
049: assertEquals(2, set.size());
050:
051: // test non-blocking...
052: assertTrue(set.contains(id1));
053: assertTrue(set.contains(id2));
054: assertFalse(set.contains(id3));
055:
056: set.startPopulating();
057: assertTrue(set.contains(id1));
058: assertTrue(set.contains(id2));
059: }
060:
061: public void testBlockingContains() throws Throwable {
062: final ObjectID id3 = new ObjectID(3);
063: set.add(id1);
064: set.add(id2);
065: assertEquals(2, set.size());
066:
067: assertTrue(set.contains(id1));
068: assertTrue(set.contains(id2));
069: assertFalse(set.contains(id3));
070:
071: // test blocking..
072: checkBlocking(new ContainsCaller(id3));
073: }
074:
075: private void checkBlocking(MethodCaller caller) throws Throwable {
076: set.startPopulating();
077: for (int i = 0; i < threadCnt; i++) {
078: ClientThread ct = new ClientThread(caller, set, preMon,
079: postMon);
080: ct.setDaemon(true);
081: ct.start();
082: }
083: preMon.acquire();
084: // here, we can be sure that all client threads a ready to call the method..
085: // sleep for a while, then make sure that no thread came out of the method..
086: System.err.println("\n### Sleeping...");
087: Thread.sleep(10 * 1000);
088: System.err.println("\n### Woke up...");
089: assertEquals(threadCnt, postMon.currentCount());
090:
091: // now release all client threads
092: set.stopPopulating(new ObjectIDSet2());
093: // if all threads don't come out of the method we'll hang forever on the next line..
094: postMon.acquire();
095:
096: }
097:
098: }
099:
100: abstract class MethodCaller {
101: protected final ObjectID id;
102:
103: public MethodCaller(ObjectID id) {
104: this .id = id;
105: }
106:
107: abstract void invoke(SyncObjectIdSetImpl set);
108: }
109:
110: class RemoveCaller extends MethodCaller {
111: public RemoveCaller(ObjectID id) {
112: super (id);
113: }
114:
115: public void invoke(SyncObjectIdSetImpl set) {
116: set.remove(id);
117: }
118: }
119:
120: class ContainsCaller extends MethodCaller {
121: public ContainsCaller(ObjectID id) {
122: super (id);
123: }
124:
125: public void invoke(SyncObjectIdSetImpl set) {
126: set.contains(id);
127: }
128: }
129:
130: class ClientThread extends Thread {
131: private final MethodCaller mc;
132: private final SyncObjectIdSetImpl set;
133: private final CountDown pre;
134: private final CountDown post;
135:
136: public ClientThread(MethodCaller mc, SyncObjectIdSetImpl set,
137: CountDown pre, CountDown post) {
138: this .mc = mc;
139: this .set = set;
140: this .pre = pre;
141: this .post = post;
142: }
143:
144: public void run() {
145: try {
146: pre.release();
147: mc.invoke(set);
148: post.release();
149: } catch (Throwable e) {
150: e.printStackTrace();
151: throw new RuntimeException(e);
152: }
153: }
154: }
|