KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > lockmanager > impl > LockTimerTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.objectserver.lockmanager.impl;
6
7 import com.tc.exception.ImplementMe;
8 import com.tc.net.protocol.tcm.ChannelID;
9 import com.tc.net.protocol.tcm.MessageChannel;
10 import com.tc.net.protocol.tcm.MockMessageChannel;
11 import com.tc.object.lockmanager.api.LockID;
12 import com.tc.object.msg.BatchTransactionAcknowledgeMessage;
13 import com.tc.object.net.DSOChannelManager;
14 import com.tc.object.net.DSOChannelManagerEventListener;
15 import com.tc.objectserver.lockmanager.api.LockAwardContext;
16 import com.tc.util.TCAssertionError;
17
18 import java.util.Collection JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import junit.framework.TestCase;
23
24 public class LockTimerTest extends TestCase {
25
26   private LockTimer timer;
27   private LockAwardContext lockAwardContext;
28   private MockChannelManager channelManager;
29   private MockMessageChannel channel;
30   private ChannelID channelId;
31   private int timeout;
32
33   protected void setUp() throws Exception JavaDoc {
34     super.setUp();
35     this.channelId = new ChannelID(101);
36     this.channel = new MockMessageChannel(channelId);
37     this.channelManager = new MockChannelManager();
38     this.channelManager.addChannel(this.channel);
39
40     this.timer = new LockTimer(this.channelManager);
41     this.lockAwardContext = new LockAwardContext() {
42
43       public LockID getLockID() {
44         throw new ImplementMe();
45       }
46
47       public ChannelID getChannelID() {
48         return channel.getChannelID();
49       }
50
51       public long getTimeout() {
52         return timeout;
53       }
54
55     };
56   }
57
58   public void testNotifyAddPending() throws Exception JavaDoc {
59     this.timeout = 1000;
60
61     // check that pending count is greater than zero.
62
try {
63       this.timer.notifyAddPending(0, this.lockAwardContext);
64       fail("Should have thrown an assertion error.");
65     } catch (TCAssertionError e) {
66       // expected
67
}
68
69     // adding a pending with a pending count greater than one
70
this.timer.notifyAddPending(2, this.lockAwardContext);
71     checkTimerDoesNotFire();
72
73     // adding a pending with a pending count of one should schedule the timeout.
74
this.timer.notifyAddPending(1, this.lockAwardContext);
75     checkTimerFires();
76   }
77
78   public void testNotifyAward() throws Exception JavaDoc {
79     this.timeout = 1000;
80
81     // the pending count must be >= 0;
82
try {
83       this.timer.notifyAward(-1, this.lockAwardContext);
84       fail("Should have thrown an assertion error.");
85     } catch (TCAssertionError e) {
86       // expected
87
}
88
89     // if the pending count is 0, then the timeout should not get scheduled.
90
this.timer.notifyAward(0, this.lockAwardContext);
91     checkTimerDoesNotFire();
92
93     // if the pending count is > 0, then the timeout SHOULD get scheduled.
94
this.timer.notifyAward(1, this.lockAwardContext);
95     checkTimerFires();
96
97     this.timer.notifyAward(100, this.lockAwardContext);
98     checkTimerFires();
99   }
100
101   public void testNotifyRevoke() throws Exception JavaDoc {
102     this.timeout = 1000;
103
104     // if the award context is revoked without the timer having been scheduled, we should throw an assertion
105
// error
106
try {
107       this.timer.notifyRevoke(this.lockAwardContext);
108       fail("Expected an assertion error.");
109     } catch (TCAssertionError e) {
110       // expected
111
}
112
113     // if the award context is added without any pending and then revoked, there should be no assertion fired.
114
this.timer.notifyAward(0, this.lockAwardContext);
115     this.timer.notifyRevoke(this.lockAwardContext);
116
117     // if the award context is revoked after being awarded, the timer should not fire
118
this.timer.notifyAward(1, this.lockAwardContext);
119     checkTimerFires();
120     this.timer.notifyAward(1, this.lockAwardContext);
121     Thread.sleep(timeout / 4);
122     this.timer.notifyRevoke(this.lockAwardContext);
123     checkTimerDoesNotFire();
124
125     // if a different award context is revoked, the original one should still fire.
126
this.timer.notifyAward(1, this.lockAwardContext);
127
128     LockAwardContext newContext = new LockAwardContext() {
129
130       public LockID getLockID() {
131         throw new ImplementMe();
132       }
133
134       public ChannelID getChannelID() {
135         return new ChannelID(234709381274908237L);
136       }
137
138       public long getTimeout() {
139         return 1001;
140       }
141     };
142     this.timer.notifyAward(1, newContext);
143     this.timer.notifyRevoke(newContext);
144
145     checkTimerFires();
146   }
147
148   public void testStartTimerForLock() throws Exception JavaDoc {
149     this.timeout = 2000;
150     // max time over the timeout to allow for the test to pass.
151
long excessThreshold = 800;
152     long start = System.currentTimeMillis();
153     this.timer.startTimerForLock(this.lockAwardContext);
154
155     checkTimerFires();
156
157     long elapsed = this.channel.getLastClosedCallTimestamp() - start;
158
159     // make sure that it didn't happen in less time than the lock timeout.
160
assertTrue("elapsed time (" + elapsed + " ms.) not greater than or equal to the timeout (" + timeout + " ms.)",
161                elapsed >= timeout);
162     // make sure that it didn't happen in greater time than the lock timeout + the excess threshold.
163
assertTrue("elapsed time (" + elapsed + " ms.) not less than or equal to the timeout plus excess threshold ("
164                + (timeout + excessThreshold) + " ms.)", elapsed <= (timeout + excessThreshold));
165
166   }
167
168   public void testCancel() throws Exception JavaDoc {
169     timeout = 2000;
170
171     this.timer.startTimerForLock(this.lockAwardContext);
172     // make sure the timer actually fires.
173
checkTimerFires();
174
175     // reschedule
176
this.timer.startTimerForLock(this.lockAwardContext);
177
178     // wait a bit...
179
Thread.sleep(timeout / 4);
180     // cancel the timer...
181
LockAwardContext cancelled = this.timer.cancel(this.lockAwardContext);
182
183     // we should never see the close call.
184
checkTimerDoesNotFire();
185
186     assertEquals("Unexpected return value from cancel.", this.lockAwardContext, cancelled);
187
188     cancelled = this.timer.cancel(this.lockAwardContext);
189     assertTrue("Return value from a cancel that doesn't cancel anything should be null but is: " + cancelled,
190                cancelled == null);
191   }
192
193   private void checkTimerDoesNotFire() throws InterruptedException JavaDoc {
194     assertTrue(timeout > 0);
195     assertFalse(this.channel.waitForCloseCall(timeout + 1000));
196   }
197
198   private void checkTimerFires() throws InterruptedException JavaDoc {
199     assertTrue(timeout > 0);
200     assertTrue(this.channel.waitForCloseCall(timeout + 1000));
201   }
202
203   public static class MockChannelManager implements DSOChannelManager {
204
205     private Map JavaDoc channels = new HashMap JavaDoc();
206
207     public void addChannel(MessageChannel channel) {
208       synchronized (channels) {
209         this.channels.put(channel.getChannelID(), channel);
210       }
211     }
212
213     public MessageChannel getActiveChannel(ChannelID id) {
214       synchronized (channels) {
215         return (MessageChannel) this.channels.get(id);
216       }
217     }
218
219     public MessageChannel[] getActiveChannels() {
220       throw new ImplementMe();
221     }
222
223     public boolean isActiveID(ChannelID channelID) {
224       throw new ImplementMe();
225     }
226
227     public void closeAll(Collection JavaDoc channelIDs) {
228       throw new ImplementMe();
229     }
230
231     public String JavaDoc getChannelAddress(ChannelID channelID) {
232       return null;
233     }
234
235     public BatchTransactionAcknowledgeMessage newBatchTransactionAcknowledgeMessage(ChannelID channelID) {
236       throw new ImplementMe();
237     }
238
239     public Collection JavaDoc getAllActiveChannelIDs() {
240       throw new ImplementMe();
241     }
242
243     public void addEventListener(DSOChannelManagerEventListener listener) {
244       throw new ImplementMe();
245     }
246
247     public void makeChannelActive(ChannelID channelID, long startIDs, long endIDs, boolean persistent) {
248       throw new ImplementMe();
249     }
250
251     public Collection JavaDoc getRawChannelIDs() {
252       throw new ImplementMe();
253     }
254
255     public void makeChannelActiveNoAck(MessageChannel channel) {
256       throw new ImplementMe();
257     }
258
259   }
260
261 }
Popular Tags