/** * Runs given code in multiple threads and synchronously waits for all threads to complete. * If any thread failed, exception will be thrown out of this method. * * @param c Callable. * @param threadNum Thread number. * @param threadName Thread name. * @throws Exception If failed. */ protected void multithreaded(Callable<?> c, int threadNum, String threadName) throws Exception { GridTestUtils.runMultiThreaded(c, threadNum, threadName); }
/** * Runs given code in multiple threads and synchronously waits for all * threads to complete. If any thread failed, exception will be thrown * out of this method. * * @param r Runnable. * @param threadNum Thread number. * @param threadName Thread name. * @throws Exception If failed. */ protected void multithreaded(Runnable r, int threadNum, String threadName) throws Exception { GridTestUtils.runMultiThreaded(r, threadNum, threadName); }
/** * Runs callable object in specified number of threads. * * @param call Callable. * @param threadNum Number of threads. * @param threadName Thread names. * @return Execution time in milliseconds. * @throws Exception If failed. */ public static long runMultiThreaded(Callable<?> call, int threadNum, String threadName) throws Exception { List<Callable<?>> calls = Collections.<Callable<?>>nCopies(threadNum, call); return runMultiThreaded(calls, threadName); }
/** * @param call Closure that receives thread index. * @param threadNum Number of threads. * @param threadName Thread names. * @return Execution time in milliseconds. * @throws Exception If failed. */ public static long runMultiThreaded(final IgniteInClosure<Integer> call, int threadNum, String threadName) throws Exception { List<Callable<?>> calls = new ArrayList<>(threadNum); for (int i = 0; i < threadNum; i++) { final int idx = i; calls.add(new Callable<Void>() { @Override public Void call() throws Exception { call.apply(idx); return null; } }); } return runMultiThreaded(calls, threadName); }
/** * Runs runnable object in specified number of threads. * * @param run Target runnable. * @param threadNum Number of threads. * @param threadName Thread name. * @return Execution time in milliseconds. * @throws Exception Thrown if at least one runnable execution failed. */ public static long runMultiThreaded(Runnable run, int threadNum, String threadName) throws Exception { return runMultiThreaded(makeCallable(run, null), threadNum, threadName); }
/** * Runs callable tasks each in separate threads. * * @param calls Callable tasks. * @param threadName Thread name. * @return Execution time in milliseconds. * @throws Exception If failed. */ public static long runMultiThreaded(Iterable<Callable<?>> calls, String threadName) throws Exception { return runMultiThreaded(calls, new GridTestSafeThreadFactory(threadName)); }
/** * On linux NetworkInterface.getHardwareAddress() returns null from time to time. * * @throws Exception If failed. */ @Test public void testAllLocalMACsMultiThreaded() throws Exception { GridTestUtils.runMultiThreaded(new Runnable() { @Override public void run() { for (int i = 0; i < 30; i++) { Collection<String> macs = U.allLocalMACs(); assertTrue("Mac address are not defined.", !macs.isEmpty()); } } }, 32, "thread"); }
/** * Performs operation, measuring it's time. * * @param c Operation to perform. * @param threadCnt Number of parallel threads to perform operation (>= 1). * @return Number of milliseconds, in which the operation was completed. * @throws Exception If provided callable has thrown exception. */ public static long measureTime(Callable<Object> c, int threadCnt) throws Exception { A.ensure(threadCnt >= 1, "threadCnt should be >= 1"); long start = System.currentTimeMillis(); if (threadCnt == 1) c.call(); else GridTestUtils.runMultiThreaded(c, threadCnt, "test-worker"); return System.currentTimeMillis() - start; }
/** * Test concurrent schema creation and destruction. * * @throws Exception If failed. */ @Test public void testCustomSchemaConcurrentUse() throws Exception { final AtomicInteger maxIdx = new AtomicInteger(); GridTestUtils.runMultiThreaded(new Runnable() { @Override public void run() { for (int i = 0; i < 100; i++) { int idx = maxIdx.incrementAndGet(); String tbl = "Person" + idx; IgniteCache<Long, Person> cache = registerQueryEntity(tbl, "PersonCache" + idx); testQueryEntity(cache, tbl); cache.destroy(); } } }, 4, "schema-test"); }
/** * @param threads Threads number. * @param checkBalance Check balance flag. * @param time Execution time. * @throws Exception If failed. */ private void runQueries(int threads, final boolean checkBalance, final long time) throws Exception { final Ignite ignite = grid(0); GridTestUtils.runMultiThreaded(new Callable<Object>() { @Override public Object call() { long endTime = System.currentTimeMillis() + time; while (System.currentTimeMillis() < endTime) { ScanQueryBroadcastClosure c = new ScanQueryBroadcastClosure(partitionsMap, checkBalance); ignite.compute().broadcast(c); } return null; } }, threads, "test-thread"); }
/** * @throws Exception If test fails. */ @Test public void testBasicLocks() throws Throwable { GridTestUtils.runMultiThreaded(new Callable<Object>() { /** {@inheritDoc} */ @Override public Object call() throws Exception { Lock lock = cache.lock(1); assert lock.tryLock(1000L, TimeUnit.MILLISECONDS); info("Locked key from thread: " + thread()); Thread.sleep(50); info("Unlocking key from thread: " + thread()); lock.unlock(); info("Unlocked key from thread: " + thread()); return null; } }, 10, "basic-lock-thread"); }
/** * @throws Exception If failed. */ @Test public void test() throws Exception { final AtomicInteger nodeIdx = new AtomicInteger(); GridTestUtils.runMultiThreaded(new Runnable() { @Override public void run() { try { startGrid(nodeIdx.getAndIncrement()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } }, 10, "auth-grid-starter"); } }
/** * @throws Exception If failed. */ @Test public void testMultithreadedPutAll() throws Exception { runMultiThreaded(new Callable<Object>() { private final Random rnd = new Random(); @Override public Object call() throws Exception { for (int i = 0; i < TX_CNT; i++) { Map<Integer, String> map = new TreeMap<>(); for (int j = 0; j < 10; j++) map.put(j, "value"); IgniteCache<Object, Object> cache = jcache(rnd.nextInt(GRID_CNT)); cache.putAll(map); } return null; } }, 8, "putAll"); checkOpenedClosedCount(); }
/** * @throws Exception If failed. */ @Test public void testMetadataUpdate() throws Exception { startGrid(0); GridTestUtils.runMultiThreaded(new Callable<Void>() { @SuppressWarnings("deprecation") @Override public Void call() throws Exception { ignite(0).configuration().getMarshaller().marshal(new C1()); ignite(0).configuration().getMarshaller().marshal(new C2()); return null; } }, 64, "marshal"); }
/** * @throws Exception if failed. */ @Test public void testMultiThreadedMaxSize() throws Exception { final int maxSize = 10; registry.setMaxSize(maxSize); GridTestUtils.runMultiThreaded(new Callable<Object>() { @Override public Object call() throws Exception { for (int i = 0; i < maxSize; i++) registry.onException("Test " + i, new Exception("test")); return null; } }, 10, "TestSetMaxSize"); int size = registry.getErrors(0).size(); assert maxSize + 1 >= size && maxSize - 1 <= size; } }
/** * @throws Exception If test failed */ @Test public void testMultiThreaded() throws Exception { GridTestUtils.runMultiThreaded(new Callable<Object>() { @Override public Object call() throws Exception { for (int i = 0; i < 100000; i++) getSpi().record(new DiscoveryEvent(null, "Test event", 1, null)); return null; } }, 10, "event-thread"); Collection<Event> evts = getSpi().localEvents(F.<Event>alwaysTrue()); info("Events count in memory: " + evts.size()); assert evts.size() <= 10000 : "Incorrect number of events: " + evts.size(); } }
/** * @throws Exception If failed. */ @Test public void testGetOrCreateMultiNodeTemplate() throws Exception { final AtomicInteger idx = new AtomicInteger(); GridTestUtils.runMultiThreaded(new Callable<Object>() { @Override public Object call() throws Exception { int idx0 = idx.getAndIncrement(); ignite(idx0 % nodeCount()).getOrCreateCache(DYNAMIC_CACHE_NAME); return null; } }, nodeCount() * 4, "runner"); ignite(0).destroyCache(DYNAMIC_CACHE_NAME); }
/** * @throws Exception If failed. */ @Test public void testJoinMutlithreaded() throws Exception { startServerNodes(1); final int CLIENTS = 30; clientsPerSrv = CLIENTS; GridTestUtils.runMultiThreaded(new Callable<Void>() { @Override public Void call() throws Exception { Ignite g = startGrid("client-" + clientIdx.getAndIncrement()); clientNodeIds.add(g.cluster().localNode().id()); return null; } }, CLIENTS, "start-client"); checkNodes(1, CLIENTS); }
/** * @param testName Test name. * @param q Queue. * @throws Exception If failed. */ private static void testBlockingQueueMultithreaded(String testName, final BlockingQueue<Object> q) throws Exception { waitGc(); X.println(">>> Starting test for: " + testName); final LongAdder adder = new LongAdder(); GridTestUtils.runMultiThreaded(new Callable<Object>() { @Override public Object call() throws Exception { adder.add(testBlockingQueue(q)); return null; } }, THREAD_CNT, "queue-test-worker"); X.println(">>> Tested queue [testName=" + testName + ", dur=" + adder.sum() + "ms]"); assert q.isEmpty(); }
/** * @throws Exception If failed. */ @Test public void testStartStop2() throws Exception { startGridsMultiThreaded(10, false); GridTestUtils.runMultiThreaded((IgniteInClosure<Integer>)this::stopGrid, 3, "stop-node-thread"); waitForTopology(7); startGridsMultiThreaded(0, 3); waitForTopology(10); }