/* * Copyright (C) 2015 higherfrequencytrading.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ package net.openhft.chronicle.map; import org.junit.Test; import static org.junit.Assert.assertNull; public class NestedContextsInIterationContextTest { @Test public void testNestedGetsInIterationContextAllowed() { try (ChronicleMap map = ChronicleMapBuilder .of(Integer.class, Integer.class) .entries(100) .create()) { map.put(42, 42); map.forEachEntry(e -> assertNull(map.get(0))); } } @Test(expected = IllegalStateException.class) public void testNestedPutsInIterationContextForbidden() { try (ChronicleMap map = ChronicleMapBuilder .of(Integer.class, Integer.class) .entries(100) .create()) { map.put(42, 42); map.forEachEntry(e -> map.put(0, 0)); } } @Test public void testNestedUpdatesDifferentSegmentInIterationContextForbidden() { try (ChronicleMap map = ChronicleMapBuilder .of(Integer.class, Integer.class) .entries(100) .actualSegments(2) .create()) { map.put(42, 42); int keySegmentIndex = map.queryContext(42).segmentIndex(); try (MapSegmentContext cxt = map.segmentContext(keySegmentIndex)) { cxt.forEachSegmentEntry(e -> { int k = 0; ExternalMapQueryContext nestedCxt; // try lock different segment while ((nestedCxt = map.queryContext(k)).segmentIndex() == keySegmentIndex) { k++; } try (ExternalMapQueryContext nestedC = nestedCxt) { nestedC.updateLock().lock(); } catch (IllegalStateException ex) { // expected // try lock the same segment while ((nestedCxt = map.queryContext(k)).segmentIndex() != keySegmentIndex) { k++; } try (ExternalMapQueryContext nestedC2 = nestedCxt) { nestedC2.updateLock().lock(); } catch (IllegalStateException ex2) { // expected return; } } throw new AssertionError("expected " + IllegalStateException.class); }); } } } @Test public void testNestedWritesDifferentSegmentInIterationContextForbidden() { try (ChronicleMap map = ChronicleMapBuilder .of(Integer.class, Integer.class) .entries(100) .actualSegments(2) .create()) { map.put(42, 42); int keySegmentIndex = map.queryContext(42).segmentIndex(); try (MapSegmentContext cxt = map.segmentContext(keySegmentIndex)) { cxt.forEachSegmentEntry(e -> { int k = 0; ExternalMapQueryContext nestedCxt; // try lock different segment while ((nestedCxt = map.queryContext(k)).segmentIndex() == keySegmentIndex) { k++; } try (ExternalMapQueryContext nestedC = nestedCxt) { nestedC.writeLock().lock(); } catch (IllegalStateException ex) { // expected // try lock the same segment while ((nestedCxt = map.queryContext(k)).segmentIndex() != keySegmentIndex) { k++; } try (ExternalMapQueryContext nestedC2 = nestedCxt) { nestedC2.writeLock().lock(); } catch (IllegalStateException ex2) { // expected return; } } throw new AssertionError("expected " + IllegalStateException.class); }); } } } @Test public void testNestedReadDifferentSegmentInIterationContextAllowed() { try (ChronicleMap map = ChronicleMapBuilder .of(Integer.class, Integer.class) .entries(100) .actualSegments(2) .create()) { map.put(42, 42); int keySegmentIndex = map.queryContext(42).segmentIndex(); try (MapSegmentContext cxt = map.segmentContext(keySegmentIndex)) { cxt.forEachSegmentEntry(e -> { int k = 0; ExternalMapQueryContext nestedCxt; // try lock different segment while ((nestedCxt = map.queryContext(k)).segmentIndex() == keySegmentIndex) { k++; } try (ExternalMapQueryContext nestedC = nestedCxt) { nestedC.readLock().lock(); } // try lock the same segment while ((nestedCxt = map.queryContext(k)).segmentIndex() != keySegmentIndex) { k++; } try (ExternalMapQueryContext nestedC = nestedCxt) { nestedC.readLock().lock(); } }); } } } @Test(expected = IllegalStateException.class) public void testNestedIterationInIterationContextForbidden() { try (ChronicleMap map = ChronicleMapBuilder .of(Integer.class, Integer.class) .entries(100) .create()) { map.put(42, 42); try (MapSegmentContext cxt = map.segmentContext(0)) { cxt.forEachSegmentEntry(e -> map.segmentContext(1).forEachSegmentEntry(e2 -> {})); } } } }