1 package com.sleepycat.je.rep.txn; 2 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertTrue; 5 import static org.junit.Assert.fail; 6 7 import java.io.ByteArrayInputStream; 8 import java.io.ByteArrayOutputStream; 9 import java.io.File; 10 import java.io.IOException; 11 import java.io.ObjectInputStream; 12 import java.io.ObjectOutputStream; 13 import java.util.UUID; 14 15 import org.junit.Test; 16 17 import com.sleepycat.je.CommitToken; 18 import com.sleepycat.je.Database; 19 import com.sleepycat.je.DatabaseConfig; 20 import com.sleepycat.je.Transaction; 21 import com.sleepycat.je.rep.ReplicatedEnvironment; 22 import com.sleepycat.je.rep.utilint.RepTestUtils; 23 import com.sleepycat.je.rep.utilint.RepTestUtils.RepEnvInfo; 24 import com.sleepycat.util.test.SharedTestUtils; 25 import com.sleepycat.util.test.TestBase; 26 27 public class CommitTokenTest extends TestBase { 28 29 private final File envRoot; 30 CommitTokenTest()31 public CommitTokenTest() { 32 envRoot = SharedTestUtils.getTestDir(); 33 } 34 35 @Test testBasic()36 public void testBasic() 37 throws IOException, ClassNotFoundException { 38 39 UUID repenvUUID = UUID.randomUUID(); 40 41 CommitToken t1 = new CommitToken(repenvUUID, 1); 42 CommitToken t2 = new CommitToken(repenvUUID, 2); 43 CommitToken t3 = new CommitToken(repenvUUID, 3); 44 45 assertTrue((t1.compareTo(t2) < 0) && (t2.compareTo(t1) > 0)); 46 assertTrue((t2.compareTo(t3) < 0) && (t3.compareTo(t2) > 0)); 47 assertTrue((t1.compareTo(t3) < 0) && (t3.compareTo(t1) > 0)); 48 49 assertEquals(t1, new CommitToken(repenvUUID, 1)); 50 assertEquals(0, t1.compareTo(new CommitToken(repenvUUID, 1))); 51 52 try { 53 t1.compareTo(new CommitToken(UUID.randomUUID(), 1)); 54 fail("Expected exception"); 55 } catch (IllegalArgumentException ie) { 56 // expected 57 } 58 59 /* test serialization/de-serialization. */ 60 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 61 ObjectOutputStream oos = new ObjectOutputStream(baos); 62 oos.writeObject(t1); 63 ByteArrayInputStream bais = 64 new ByteArrayInputStream(baos.toByteArray()); 65 ObjectInputStream ois = new ObjectInputStream(bais); 66 CommitToken t11 = (CommitToken)ois.readObject(); 67 68 assertEquals(t1, t11); 69 } 70 71 /** 72 * Make sure that we only return a commit token when we've done real work. 73 */ 74 @Test testCommitTokenFailures()75 public void testCommitTokenFailures() 76 throws IOException { 77 78 RepEnvInfo[] repEnvInfo = RepTestUtils.setupEnvInfos(envRoot, 1); 79 ReplicatedEnvironment master = RepTestUtils.joinGroup(repEnvInfo); 80 81 /* It's illegal to get a commit token before it has closed. */ 82 Transaction txn = master.beginTransaction(null, null); 83 try { 84 txn.getCommitToken(); 85 fail("Should have gotten IllegalStateException"); 86 } catch (IllegalStateException expected) { 87 /* expected outcome. */ 88 } 89 90 /* 91 * Now abort and try again. Simce this transaction has done no writing 92 * the commit token should be null. 93 */ 94 txn.abort(); 95 CommitToken token = txn.getCommitToken(); 96 assertTrue(token == null); 97 98 /* 99 * A committed txn that has done no writing should also return a null 100 * commit token. 101 */ 102 txn = master.beginTransaction(null, null); 103 txn.commit(); 104 token = txn.getCommitToken(); 105 assertTrue(token == null); 106 107 /* 108 * A committed txn that has done a write should return a non-null 109 * token. 110 */ 111 txn = master.beginTransaction(null, null); 112 DatabaseConfig dbConfig = new DatabaseConfig(); 113 dbConfig.setTransactional(true); 114 dbConfig.setAllowCreate(true); 115 Database db = master.openDatabase(txn, "foo", dbConfig); 116 db.close(); 117 txn.commit(); 118 token = txn.getCommitToken(); 119 assertTrue(token != null); 120 121 RepTestUtils.shutdownRepEnvs(repEnvInfo); 122 } 123 } 124