From 24e6e5e515b66ead85d747ae25663278c43d8895 Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Mon, 2 Feb 2015 16:15:43 +0000 Subject: [PATCH] focusing on using autoClose --- .../chronicle/map/ChronicleMapTest.java | 15 +++-- .../chronicle/map/EntryCountMapTest.java | 28 +++++---- .../chronicle/map/LargeEntriesTest.java | 59 ++++++++++--------- .../TCPSocketReplicationBootStrapTest.java | 6 +- 4 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java index 6910daff5..2e12793ce 100755 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java @@ -203,14 +203,17 @@ public class ChronicleMapTest { final File persistenceFile = Builder.getPersistenceFile(); for (int i = 0; i < 2; i++) { - ChronicleMap map = ChronicleMapBuilder.of(byte[].class, byte[] - .class).createPersistedTo(persistenceFile); + try (ChronicleMap map = ChronicleMapBuilder.of(byte[].class, byte[] + .class).createPersistedTo(persistenceFile)) { - byte[] o = map.get("hello".getBytes()); - System.out.println(o == null ? "null" : new String(o)); - map.put("hello".getBytes(), "world".getBytes()); + byte[] o = map.get("hello".getBytes()); + System.out.println(o == null ? "null" : new String(o)); + map.put("hello".getBytes(), "world".getBytes()); + } } + persistenceFile.delete(); + } @@ -1880,7 +1883,9 @@ public class ChronicleMapTest { assertEquals(2, value1.getValue()); } } + tmpFile.delete(); } + } diff --git a/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java b/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java index 0df11a3cf..6a232599a 100644 --- a/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java @@ -190,24 +190,26 @@ public class EntryCountMapTest { Random random = new Random(seed); int counter = minSize + random.nextInt(9999 + maxSize); final int stride = 1 + random.nextInt(100); - int maxKeySize = "key:".length() + + final int maxKeySize = "key:".length() + (int) round(log10(moreThanMaxSize(maxSize) * stride + counter)) + 1; - File f = null; - try ( final ChronicleMap map = - getSharedMap(minSize, segments, maxKeySize)) { - f = map.file(); - return es.submit(new Callable() { - @Override - public Void call() throws Exception { + + return es.submit(new Callable() { + @Override + public Void call() throws Exception { + File f = null; + try (final ChronicleMap map = + getSharedMap(minSize, segments, maxKeySize)) { + f = map.file(); testEntriesMaxSize0(segments, minSize, maxSize, seed, stride, map); + } finally { + if (f != null && f.exists()) + f.delete(); return null; } - }); - } finally { - if (f != null && f.exists()) - f.delete(); - } + } + }); + } void testEntriesMaxSize(int segments, int minSize, int maxSize, int seed) throws IOException { diff --git a/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java b/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java index 338d18dc9..1e83c5d33 100644 --- a/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java +++ b/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java @@ -48,7 +48,7 @@ public class LargeEntriesTest { File file = File.createTempFile("largeEntries" + System.currentTimeMillis(), ".deleteme"); file.deleteOnExit(); - final ChronicleMap map = ChronicleMapBuilder + try (final ChronicleMap map = ChronicleMapBuilder .of(String.class, String.class) .valueMarshaller(SnappyStringMarshaller.INSTANCE) .actualSegments(1) // to force an error. @@ -56,37 +56,38 @@ public class LargeEntriesTest { .averageKeySize(10) .averageValueSize(ENTRY_SIZE) .putReturnsNull(true) - .createPersistedTo(file); - { - warmUpCompression(ENTRY_SIZE); - int threads = 4; //Runtime.getRuntime().availableProcessors(); - ExecutorService es = Executors.newFixedThreadPool(threads); - final int block = ENTRIES / threads; - for (int i = 0; i < 3; i++) { - long start = System.currentTimeMillis(); - List> futureList = new ArrayList<>(); - for (int t = 0; t < threads; t++) { - final int finalT = t; - futureList.add(es.submit(new Runnable() { - @Override - public void run() { - exerciseLargeStrings(map, finalT * block, finalT * block + block, - ENTRY_SIZE); - } - })); + .createPersistedTo(file)) { + { + warmUpCompression(ENTRY_SIZE); + int threads = 4; //Runtime.getRuntime().availableProcessors(); + ExecutorService es = Executors.newFixedThreadPool(threads); + final int block = ENTRIES / threads; + for (int i = 0; i < 3; i++) { + long start = System.currentTimeMillis(); + List> futureList = new ArrayList<>(); + for (int t = 0; t < threads; t++) { + final int finalT = t; + futureList.add(es.submit(new Runnable() { + @Override + public void run() { + exerciseLargeStrings(map, finalT * block, finalT * block + block, + ENTRY_SIZE); + } + })); + } + for (Future future : futureList) { + future.get(); + } + long time = System.currentTimeMillis() - start; + long operations = 3; + System.out.printf("Put/Get rate was %.1f MB/s%n", + operations * ENTRIES * ENTRY_SIZE / 1e6 / (time / 1e3)); } - for (Future future : futureList) { - future.get(); - } - long time = System.currentTimeMillis() - start; - long operations = 3; - System.out.printf("Put/Get rate was %.1f MB/s%n", - operations * ENTRIES * ENTRY_SIZE / 1e6 / (time / 1e3)); + es.shutdown(); + } - es.shutdown(); - if (es.isTerminated()) - map.close(); } + file.delete(); } private void warmUpCompression(int entrySize) { diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java index d68078dd9..adbfe3872 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java @@ -55,8 +55,9 @@ public class TCPSocketReplicationBootStrapTest { ChronicleMapBuilder map2aBuilder = newTcpSocketShmBuilder(Integer.class, CharSequence.class, (byte) 2, 8092, new InetSocketAddress("localhost", 8091)); + File persistenceFile = getPersistenceFile(); final ChronicleMap map2a = - map2aBuilder.createPersistedTo(getPersistenceFile()); + map2aBuilder.createPersistedTo(persistenceFile); map2a.put(10, "EXAMPLE-10"); // this will be the last time that map1 go an update from map2 long lastModificationTime; @@ -85,6 +86,9 @@ public class TCPSocketReplicationBootStrapTest { // add data into it waitTillEqual(5000); assertEquals("ADDED WHEN DISCONNECTED TO MAP1", map1.get(11)); + + map2.file().delete(); + persistenceFile.delete(); } @Test