From c67fb8bb20f0c7dc968d07935115f4ef04a933e4 Mon Sep 17 00:00:00 2001 From: leventov Date: Mon, 8 Jun 2015 18:01:59 +0300 Subject: [PATCH] Conflicts: pom.xml --- pom.xml | 7 + .../set/Issue24ChronicleSetTest.java | 128 ++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/test/java/net/openhft/chronicle/set/Issue24ChronicleSetTest.java diff --git a/pom.xml b/pom.xml index e7e751ad5..a1189638c 100755 --- a/pom.xml +++ b/pom.xml @@ -159,6 +159,13 @@ 0.2 test + + + com.samskivert + samskivert + 1.9 + test + diff --git a/src/test/java/net/openhft/chronicle/set/Issue24ChronicleSetTest.java b/src/test/java/net/openhft/chronicle/set/Issue24ChronicleSetTest.java new file mode 100644 index 000000000..3b25c1996 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/set/Issue24ChronicleSetTest.java @@ -0,0 +1,128 @@ +/* + * Copyright 2015 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.set; + +import net.openhft.chronicle.hash.ChronicleHash; +import net.openhft.chronicle.hash.ChronicleHashBuilder; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static com.samskivert.util.CollectionUtil.selectRandomSubset; + +public class Issue24ChronicleSetTest { + + public static , K> + H init(B builder, int entrySize, int averageKeySize, String fileName) { + + String tmp = System.getProperty("java.io.tmpdir"); + String pathname = tmp + "/" + fileName; + File file = new File(pathname); + try { + H result = builder.entries(entrySize) + .averageKeySize(averageKeySize).createPersistedTo(file); + return result; + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } + } + + public synchronized static ChronicleSet initSet( + Class entryClass, int entrySize, int averageKeySize, String fileName) { + return init(ChronicleSetBuilder.of(entryClass), entrySize, averageKeySize, fileName); + } + + @Test + public void issue24ChronicleSetTest() { + ChronicleSet set = initSet(String.class, 1_000_000, 30, "stringSet.dat"); + ExecutorService executor = Executors.newFixedThreadPool(5); + for (int i = 0; i < 10; i++) { + Runnable worker = new WorkerThread(set); + executor.execute(worker); + } + executor.shutdown(); + while (!executor.isTerminated()) { + + } + System.out.println("Finished all threads"); + + + } + + public static class WorkerThread implements Runnable { + private final ChronicleSet set; + public WorkerThread(ChronicleSet set){ + this.set = set; + } + + @Override + public void run() { + System.out.println(Thread.currentThread().getName()+" Start. Command = " + set.size()); + processCommand(); + System.out.println(Thread.currentThread().getName()+" End."); + } + + private void processCommand() { + Set nomenclatures = new HashSet<>(); + for(int i = 0; i < 10; i++) { + String nomenclature = "#############################" + i; + nomenclatures.add(nomenclature); + + } + + set.addAll(nomenclatures); + + + Set strings = new HashSet<>( + selectRandomSubset(nomenclatures, nomenclatures.size() / 2)); + + set.addAll(strings); + + + Set toRemove = new HashSet<>(); + Random generator = new Random(); + for (int j = 0; j < 3; j++) { + int i = generator.nextInt(10); + String nomenclature = "#############################" + i; + toRemove.add(nomenclature); + } + set.removeAll(toRemove); + + for (String s : set) { + System.out.println(s); + } + + strings = new HashSet<>(selectRandomSubset( + nomenclatures, nomenclatures.size() / 2)); + + set.addAll(strings); + + for (String s : set) { + System.out.println(s); + } + } + + } +}