1 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 ; 19 import java.util.HashMap ; 20 import java.util.Map ; 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 { 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 { 59 this.timeout = 1000; 60 61 try { 63 this.timer.notifyAddPending(0, this.lockAwardContext); 64 fail("Should have thrown an assertion error."); 65 } catch (TCAssertionError e) { 66 } 68 69 this.timer.notifyAddPending(2, this.lockAwardContext); 71 checkTimerDoesNotFire(); 72 73 this.timer.notifyAddPending(1, this.lockAwardContext); 75 checkTimerFires(); 76 } 77 78 public void testNotifyAward() throws Exception { 79 this.timeout = 1000; 80 81 try { 83 this.timer.notifyAward(-1, this.lockAwardContext); 84 fail("Should have thrown an assertion error."); 85 } catch (TCAssertionError e) { 86 } 88 89 this.timer.notifyAward(0, this.lockAwardContext); 91 checkTimerDoesNotFire(); 92 93 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 { 102 this.timeout = 1000; 103 104 try { 107 this.timer.notifyRevoke(this.lockAwardContext); 108 fail("Expected an assertion error."); 109 } catch (TCAssertionError e) { 110 } 112 113 this.timer.notifyAward(0, this.lockAwardContext); 115 this.timer.notifyRevoke(this.lockAwardContext); 116 117 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 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 { 149 this.timeout = 2000; 150 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 assertTrue("elapsed time (" + elapsed + " ms.) not greater than or equal to the timeout (" + timeout + " ms.)", 161 elapsed >= timeout); 162 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 { 169 timeout = 2000; 170 171 this.timer.startTimerForLock(this.lockAwardContext); 172 checkTimerFires(); 174 175 this.timer.startTimerForLock(this.lockAwardContext); 177 178 Thread.sleep(timeout / 4); 180 LockAwardContext cancelled = this.timer.cancel(this.lockAwardContext); 182 183 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 { 194 assertTrue(timeout > 0); 195 assertFalse(this.channel.waitForCloseCall(timeout + 1000)); 196 } 197 198 private void checkTimerFires() throws InterruptedException { 199 assertTrue(timeout > 0); 200 assertTrue(this.channel.waitForCloseCall(timeout + 1000)); 201 } 202 203 public static class MockChannelManager implements DSOChannelManager { 204 205 private Map channels = new HashMap (); 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 channelIDs) { 228 throw new ImplementMe(); 229 } 230 231 public String getChannelAddress(ChannelID channelID) { 232 return null; 233 } 234 235 public BatchTransactionAcknowledgeMessage newBatchTransactionAcknowledgeMessage(ChannelID channelID) { 236 throw new ImplementMe(); 237 } 238 239 public Collection 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 getRawChannelIDs() { 252 throw new ImplementMe(); 253 } 254 255 public void makeChannelActiveNoAck(MessageChannel channel) { 256 throw new ImplementMe(); 257 } 258 259 } 260 261 } | Popular Tags |