1 5 package com.tc.objectserver.handler; 6 7 import com.tc.async.impl.MockSink; 8 import com.tc.async.impl.MockStage; 9 import com.tc.net.protocol.tcm.ChannelID; 10 import com.tc.object.dmi.DmiDescriptor; 11 import com.tc.object.dna.impl.ObjectStringSerializer; 12 import com.tc.object.lockmanager.api.LockID; 13 import com.tc.object.lockmanager.api.Notify; 14 import com.tc.object.lockmanager.api.ThreadID; 15 import com.tc.object.tx.TransactionID; 16 import com.tc.object.tx.TxnBatchID; 17 import com.tc.object.tx.TxnType; 18 import com.tc.objectserver.api.ObjectInstanceMonitor; 19 import com.tc.objectserver.context.ApplyTransactionContext; 20 import com.tc.objectserver.context.BroadcastChangeContext; 21 import com.tc.objectserver.core.api.ServerConfigurationContext; 22 import com.tc.objectserver.core.impl.TestServerConfigurationContext; 23 import com.tc.objectserver.gtx.TestGlobalTransactionManager; 24 import com.tc.objectserver.impl.ObjectInstanceMonitorImpl; 25 import com.tc.objectserver.lockmanager.api.NotifiedWaiters; 26 import com.tc.objectserver.lockmanager.api.TestLockManager; 27 import com.tc.objectserver.tx.NullTransactionalObjectManager; 28 import com.tc.objectserver.tx.ServerTransaction; 29 import com.tc.objectserver.tx.ServerTransactionImpl; 30 import com.tc.objectserver.tx.TestServerTransactionManager; 31 import com.tc.util.SequenceID; 32 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.LinkedList ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 import junit.framework.TestCase; 41 42 public class ApplyTransactionChangeHandlerTest extends TestCase { 43 44 private ApplyTransactionChangeHandler handler; 45 private ObjectInstanceMonitor instanceMonitor; 46 private TestServerTransactionManager transactionManager; 47 private TestGlobalTransactionManager gtxm; 48 private TestLockManager lockManager; 49 private MockSink broadcastSink; 50 51 public void setUp() throws Exception { 52 instanceMonitor = new ObjectInstanceMonitorImpl(); 53 transactionManager = new TestServerTransactionManager(); 54 lockManager = new TestLockManager(); 55 gtxm = new TestGlobalTransactionManager(); 56 handler = new ApplyTransactionChangeHandler(instanceMonitor, gtxm); 57 58 MockStage stageBo = new MockStage("Bo"); 59 MockStage stageCo = new MockStage("Co"); 60 broadcastSink = stageBo.sink; 61 TestServerConfigurationContext context = new TestServerConfigurationContext(); 62 context.transactionManager = transactionManager; 63 context.txnObjectManager = new NullTransactionalObjectManager(); 64 context.addStage(ServerConfigurationContext.BROADCAST_CHANGES_STAGE, stageBo); 65 context.addStage(ServerConfigurationContext.COMMIT_CHANGES_STAGE, stageCo); 66 context.lockManager = lockManager; 67 68 handler.initialize(context); 69 } 70 71 public void testLockManagerNotifyIsCalled() throws Exception { 72 TxnBatchID batchID = new TxnBatchID(1); 73 TransactionID txID = new TransactionID(1); 74 LockID[] lockIDs = new LockID[] { new LockID("1") }; 75 ChannelID channelID = new ChannelID(1); 76 List dnas = Collections.unmodifiableList(new LinkedList ()); 77 ObjectStringSerializer serializer = null; 78 Map newRoots = Collections.unmodifiableMap(new HashMap ()); 79 TxnType txnType = TxnType.NORMAL; 80 List notifies = new LinkedList (); 81 82 for (int i = 0; i < 10; i++) { 83 notifies.add(new Notify(new LockID("" + i), new ThreadID(i), i % 2 == 0)); 84 } 85 SequenceID sequenceID = new SequenceID(1); 86 ServerTransaction tx = new ServerTransactionImpl(batchID, txID, sequenceID, lockIDs, channelID, dnas, serializer, 87 newRoots, txnType, notifies, DmiDescriptor.EMPTY_ARRAY); 88 assertTrue(lockManager.notifyCalls.isEmpty()); 90 assertTrue(broadcastSink.queue.isEmpty()); 91 handler.handleEvent(getApplyTxnContext(tx)); 92 assertEquals(notifies.size(), lockManager.notifyCalls.size()); 94 lockManager.notifyCalls.clear(); 95 assertNotNull(broadcastSink.queue.remove(0)); 96 97 handler.handleEvent(getApplyTxnContext(tx)); 99 100 assertEquals(notifies.size(), lockManager.notifyCalls.size()); 101 NotifiedWaiters notifiedWaiters = null; 102 for (Iterator i = notifies.iterator(); i.hasNext();) { 103 Notify notify = (Notify) i.next(); 104 Object [] args = (Object []) lockManager.notifyCalls.remove(0); 105 assertEquals(notify.getLockID(), args[0]); 106 assertEquals(channelID, args[1]); 107 assertEquals(notify.getThreadID(), args[2]); 108 assertEquals(new Boolean (notify.getIsAll()), args[3]); 109 if (notifiedWaiters == null) { 110 notifiedWaiters = (NotifiedWaiters) args[4]; 111 } 112 assertNotNull(notifiedWaiters); 113 assertSame(notifiedWaiters, args[4]); 114 } 115 116 BroadcastChangeContext bctxt = (BroadcastChangeContext) broadcastSink.queue.remove(0); 118 assertNotNull(bctxt); 119 assertEquals(notifiedWaiters, bctxt.getNewlyPendingWaiters()); 120 } 121 122 private ApplyTransactionContext getApplyTxnContext(ServerTransaction txt) { 123 ApplyTransactionContext atc = new ApplyTransactionContext(txt, new HashMap ()); 124 return atc; 125 } 126 127 } 128 | Popular Tags |