SegStore: fix iterator caching in segment store.
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 31 Aug 2016 18:54:30 +0000 (14:54 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 7 Sep 2016 13:13:49 +0000 (09:13 -0400)
Fix bug 500607

Change-Id: Id1ae2c2176624ddab35ef541c4f34f036e361f3c
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/80158
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Tested-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Reviewed-by: Hudson CI
analysis/org.eclipse.tracecompass.analysis.timing.core.tests/src/org/eclipse/tracecompass/analysis/timing/core/tests/store/AbstractTestSegmentStore.java
analysis/org.eclipse.tracecompass.analysis.timing.core/src/org/eclipse/tracecompass/internal/analysis/timing/core/store/ArrayListStore.java

index c8ec8f8647673b374c38ee9edd16ba4c9972e3e4..179273cc8507c69320c955107068d42e1c062f22 100644 (file)
@@ -14,7 +14,10 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNull;
@@ -303,4 +306,54 @@ public abstract class AbstractTestSegmentStore {
         assertEquals(0, store.size());
     }
 
+    /**
+     * Test iterating over a store being built.
+     *
+     * bug 500607
+     */
+    @Test
+    public void testIterator() {
+        Collection<@NonNull ISegment> beforeExpected = ImmutableList.of(SEGMENT_2_6);
+        Collection<@NonNull ISegment> afterExpected = ImmutableList.of(SEGMENT_2_6, SEGMENT_4_8);
+        Collection<@NonNull ISegment> lastExpected = ImmutableList.of(SEGMENT_2_6, SEGMENT_4_8, SEGMENT_6_8);
+        Collection<@NonNull ISegment> fixture = new ArrayList<>();
+        ISegmentStore<@NonNull ISegment> store = getSegmentStore();
+
+        // Add one segment to the segment store and iterate
+        store.add(SEGMENT_2_6);
+        for (ISegment item : store) {
+            fixture.add(item);
+        }
+        assertEquals(beforeExpected, fixture);
+
+        // Add a second segment to the store and iterate
+        fixture.clear();
+        store.add(SEGMENT_4_8);
+        for (ISegment item : store) {
+            fixture.add(item);
+        }
+        assertEquals(afterExpected, fixture);
+
+        fixture.clear();
+        // Take an iterator
+        Iterator<@NonNull ISegment> iter = store.iterator();
+
+        // Add a third segment to the store and iterate
+        store.add(SEGMENT_6_8);
+        Iterator<@NonNull ISegment> iter2 = store.iterator();
+        fixture.clear();
+
+        // Make sure the first iterator take has only 2 elements and the second
+        // has 3 elements
+        while (iter.hasNext()) {
+            fixture.add(iter.next());
+        }
+        assertEquals(afterExpected, fixture);
+        fixture.clear();
+        while (iter2.hasNext()) {
+            fixture.add(iter2.next());
+        }
+        assertEquals(lastExpected, fixture);
+    }
+
 }
\ No newline at end of file
index b0a1638fcc23ad48ef5e266c668f66125faa3868..9537fb403ab5879197e8f55448a2770846b580a3 100644 (file)
@@ -121,6 +121,7 @@ public class ArrayListStore<@NonNull E extends ISegment> implements ISegmentStor
             for (int i = size() - 1; i > 0 && COMPARATOR.compare(val, fStore.get(i - 1)) < 0; i--) {
                 Collections.swap(fStore, i, i - 1);
             }
+            fLastSnapshot = null;
             return true;
         } finally {
             fLock.writeLock().unlock();
This page took 0.025789 seconds and 5 git commands to generate.