|
30 | 30 | import java.io.IOException; |
31 | 31 | import java.io.PrintStream; |
32 | 32 | import java.nio.ByteBuffer; |
| 33 | +import java.nio.charset.StandardCharsets; |
33 | 34 | import java.util.ArrayList; |
34 | 35 | import java.util.List; |
35 | 36 | import java.util.Scanner; |
36 | 37 | import java.util.regex.Matcher; |
37 | 38 | import java.util.regex.Pattern; |
38 | 39 | import org.apache.commons.io.FileUtils; |
39 | 40 | import org.apache.jute.BinaryOutputArchive; |
| 41 | +import org.apache.jute.Record; |
40 | 42 | import org.apache.zookeeper.KeeperException; |
| 43 | +import org.apache.zookeeper.ZooDefs; |
41 | 44 | import org.apache.zookeeper.test.ClientBase; |
| 45 | +import org.apache.zookeeper.txn.CheckVersionTxn; |
| 46 | +import org.apache.zookeeper.txn.CreateContainerTxn; |
| 47 | +import org.apache.zookeeper.txn.CreateTTLTxn; |
| 48 | +import org.apache.zookeeper.txn.CreateTxn; |
| 49 | +import org.apache.zookeeper.txn.DeleteTxn; |
42 | 50 | import org.apache.zookeeper.txn.ErrorTxn; |
43 | 51 | import org.apache.zookeeper.txn.MultiTxn; |
| 52 | +import org.apache.zookeeper.txn.SetDataTxn; |
44 | 53 | import org.apache.zookeeper.txn.Txn; |
45 | 54 | import org.junit.jupiter.api.AfterEach; |
46 | 55 | import org.junit.jupiter.api.BeforeEach; |
@@ -95,27 +104,31 @@ public void testInitMissingFile() throws FileNotFoundException, TxnLogToolkit.Tx |
95 | 104 |
|
96 | 105 | @Test |
97 | 106 | public void testMultiTxnDecode() throws IOException { |
98 | | - //MultiTxn with four ops, and the first op error. |
| 107 | + // MultiTxn with multi ops including errors |
99 | 108 | List<Txn> txns = new ArrayList<>(); |
100 | | - int type = -1; |
101 | | - for (int i = 0; i < 4; i++) { |
102 | | - ErrorTxn txn; |
103 | | - if (i == 0) { |
104 | | - txn = new ErrorTxn(KeeperException.Code.NONODE.intValue()); |
105 | | - } else { |
106 | | - txn = new ErrorTxn(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); |
107 | | - } |
108 | | - try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
109 | | - BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); |
110 | | - txn.serialize(boa, "request"); |
111 | | - ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); |
112 | | - txns.add(new Txn(type, bb.array())); |
113 | | - } |
114 | | - } |
| 109 | + txns.add(newSubTxn(ZooDefs.OpCode.error, new ErrorTxn(KeeperException.Code.NONODE.intValue()))); |
| 110 | + txns.add(newSubTxn(ZooDefs.OpCode.error, new ErrorTxn(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()))); |
| 111 | + txns.add(newSubTxn(ZooDefs.OpCode.create, new CreateTxn("/test", "test-data".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, true, 1))); |
| 112 | + txns.add(newSubTxn(ZooDefs.OpCode.createContainer, new CreateContainerTxn("/test_container", "test-data".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, 2))); |
| 113 | + txns.add(newSubTxn(ZooDefs.OpCode.createTTL, new CreateTTLTxn("/test_container", "test-data".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, 2, 20))); |
| 114 | + txns.add(newSubTxn(ZooDefs.OpCode.setData, new SetDataTxn("/test_set_data", "test-data".getBytes(StandardCharsets.UTF_8), 4))); |
| 115 | + txns.add(newSubTxn(ZooDefs.OpCode.delete, new DeleteTxn("/test_delete"))); |
| 116 | + txns.add(newSubTxn(ZooDefs.OpCode.check, new CheckVersionTxn("/test_check_version", 5))); |
115 | 117 | MultiTxn multiTxn = new MultiTxn(txns); |
116 | | - |
117 | 118 | String formattedTxnStr = TxnLogToolkit.getFormattedTxnStr(multiTxn); |
118 | | - assertEquals("error:-101;error:-2;error:-2;error:-2", formattedTxnStr); |
| 119 | + assertEquals("error:-101;error:-2;create:/test,test-data,[31,s{'world,'anyone}\n" |
| 120 | + + "],true,1;createContainer:/test_container,test-data,[31,s{'world,'anyone}\n" |
| 121 | + + "],2;createTTL:/test_container,test-data,[31,s{'world,'anyone}\n" |
| 122 | + + "],2,20;setData:/test_set_data,test-data,4;delete:/test_delete;check:/test_check_version,5", formattedTxnStr); |
| 123 | + } |
| 124 | + |
| 125 | + private static Txn newSubTxn(int type, Record record) throws IOException { |
| 126 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 127 | + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); |
| 128 | + record.serialize(boa, "request"); |
| 129 | + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); |
| 130 | + return new Txn(type, bb.array()); |
| 131 | + } |
119 | 132 | } |
120 | 133 |
|
121 | 134 | @Test |
|
0 commit comments