From e8245cd62468fc0f5c8928f0e14bfda317f191e3 Mon Sep 17 00:00:00 2001 From: leventov Date: Fri, 5 Jun 2015 17:06:49 +0300 Subject: [PATCH] Add SanityCheck --- .../map/ChronicleMapSanityCheckTest.java | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/test/java/net/openhft/chronicle/map/ChronicleMapSanityCheckTest.java diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapSanityCheckTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapSanityCheckTest.java new file mode 100644 index 000000000..2706a4b08 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapSanityCheckTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.openhft.chronicle.map; + +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.Random; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.LockSupport; + +/** + * Created by Borislav Ivanov on 5/29/15. + */ +public class ChronicleMapSanityCheckTest { + + enum DummyValue { + DUMMY_VALUE + } + + @Test + public void testSanity1() throws IOException, InterruptedException + { + + String tmp = System.getProperty("java.io.tmpdir"); + + String pathname = tmp + "/testSanity1-" + UUID.randomUUID().toString() + ".dat"; + + File file = new File(pathname); + + System.out.println("Starting sanity test 1. Chronicle file :" + + file.getAbsolutePath().toString()); + + ScheduledExecutorService producerExecutor = + Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() - 1); + + ScheduledExecutorService consumerExecutor = + Executors.newSingleThreadScheduledExecutor(); + + int N = 1000; + + int producerPeriod = 100; + TimeUnit producerTimeUnit = TimeUnit.MILLISECONDS; + + int consumerPeriod = 100; + TimeUnit consumerTimeUnit = TimeUnit.MILLISECONDS; + + int totalTestTimeMS = (consumerPeriod + producerPeriod) * 20; + + try (ChronicleMap map = + ChronicleMapBuilder.of(String.class, DummyValue.class) + .entries(N) + .createPersistedTo(file)) { + + map.clear(); + + producerExecutor.scheduleAtFixedRate(() -> { + + Thread.currentThread().setName("Producer " + Thread.currentThread().getId()); + Random r = new Random(); + + System.out.println("Before PRODUCING size is " + map.size()); + for (int i = 0; i < N; i++) { + LockSupport.parkNanos(r.nextInt(5)); + map.put(String.valueOf(i), DummyValue.DUMMY_VALUE); + } + System.out.println("After PRODUCING size is " + map.size()); + + }, 0, producerPeriod, producerTimeUnit); + + consumerExecutor.scheduleAtFixedRate(() -> { + + Thread.currentThread().setName("Consumer"); + Set keys = map.keySet(); + + Random r = new Random(); + + System.out.println("Before CONSUMING size is " + map.size()); + System.out.println(); + for (String key : keys) { + if (r.nextBoolean()) { + map.remove(key); + } + } + + System.out.println("After CONSUMING size is " + map.size()); + + }, 0, consumerPeriod, consumerTimeUnit); + + Thread.sleep(totalTestTimeMS); + + consumerExecutor.shutdown(); + producerExecutor.shutdown(); + } + + } + +}