001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005:
006: package org.terracotta.modules.surefire_2_3;
007:
008: import com.tc.object.bytecode.Manager;
009: import com.tc.object.bytecode.ManagerUtil;
010:
011: import junit.framework.Test;
012: import junit.framework.TestCase;
013:
014: public class JUnitBarrier {
015: protected final int parties;
016:
017: protected boolean broken = false;
018: protected int count;
019: protected int resets = 0;
020:
021: public static void createBarrierAndWait(Test t) {
022: int numberOfNodes;
023: try {
024: numberOfNodes = Integer.parseInt(System.getProperty(
025: "tc.numberOfNodes", "0"));
026: } catch (Exception ex) {
027: numberOfNodes = 0;
028: }
029: if (numberOfNodes == 0) {
030: return;
031: }
032:
033: String testName = t.getClass().getName();
034: if (t instanceof TestCase) {
035: testName = testName + ":" + ((TestCase) t).getName();
036: }
037:
038: String globalLock = "@junit_test_suite_lock";
039:
040: JUnitBarrier barrier;
041: ManagerUtil.beginLock(globalLock, Manager.LOCK_TYPE_WRITE);
042: try {
043: barrier = (JUnitBarrier) ManagerUtil.lookupOrCreateRoot(
044: "barrier:" + testName, //
045: new JUnitBarrier(numberOfNodes));
046: } finally {
047: ManagerUtil.commitLock(globalLock);
048: }
049:
050: barrier.barrier();
051: }
052:
053: public JUnitBarrier(int parties) {
054: if (parties <= 0) {
055: throw new IllegalArgumentException();
056: }
057: this .parties = parties;
058: this .count = parties;
059: }
060:
061: public int barrier() {
062: return doBarrier(false, 0L);
063: }
064:
065: protected synchronized int doBarrier(boolean flag, long l) {
066: int i = --count;
067: if (broken) {
068: throw new RuntimeException("Barrier " + i + " broken");
069: }
070: if (Thread.interrupted()) {
071: broken = true;
072: notifyAll();
073: throw new RuntimeException("Barier " + i + " interrupted");
074: }
075: if (i == 0) {
076: count = parties;
077: resets++;
078: notifyAll();
079: return 0;
080: }
081: if (flag && l <= 0L) {
082: broken = true;
083: notifyAll();
084: throw new RuntimeException("Barier " + i + " timed out "
085: + l);
086: }
087:
088: int j = resets;
089: long l1 = flag ? System.currentTimeMillis() : 0L;
090: long l2 = l;
091: do {
092: do {
093: try {
094: wait(l2);
095: } catch (InterruptedException ex) {
096: if (resets == j) {
097: broken = true;
098: notifyAll();
099: throw new RuntimeException("Barier " + i
100: + " interrupted " + ex.toString());
101: }
102: Thread.currentThread().interrupt();
103: }
104: if (broken) {
105: throw new RuntimeException("Barrier " + i
106: + " broken");
107: }
108: if (j != resets) {
109: return i;
110: }
111: } while (!flag);
112: l2 = l - (System.currentTimeMillis() - l1);
113: } while (l2 > 0L);
114: broken = true;
115: notifyAll();
116: throw new RuntimeException("Barier " + i + " timed out " + l);
117: }
118: }
|