1 5 package com.tc.objectserver.persistence.impl; 6 7 import com.tc.exception.ImplementMe; 8 import com.tc.objectserver.api.TestSink; 9 import com.tc.objectserver.persistence.api.PersistentSequence; 10 import com.tc.objectserver.persistence.impl.PersistentBatchSequenceProvider.GlobalTransactionIDBatchRequestContext; 11 import com.tc.test.TCTestCase; 12 import com.tc.util.Assert; 13 import com.tc.util.concurrent.NoExceptionLinkedQueue; 14 import com.tc.util.sequence.BatchSequenceReceiver; 15 16 public class PersistentBatchSequenceProviderTest extends TCTestCase { 17 18 public void tests() throws Exception { 19 TestPersistentSequence persistentSequence = new TestPersistentSequence(); 20 TestSink requestBatchSink = new TestSink(); 21 22 PersistentBatchSequenceProvider provider = new PersistentBatchSequenceProvider(persistentSequence); 23 provider.setRequestBatchSink(requestBatchSink); 24 25 TestBatchSequenceReceiver receiver = new TestBatchSequenceReceiver(); 26 27 int batchSize = 5; 28 provider.requestBatch(receiver, batchSize); 30 GlobalTransactionIDBatchRequestContext ctxt = (GlobalTransactionIDBatchRequestContext) requestBatchSink 31 .getInternalQueue().remove(0); 32 assertTrue(requestBatchSink.getInternalQueue().isEmpty()); 33 assertSame(receiver, ctxt.getReceiver()); 34 assertEquals(batchSize, ctxt.getBatchSize()); 35 36 provider.handleEvent(ctxt); 38 39 Object [] args = (Object []) persistentSequence.nextBatchQueue.poll(1); 41 assertEquals(new Integer (batchSize), args[0]); 42 43 args = (Object []) receiver.nextBatchQueue.poll(1); 45 assertEquals(new Long (persistentSequence.sequence - batchSize), args[0]); 46 assertEquals(new Long (persistentSequence.sequence), args[1]); 47 } 48 49 private static final class TestBatchSequenceReceiver implements BatchSequenceReceiver { 50 51 public final NoExceptionLinkedQueue nextBatchQueue = new NoExceptionLinkedQueue(); 52 53 public void setNextBatch(long start, long end) { 54 nextBatchQueue.put(new Object [] { new Long (start), new Long (end) }); 55 } 56 57 public boolean hasNext() { 58 return true; 59 } 60 61 } 62 63 private static final class TestPersistentSequence implements PersistentSequence { 64 65 public long sequence = 1; 66 public final NoExceptionLinkedQueue nextBatchQueue = new NoExceptionLinkedQueue(); 67 68 public long next() { 69 return sequence; 70 } 71 72 public long nextBatch(int batchSize) { 73 nextBatchQueue.put(new Object [] { new Integer (batchSize) }); 74 long ls = sequence; 75 sequence += batchSize; 76 return ls; 77 } 78 79 public String getUID() { 80 throw new ImplementMe(); 81 } 82 83 public void setNext(long next) { 84 Assert.assertTrue(this.sequence <= next); 85 sequence = next; 86 } 87 88 } 89 90 } 91 | Popular Tags |