Author: frm
Date: Mon Jul 18 12:11:17 2016
New Revision: 1753213

URL: http://svn.apache.org/viewvc?rev=1753213&view=rev
Log:
OAK-4570 - Add failing test case

The test case proves that a compacted, base node state is not reused when
committing changes performed by a new builder to a node state belonging to an
older generation.

Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/NodeRecordTest.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/NodeRecordTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/NodeRecordTest.java?rev=1753213&r1=1753212&r2=1753213&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/NodeRecordTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/NodeRecordTest.java
 Mon Jul 18 12:11:17 2016
@@ -18,17 +18,36 @@
 package org.apache.jackrabbit.oak.segment;
 
 import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 
+import com.google.common.base.Supplier;
 import org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState;
 import org.apache.jackrabbit.oak.segment.file.FileStore;
 import org.apache.jackrabbit.oak.segment.file.FileStoreBuilder;
+import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
 public class NodeRecordTest {
 
+    private static class Generation implements Supplier<Integer> {
+
+        private int generation;
+
+        public void set(int generation) {
+            this.generation = generation;
+        }
+
+        @Override
+        public Integer get() {
+            return generation;
+        }
+
+    }
+
     @Rule
     public TemporaryFolder root = new TemporaryFolder();
 
@@ -68,6 +87,64 @@ public class NodeRecordTest {
         }
     }
 
+    @Test
+    @Ignore("OAK-4570")
+    public void baseNodeStateShouldBeReusedAcrossGenerations() throws 
Exception {
+        try (FileStore store = newFileStore()) {
+            Generation generation = new Generation();
+
+            // Create a new SegmentWriter. It's necessary not to have any 
cache,
+            // otherwise the write of some records (in this case, template
+            // records) will be cached and prevent this test to fail.
+
+            SegmentWriter writer = 
SegmentWriterBuilder.segmentWriterBuilder("test").withGeneration(generation).withWriterPool().withoutCache().build(store);
+
+            generation.set(1);
+
+            // Write a new node with a non trivial template. This record will
+            // belong to generation 1.
+
+            SegmentNodeState base = 
writer.writeNode(EmptyNodeState.EMPTY_NODE.builder().setProperty("k", 
"v1").getNodeState());
+            writer.flush();
+
+            generation.set(2);
+
+            // Compact that same record to generation 2.
+
+            SegmentNodeState compacted = writer.writeNode(base);
+            writer.flush();
+
+            // Assert that even if the two records have the same stable ID,
+            // their physical ID and the ID of their templates are different.
+
+            assertEquals(base.getStableId(), compacted.getStableId());
+            assertNotEquals(base.getRecordId(), compacted.getRecordId());
+            assertNotEquals(base.getTemplateId(), compacted.getTemplateId());
+
+            // Create a new builder from the base, pre-compaction node state.
+            // The base node state is from generation 1, but this builder will
+            // be from generation 2 because every builder in the pool is
+            // affected by the change of generation. Writing a node state from
+            // this builder should perform a partial compaction.
+
+            SegmentNodeState modified = (SegmentNodeState) 
base.builder().setProperty("k", "v2").getNodeState();
+
+            // Assert that the stable ID of this node state is different from
+            // the one in the base state. This is expected, since we have
+            // modified the value of a property.
+
+            assertNotEquals(modified.getStableId(), base.getStableId());
+            assertNotEquals(modified.getStableId(), compacted.getStableId());
+
+            // The node state should have reused the template from the 
compacted
+            // node state, since this template didn't change and the code 
should
+            // have detected that the base state of this builder was compacted
+            // to a new generation.
+
+            assertEquals(modified.getTemplateId(), compacted.getTemplateId());
+        }
+    }
+
     private boolean isRootRecord(SegmentNodeState sns) {
         Segment segment = sns.getRecordId().getSegment();
 


Reply via email to