KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > handshakemanager > ClientHandshakeManagerTest


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.object.handshakemanager;
6
7 import com.tc.async.impl.NullSink;
8 import com.tc.cluster.Cluster;
9 import com.tc.exception.ImplementMe;
10 import com.tc.logging.TCLogging;
11 import com.tc.net.protocol.tcm.TestChannelIDProvider;
12 import com.tc.object.NullPauseListener;
13 import com.tc.object.ObjectID;
14 import com.tc.object.RemoteObjectManager;
15 import com.tc.object.TestClientObjectManager;
16 import com.tc.object.dna.api.DNA;
17 import com.tc.object.gtx.TestClientGlobalTransactionManager;
18 import com.tc.object.lockmanager.api.ClientLockManager;
19 import com.tc.object.lockmanager.api.LockContext;
20 import com.tc.object.lockmanager.api.LockID;
21 import com.tc.object.lockmanager.api.LockLevel;
22 import com.tc.object.lockmanager.api.LockRequest;
23 import com.tc.object.lockmanager.api.Notify;
24 import com.tc.object.lockmanager.api.ThreadID;
25 import com.tc.object.lockmanager.api.WaitContext;
26 import com.tc.object.lockmanager.api.WaitListener;
27 import com.tc.object.lockmanager.api.WaitLockRequest;
28 import com.tc.object.lockmanager.impl.GlobalLockInfo;
29 import com.tc.object.msg.ClientHandshakeMessage;
30 import com.tc.object.msg.ClientHandshakeMessageFactory;
31 import com.tc.object.msg.TestClientHandshakeMessage;
32 import com.tc.object.session.NullSessionManager;
33 import com.tc.object.session.SessionID;
34 import com.tc.object.tx.TestRemoteTransactionManager;
35 import com.tc.object.tx.TransactionID;
36 import com.tc.object.tx.WaitInvocation;
37 import com.tc.test.TCTestCase;
38 import com.tc.util.SequenceID;
39 import com.tc.util.concurrent.NoExceptionLinkedQueue;
40 import com.tc.util.sequence.BatchSequence;
41 import com.tc.util.sequence.BatchSequenceProvider;
42 import com.tc.util.sequence.BatchSequenceReceiver;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collection JavaDoc;
46 import java.util.HashSet JavaDoc;
47 import java.util.LinkedList JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Set JavaDoc;
50
51 public class ClientHandshakeManagerTest extends TCTestCase {
52   private TestClientObjectManager objectManager;
53   private TestClientLockManager lockManager;
54   private TestChannelIDProvider cip;
55   private ClientHandshakeManager mgr;
56   private TestClientHandshakeMessageFactory chmf;
57   private TestRemoteObjectManager remoteObjectManager;
58   private TestRemoteTransactionManager rtxManager;
59   private TestClientGlobalTransactionManager gtxManager;
60
61   public void setUp() throws Exception JavaDoc {
62     objectManager = new TestClientObjectManager();
63     remoteObjectManager = new TestRemoteObjectManager();
64     lockManager = new TestClientLockManager();
65     rtxManager = new TestRemoteTransactionManager();
66     gtxManager = new TestClientGlobalTransactionManager();
67     cip = new TestChannelIDProvider();
68     chmf = new TestClientHandshakeMessageFactory();
69     mgr = new ClientHandshakeManager(TCLogging.getLogger(ClientHandshakeManager.class), cip, chmf, objectManager,
70                                      remoteObjectManager, lockManager, rtxManager, gtxManager, new ArrayList JavaDoc(),
71                                      new NullSink(), new NullSessionManager(), new NullPauseListener(),
72                                      new BatchSequence(new TestSequenceProvider(), 100), new Cluster());
73     assertNotNull(gtxManager.pauseCalls.poll(0));
74     assertNull(gtxManager.pauseCalls.poll(0));
75     newMessage();
76   }
77
78   private void newMessage() {
79     chmf.message = new TestClientHandshakeMessage();
80   }
81
82   public void tests() {
83
84     Collection JavaDoc sequenceIDs = new ArrayList JavaDoc();
85     sequenceIDs.add(new SequenceID(1001));
86     gtxManager.transactionSequenceIDs = sequenceIDs;
87
88     Set JavaDoc objectIds = new HashSet JavaDoc();
89     for (int i = 0; i < 10; i++) {
90       ObjectID id = new ObjectID(i);
91       objectIds.add(id);
92       objectManager.objects.put(id, new Object JavaDoc());
93     }
94
95     WaitLockRequest waitLockRequest = new WaitLockRequest(new LockID("1"), new ThreadID(1), LockLevel.WRITE,
96                                                           new WaitInvocation());
97     lockManager.outstandingWaitLockRequests.add(waitLockRequest);
98
99     LockRequest lockRequest = new LockRequest(new LockID("2"), new ThreadID(2), LockLevel.WRITE);
100     lockManager.outstandingLockAwards.add(lockRequest);
101
102     assertTrue(lockManager.requestOutstandingContexts.isEmpty());
103     assertTrue(chmf.newMessageQueue.isEmpty());
104
105     new Thread JavaDoc(new Runnable JavaDoc() {
106       public void run() {
107         mgr.initiateHandshake();
108       }
109     }).start();
110
111     // make sure that the lock manager was paused and unpaused.
112
// assertEquals(1, lockManager.pauseContexts.size());
113
lockManager.pauseContexts.take();
114
115     TestClientHandshakeMessage sentMessage = (TestClientHandshakeMessage) chmf.newMessageQueue.take();
116     assertTrue(chmf.newMessageQueue.isEmpty());
117
118     // make sure that the manager called send on the message...
119
sentMessage.sendCalls.take();
120     assertTrue(sentMessage.sendCalls.isEmpty());
121
122     // make sure the transaction sequence is retrieved and added to the message.
123
assertNotNull(gtxManager.getTransactionSequenceIDsCalls.poll(0));
124     assertNull(gtxManager.getTransactionSequenceIDsCalls.poll(0));
125     assertEquals(gtxManager.transactionSequenceIDs, sentMessage.setTransactionSequenceIDsCalls.poll(0));
126
127     // make sure that the manager set the expected object ids on the message...
128
assertEquals(objectIds, sentMessage.clientObjectIds);
129
130     // make sure that the manager called addAllOutstandingWaitersTo on the lock
131
// manager.
132
lockManager.addAllOutstandingWaitersToContexts.take();
133     assertTrue(lockManager.addAllOutstandingWaitersToContexts.isEmpty());
134
135     List JavaDoc waitContexts = new ArrayList JavaDoc(sentMessage.waitContexts);
136     assertEquals(1, waitContexts.size());
137     WaitContext wctxt = (WaitContext) waitContexts.get(0);
138     assertEquals(waitLockRequest.lockID(), wctxt.getLockID());
139     assertEquals(waitLockRequest.threadID(), wctxt.getThreadID());
140     assertEquals(waitLockRequest.getWaitInvocation(), wctxt.getWaitInvocation());
141
142     // make sure that the manager called addAllOutstandingLocksTo on the lock
143
// manager.
144
lockManager.addAllOutstandingLocksToContexts.take();
145     assertTrue(lockManager.addAllOutstandingLocksToContexts.isEmpty());
146     List JavaDoc lockContexts = new ArrayList JavaDoc(sentMessage.lockContexts);
147     assertEquals(1, lockContexts.size());
148     LockContext lctxt = (LockContext) lockContexts.get(0);
149     assertEquals(lockRequest.lockID(), lctxt.getLockID());
150     assertEquals(lockRequest.threadID(), lctxt.getThreadID());
151     assertEquals(lockRequest.lockLevel(), lctxt.getLockLevel());
152
153     // make sure that the manager called requestOutstanding() on the lock
154
// manager.
155
// assertEquals(1, lockManager.requestOutstandingContexts.size());
156
lockManager.requestOutstandingContexts.take();
157     assertTrue(lockManager.requestOutstandingContexts.isEmpty());
158
159     // make sure the lock manager isn't unpaused until after the ACK arrives
160
assertTrue(lockManager.unpauseContexts.isEmpty());
161     assertTrue(gtxManager.resendOutstandingCalls.isEmpty());
162
163     mgr.acknowledgeHandshake(0, 0, false, "1", new String JavaDoc[] {});
164
165     // make sure the remote object manager was told to requestOutstanding()
166
remoteObjectManager.requestOutstandingContexts.take();
167     assertTrue(remoteObjectManager.requestOutstandingContexts.isEmpty());
168
169     assertNotNull(lockManager.unpauseContexts.poll(1));
170     assertNotNull(gtxManager.resendOutstandingCalls.poll(1));
171   }
172
173   private static class TestRemoteObjectManager implements RemoteObjectManager {
174
175     public final NoExceptionLinkedQueue requestOutstandingContexts = new NoExceptionLinkedQueue();
176
177     public DNA retrieve(ObjectID id) {
178       throw new ImplementMe();
179     }
180
181     public ObjectID retrieveRootID(String JavaDoc name) {
182       throw new ImplementMe();
183     }
184
185     public void addRoot(String JavaDoc name, ObjectID id) {
186       throw new ImplementMe();
187     }
188
189     public void removed(ObjectID id) {
190       throw new ImplementMe();
191     }
192
193     public void requestOutstanding() {
194       requestOutstandingContexts.put(new Object JavaDoc());
195     }
196
197     public void pause() {
198       return;
199     }
200
201     public void unpause() {
202       return;
203     }
204
205     public void starting() {
206       return;
207     }
208
209     public void addAllObjects(SessionID sessionID, long batchID, Collection JavaDoc dnas) {
210       throw new ImplementMe();
211
212     }
213
214     public void clear() {
215       return;
216     }
217
218     public DNA retrieve(ObjectID id, int depth) {
219       throw new ImplementMe();
220     }
221
222   }
223
224   private static class TestClientHandshakeMessageFactory implements ClientHandshakeMessageFactory {
225
226     public TestClientHandshakeMessage message;
227     public final NoExceptionLinkedQueue newMessageQueue = new NoExceptionLinkedQueue();
228
229     public ClientHandshakeMessage newClientHandshakeMessage() {
230       newMessageQueue.put(message);
231       return message;
232     }
233
234   }
235
236   private static class TestClientLockManager implements ClientLockManager {
237
238     public List JavaDoc outstandingLockAwards = new LinkedList JavaDoc();
239     public List JavaDoc outstandingWaitLockRequests = new LinkedList JavaDoc();
240
241     public NoExceptionLinkedQueue requestOutstandingContexts = new NoExceptionLinkedQueue();
242     public NoExceptionLinkedQueue addAllOutstandingWaitersToContexts = new NoExceptionLinkedQueue();
243     public NoExceptionLinkedQueue addAllOutstandingLocksToContexts = new NoExceptionLinkedQueue();
244     public NoExceptionLinkedQueue pauseContexts = new NoExceptionLinkedQueue();
245     public NoExceptionLinkedQueue unpauseContexts = new NoExceptionLinkedQueue();
246
247     public void lock(LockID id, ThreadID threadID, int type) {
248       return;
249     }
250
251     public void unlock(LockID id, ThreadID threadID) {
252       return;
253     }
254
255     public void awardLock(SessionID sessionID, LockID id, ThreadID threadID, int type) {
256       return;
257     }
258
259     public LockID lockIDFor(String JavaDoc id) {
260       return null;
261     }
262
263     public void wait(LockID lockID, ThreadID threadID, WaitInvocation call, Object JavaDoc waitObject, WaitListener listener) {
264       return;
265     }
266
267     public Notify notify(LockID lockID, ThreadID threadID, boolean all) {
268       return Notify.NULL;
269     }
270
271     public Collection JavaDoc addAllPendingLockRequestsTo(Collection JavaDoc c) {
272       requestOutstandingContexts.put(new Object JavaDoc());
273       return c;
274     }
275
276     public void pause() {
277       pauseContexts.put(new Object JavaDoc());
278     }
279
280     public void starting() {
281       return;
282     }
283
284     public void unpause() {
285       unpauseContexts.put(new Object JavaDoc());
286     }
287
288     public boolean isStarting() {
289       return false;
290     }
291
292     public Collection JavaDoc addAllWaitersTo(Collection JavaDoc c) {
293       this.addAllOutstandingWaitersToContexts.put(c);
294       c.addAll(this.outstandingWaitLockRequests);
295       return c;
296     }
297
298     public Collection JavaDoc addAllHeldLocksTo(Collection JavaDoc c) {
299       this.addAllOutstandingLocksToContexts.put(c);
300       c.addAll(this.outstandingLockAwards);
301       return c;
302     }
303
304     public void notified(LockID lockID, ThreadID threadID) {
305       return;
306     }
307
308     public void recall(LockID lockID, ThreadID id, int level) {
309       return;
310     }
311
312     public void waitTimedOut(LockID lockID, ThreadID threadID) {
313       return;
314     }
315
316     public void runGC() {
317       return;
318     }
319
320     public boolean isLocked(LockID lockID) {
321       return false;
322     }
323
324     public int queueLength(LockID lockID, ThreadID threadID) {
325       throw new ImplementMe();
326     }
327
328     public int localHeldCount(LockID lockID, int lockLevel, ThreadID threadID) {
329       throw new ImplementMe();
330     }
331
332     public boolean isLocked(LockID lockID, ThreadID threadID) {
333       throw new ImplementMe();
334     }
335
336     public boolean haveLock(LockID lockID, TransactionID requesterID) {
337       throw new ImplementMe();
338     }
339
340     public void queryLockCommit(ThreadID threadID, GlobalLockInfo globalLockInfo) {
341       throw new ImplementMe();
342
343     }
344
345     public int waitLength(LockID lockID, ThreadID threadID) {
346       throw new ImplementMe();
347     }
348
349     public boolean tryLock(LockID id, ThreadID threadID, int type) {
350       throw new ImplementMe();
351     }
352
353     public void cannotAwardLock(SessionID sessionID, LockID id, ThreadID threadID, int type) {
354       throw new ImplementMe();
355
356     }
357   }
358
359   public class TestSequenceProvider implements BatchSequenceProvider {
360
361     long sequence = 1;
362
363     public synchronized void requestBatch(BatchSequenceReceiver receiver, int size) {
364       receiver.setNextBatch(sequence, sequence + size);
365       sequence += size;
366     }
367
368   }
369
370 }
371
Popular Tags