01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest;
05:
06: import com.tc.object.config.ConfigVisitor;
07: import com.tc.object.config.DSOClientConfigHelper;
08: import com.tc.object.config.Root;
09: import com.tc.simulator.app.ApplicationConfig;
10: import com.tc.simulator.listener.ListenerProvider;
11: import com.tc.util.Assert;
12: import com.tctest.runner.AbstractTransparentApp;
13:
14: import java.util.HashMap;
15:
16: public class TransparentWaitNotifyApp extends AbstractTransparentApp {
17: private static final String DONE = "done Biatch!";
18:
19: private static final int INITIAL_STAGE = 0;
20: private static final int ABOUT_TO_WAIT = 1;
21: private static final int WAIT_COMPLETE = 2;
22:
23: private final HashMap sharedRoot = new HashMap();
24:
25: public TransparentWaitNotifyApp(String globalId,
26: ApplicationConfig cfg, ListenerProvider listenerProvider) {
27: super (globalId, cfg, listenerProvider);
28:
29: if ((getIntensity() != 1) || (getParticipantCount() != 2)) {
30: //
31: throw Assert.failure("Invalid parameters");
32: }
33:
34: }
35:
36: public void run() {
37: if ((new Integer(this .getApplicationId()).intValue() % 2) == 0) {
38: testWaiter();
39: } else {
40: testNotify();
41: }
42: notifyResult(Boolean.TRUE);
43: }
44:
45: private void testNotify() {
46: moveToStage(INITIAL_STAGE);
47: moveToStageAndWait(ABOUT_TO_WAIT);
48:
49: synchronized (sharedRoot) {
50: System.err.println("NOTIFY");
51: sharedRoot.notify();
52: }
53:
54: moveToStageAndWait(WAIT_COMPLETE);
55:
56: synchronized (sharedRoot) {
57: Assert.eval("key not in map", sharedRoot.containsKey(DONE));
58: }
59: }
60:
61: private void testWaiter() {
62: moveToStage(INITIAL_STAGE);
63:
64: synchronized (sharedRoot) {
65: moveToStage(ABOUT_TO_WAIT);
66:
67: System.err.println("WAIT");
68:
69: try {
70: sharedRoot.wait();
71: System.err.println("NOTIFIED");
72: } catch (InterruptedException e) {
73: throw new RuntimeException(e);
74: }
75:
76: sharedRoot.put(DONE, null);
77: }
78:
79: moveToStage(WAIT_COMPLETE);
80: }
81:
82: public static void visitL1DSOConfig(ConfigVisitor visitor,
83: DSOClientConfigHelper config) {
84: String testClassName = TransparentWaitNotifyApp.class.getName();
85: config.addRoot(new Root(testClassName, "sharedRoot",
86: "sharedRootLock"), true);
87: String methodExpression = "* " + testClassName + ".*(..)";
88: System.err.println("Adding autolock for: " + methodExpression);
89: config.addWriteAutolock(methodExpression);
90: }
91: }
|