@Test public void valuesToArray() { HazelcastClient hClient = getHazelcastClient(); IMap map = hClient.getMap("valuesToArray"); assertEquals(0, map.size()); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); assertEquals(3, map.size()); { final Object[] values = map.values().toArray(); Arrays.sort(values); assertArrayEquals(new Object[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[3]); Arrays.sort(values); assertArrayEquals(new String[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[2]); Arrays.sort(values); assertArrayEquals(new String[] {"1", "2", "3"}, values); } { final String[] values = (String[]) map.values().toArray(new String[5]); Arrays.sort(values, 0, 3); assertArrayEquals(new String[] {"1", "2", "3", null, null}, values); } }
/** @return Client configuration for the test. */ protected GridClientConfiguration clientConfiguration() throws GridClientException { GridClientConfiguration cfg = new GridClientConfiguration(); cfg.setBalancer(getBalancer()); cfg.setTopologyRefreshFrequency(TOP_REFRESH_FREQ); cfg.setProtocol(protocol()); cfg.setServers(Arrays.asList(serverAddress())); cfg.setSslContextFactory(sslContextFactory()); GridClientDataConfiguration loc = new GridClientDataConfiguration(); GridClientDataConfiguration partitioned = new GridClientDataConfiguration(); partitioned.setName(PARTITIONED_CACHE_NAME); partitioned.setAffinity(new GridClientPartitionAffinity()); GridClientDataConfiguration replicated = new GridClientDataConfiguration(); replicated.setName(REPLICATED_CACHE_NAME); GridClientDataConfiguration replicatedAsync = new GridClientDataConfiguration(); replicatedAsync.setName(REPLICATED_ASYNC_CACHE_NAME); cfg.setDataConfigurations(Arrays.asList(loc, partitioned, replicated, replicatedAsync)); return cfg; }
/** * Copy from the copy method in StructUtil. Did not want to drag that code in. maybe this actually * should go to struct. * * @param from * @param to * @param excludes * @return * @throws Exception */ public static <T extends struct> T xcopy(struct from, T to, String... excludes) throws Exception { Arrays.sort(excludes); for (Field f : from.fields()) { if (Arrays.binarySearch(excludes, f.getName()) >= 0) continue; Object o = f.get(from); if (o == null) continue; Field tof = to.getField(f.getName()); if (tof != null) try { tof.set(to, Converter.cnv(tof.getGenericType(), o)); } catch (Exception e) { System.out.println( "Failed to convert " + f.getName() + " from " + from.getClass() + " to " + to.getClass() + " value " + o + " exception " + e); } } return to; }
/** descendingkeySet.toArray returns contains all keys */ public void testDescendingDescendingKeySetToArray() { ConcurrentNavigableMap map = dmap5(); Set s = map.descendingKeySet(); Object[] ar = s.toArray(); assertEquals(5, ar.length); assertTrue(s.containsAll(Arrays.asList(ar))); ar[0] = m10; assertFalse(s.containsAll(Arrays.asList(ar))); }
@Test public void testDelayErrorMaxConcurrent() { final List<Long> requests = new ArrayList<>(); Observable<Integer> source = Observable.mergeDelayError( Observable.just( Observable.just(1).asObservable(), Observable.<Integer>error(new TestException())) .doOnRequest( new LongConsumer() { @Override public void accept(long t1) { requests.add(t1); } }), 1); TestSubscriber<Integer> ts = new TestSubscriber<>(); source.subscribe(ts); ts.assertValue(1); ts.assertTerminated(); ts.assertError(TestException.class); assertEquals(Arrays.asList(1L, 1L, 1L), requests); }
/** @throws Exception If failed. */ public void testClientAffinity() throws Exception { GridClientData partitioned = client.data(PARTITIONED_CACHE_NAME); Collection<Object> keys = new ArrayList<>(); keys.addAll(Arrays.asList(Boolean.TRUE, Boolean.FALSE, 1, Integer.MAX_VALUE)); Random rnd = new Random(); StringBuilder sb = new StringBuilder(); // Generate some random strings. for (int i = 0; i < 100; i++) { sb.setLength(0); for (int j = 0; j < 255; j++) // Only printable ASCII symbols for test. sb.append((char) (rnd.nextInt(0x7f - 0x20) + 0x20)); keys.add(sb.toString()); } // Generate some more keys to achieve better coverage. for (int i = 0; i < 100; i++) keys.add(UUID.randomUUID()); for (Object key : keys) { UUID nodeId = grid(0).mapKeyToNode(PARTITIONED_CACHE_NAME, key).id(); UUID clientNodeId = partitioned.affinity(key); assertEquals( "Invalid affinity mapping for REST response for key: " + key, nodeId, clientNodeId); } }
private void add(PooledFatPipe child) { long start = System.currentTimeMillis(); while (true) { int fails = 0; for (int i = 0; i < inUse.length(); i++) { if (!inUse.compareAndSet(i, 0, 2)) { if (inUse.get(i) != 2) fails++; } } if (fails == 0) break; if (start + 100 < System.currentTimeMillis()) { logger.warn("unable to add cleanly " + toString()); break; } } final AtomicIntegerArray updated = new AtomicIntegerArray(inUse.length() + 1); final List<Integer> needsReset = new ArrayList<>(); for (int i = 0; i < inUse.length(); i++) { int value = inUse.get(i); if (value == 1) { updated.set(i, value); needsReset.add(i); } } final AtomicIntegerArray oldUse = inUse; inUse = updated; signals = new ChildSignalImpl[signals.length + poolSize]; for (int i = 0; i < signals.length; i++) { signals[i] = new ChildSignalImpl(inUse, 0); } children = Arrays.copyOf(children, children.length + 1); children[children.length - 1] = child; scheduleReset(needsReset, oldUse, updated); }
/** * Prints a phrase on the grid nodes running anonymous closure objects and calculating total * number of letters. * * @param phrase Phrase to print on of the grid nodes. * @throws GridException If failed. */ private static void countLettersClosure(String phrase) throws GridException { X.println(">>> Starting countLettersClosure() example..."); // Explicitly execute the collection of callable objects and receive a result. Collection<Integer> results = G.grid() .call( SPREAD, new GridClosure<String, Integer>() { // Create executable logic. @Override public Integer apply(String word) { // Print out a given word, just so we can // see which node is doing what. X.println(">>> Executing word: " + word); // Return the length of a given word, i.e. number of letters. return word.length(); } }, Arrays.asList(phrase.split(" "))); // Collection of arguments for closures. // Add up all results using convenience 'sum()' method. int letterCnt = F.sum(results); X.println(">>>"); X.println(">>> Finished execution of counting letters with closure based on GridGain 3.0 API."); X.println(">>> You should see the phrase '" + phrase + "' printed out on the nodes."); X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'."); X.println(">>> Check all nodes for output (this node is also part of the grid)."); X.println(">>>"); }
@Test public void testOnStartRequestsAreAdditiveAndOverflowBecomesMaxValue() { final List<Integer> list = new ArrayList<>(); Observable.just(1, 2, 3, 4, 5) .subscribe( new Observer<Integer>() { @Override public void onStart() { request(2); request(Long.MAX_VALUE - 1); } @Override public void onComplete() {} @Override public void onError(Throwable e) {} @Override public void onNext(Integer t) { list.add(t); } }); assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); }
/** addAll of a collection with null elements throws NPE */ public void testAddAll2() { try { Succeed q = new Succeed(); Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) { } }
private static void testImplementation(Class<? extends Collection> implClazz) throws Throwable { testPotato(implClazz, Vector.class); testPotato(implClazz, CopyOnWriteArrayList.class); final Constructor<? extends Collection> constr = implClazz.getConstructor(Collection.class); final Collection<Object> coll = constr.newInstance(Arrays.asList(new String[] {})); coll.add(1); equal(coll.toString(), "[1]"); }
public RegularBot(RegularBotControlsUI ui, RegularBotData data) { this.ui = ui; data.lock(); this.data = data; commands = new ArrayList<Command>(); commands.addAll(Arrays.asList((Command[]) DefaultCommands.values())); status("Waiting."); progress(0, false); connect(); }
/** * addAll of a collection with any null elements throws NPE after possibly adding some elements */ public void testAddAll3() { try { Succeed q = new Succeed(); Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE - 1; ++i) ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) { } }
/** addAll throws ISE if an add fails */ public void testAddAll4() { try { Fail q = new Fail(); Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE; ++i) ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (IllegalStateException success) { } }
/** * Adds a new int parameter to be altered for the model being tuned. * * @param param the model parameter * @param initialSearchValues the values to try for the specified parameter */ public void addParameter(IntParameter param, int... initialSearchValues) { searchParams.add(param); DoubleList dl = new DoubleList(initialSearchValues.length); for (double d : initialSearchValues) dl.add(d); Arrays.sort(dl.getBackingArray()); // convience, only really needed if param is warm if (param.isWarmParameter() && !param.preferredLowToHigh()) Collections.reverse(dl); // put it in the prefered order if (param.isWarmParameter()) // put it at the front! searchValues.add(0, dl); else searchValues.add(dl); }
/** * Adds a new double parameter to be altered for the model being tuned. * * @param param the model parameter * @param initialSearchValues the values to try for the specified parameter */ public void addParameter(DoubleParameter param, double... initialSearchValues) { if (param == null) throw new IllegalArgumentException("null not allowed for parameter"); searchParams.add(param); DoubleList dl = new DoubleList(initialSearchValues.length); for (double d : initialSearchValues) dl.add(d); Arrays.sort(dl.getBackingArray()); // convience, only really needed if param is warm if (param.isWarmParameter() && !param.preferredLowToHigh()) Collections.reverse(dl); // put it in the prefered order if (param.isWarmParameter()) // put it at the front! searchValues.add(0, dl); else searchValues.add(dl); }
/** Values.toArray contains all values */ public void testDescendingValuesToArray() { ConcurrentNavigableMap map = dmap5(); Collection v = map.values(); Object[] ar = v.toArray(); ArrayList s = new ArrayList(Arrays.asList(ar)); assertEquals(5, ar.length); assertTrue(s.contains("A")); assertTrue(s.contains("B")); assertTrue(s.contains("C")); assertTrue(s.contains("D")); assertTrue(s.contains("E")); }
/** * Calculates length of a given phrase on the grid. * * @param phrase Phrase to count the number of letters in. * @throws GridException If failed. */ private static void countLettersReducer(String phrase) throws GridException { X.println(">>> Starting countLettersReducer() example..."); Grid grid = G.grid(); // Logger to use in your closure. Note that even though we assign it // to a local variable, GridGain still allows to use it from remotely // executed code. final GridLogger log = grid.log(); // Execute Hello World task. int letterCnt = grid.reduce( BALANCE, new GridClosure<String, Integer>() { // Create executable logic. @Override public Integer apply(String word) { // Print out a given word, just so we can // see which node is doing what. log.info(">>> Calculating for word: " + word); // Return the length of a given word, i.e. number of letters. return word.length(); } }, Arrays.asList(phrase.split(" ")), // Collection of words. // Create custom reducer. // NOTE: Alternatively, you can use existing reducer: F.sumIntReducer() new GridReducer<Integer, Integer>() { private int sum; @Override public boolean collect(Integer res) { sum += res; return true; // True means continue collecting until last result. } @Override public Integer apply() { return sum; } }); X.println(">>>"); X.println(">>> Finished execution of counting letters with reducer based on GridGain 3.0 API."); X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'."); X.println(">>> You should see individual words printed out on different nodes."); X.println(">>> Check all nodes for output (this node is also part of the grid)."); X.println(">>>"); }
@Test public void putBigObject() { HazelcastClient hClient = getHazelcastClient(); Map<String, Object> clientMap = hClient.getMap("putABigObject"); List list = new ArrayList(); int size = 10000000; byte[] b = new byte[size]; b[size - 1] = (byte) 144; list.add(b); clientMap.put("obj", b); byte[] bigB = (byte[]) clientMap.get("obj"); assertTrue(Arrays.equals(b, bigB)); assertEquals(size, bigB.length); }
/** * Returns compact class host. * * @param obj Object to compact. * @return String. */ @Nullable public static Object compactObject(Object obj) { if (obj == null) return null; if (obj instanceof Enum) return obj.toString(); if (obj instanceof String || obj instanceof Boolean || obj instanceof Number) return obj; if (obj instanceof Collection) { Collection col = (Collection) obj; Object[] res = new Object[col.size()]; int i = 0; for (Object elm : col) res[i++] = compactObject(elm); return res; } if (obj.getClass().isArray()) { Class<?> arrType = obj.getClass().getComponentType(); if (arrType.isPrimitive()) { if (obj instanceof boolean[]) return Arrays.toString((boolean[]) obj); if (obj instanceof byte[]) return Arrays.toString((byte[]) obj); if (obj instanceof short[]) return Arrays.toString((short[]) obj); if (obj instanceof int[]) return Arrays.toString((int[]) obj); if (obj instanceof long[]) return Arrays.toString((long[]) obj); if (obj instanceof float[]) return Arrays.toString((float[]) obj); if (obj instanceof double[]) return Arrays.toString((double[]) obj); } Object[] arr = (Object[]) obj; int iMax = arr.length - 1; StringBuilder sb = new StringBuilder("["); for (int i = 0; i <= iMax; i++) { sb.append(compactObject(arr[i])); if (i != iMax) sb.append(", "); } sb.append("]"); return sb.toString(); } return U.compact(obj.getClass().getName()); }
/** * Create the containers. * * @param optionList * @return */ public Set<Container> build(Collection<CreateContainerOptions> optionList) { Set<Container> containers = new HashSet<Container>(); FabricService fabricService = getOsgiService(FabricService.class); CompletionService<Set<Container>> completionService = new ExecutorCompletionService<Set<Container>>(executorService); int tasks = 0; for (CreateContainerOptions options : optionList) { if (!options.isEnsembleServer()) { options.setZookeeperUrl(fabricService.getZookeeperUrl()); completionService.submit(new CreateContainerTask(fabricService, options)); tasks++; } } try { for (int i = 0; i < tasks; i++) { Future<Set<Container>> futureContainerSet = completionService.poll(CREATE_TIMEOUT, TimeUnit.MILLISECONDS); Set<Container> containerSet = futureContainerSet.get(); CONTAINERS.addAll(containerSet); containers.addAll(containerSet); } for (Container container : containers) { Version version = fabricService.getDefaultVersion(); container.setVersion(version); Set<Profile> profiles = new HashSet(Arrays.asList(container.getProfiles())); for (String profileName : profileNames) { Profile profile = container.getVersion().getProfile(profileName); profiles.add(profile); } container.setProfiles(profiles.toArray(new Profile[profiles.size()])); } try { if (waitForProvisioning) { Provision.waitForContainerStatus(containers, provisionTimeOut); } if (assertProvisioningResult) { Provision.assertSuccess(containers, provisionTimeOut); } } catch (Exception e) { throw new FabricException(e); } } catch (Exception e) { throw new FabricException(e); } return containers; }
private int compare(Revision a, Revision b) { if (Arrays.equals(a._id, b._id)) return 0; Version va = getVersion(a); Version vb = getVersion(b); int n = va.compareTo(vb); if (n != 0) return n; if (a.created != b.created) return a.created > b.created ? 1 : -1; for (int i = 0; i < a._id.length; i++) if (a._id[i] != b._id[i]) return a._id[i] > b._id[i] ? 1 : -1; return 0; }
private static Collection<String> parseSoapResponseForUrls(byte[] data) throws SOAPException, IOException { // System.out.println(new String(data)); final Collection<String> urls = new ArrayList<>(); MessageFactory factory = MessageFactory.newInstance(WS_DISCOVERY_SOAP_VERSION); final MimeHeaders headers = new MimeHeaders(); headers.addHeader("Content-type", WS_DISCOVERY_CONTENT_TYPE); SOAPMessage message = factory.createMessage(headers, new ByteArrayInputStream(data)); SOAPBody body = message.getSOAPBody(); for (Node node : getNodeMatching(body, ".*:XAddrs")) { if (node.getTextContent().length() > 0) { urls.addAll(Arrays.asList(node.getTextContent().split(" "))); } } return urls; }
/** * Concat arrays in one. * * @param arrays Arrays. * @return Summary array. */ public static int[] concat(int[]... arrays) { assert arrays != null; assert arrays.length > 1; int len = 0; for (int[] a : arrays) len += a.length; int[] r = Arrays.copyOf(arrays[0], len); for (int i = 1, shift = 0; i < arrays.length; i++) { shift += arrays[i - 1].length; System.arraycopy(arrays[i], 0, r, shift, arrays[i].length); } return r; }
public void testDelayedTasksReusePooledThreadIfExecuteAtDifferentTimes() throws InterruptedException, ExecutionException { final AppScheduledExecutorService service = new AppScheduledExecutorService(getName()); final List<LogInfo> log = Collections.synchronizedList(new ArrayList<>()); // pre-start one thread Future<?> future = service.submit(EmptyRunnable.getInstance()); future.get(); service.setBackendPoolCorePoolSize(1); assertEquals(1, service.getBackendPoolExecutorSize()); int delay = 500; ScheduledFuture<?> f1 = service.schedule((Runnable) () -> log.add(new LogInfo(1)), delay, TimeUnit.MILLISECONDS); ScheduledFuture<?> f2 = service.schedule( (Runnable) () -> log.add(new LogInfo(2)), delay + 100, TimeUnit.MILLISECONDS); ScheduledFuture<?> f3 = service.schedule( (Runnable) () -> log.add(new LogInfo(3)), delay + 200, TimeUnit.MILLISECONDS); assertEquals(1, service.getBackendPoolExecutorSize()); assertFalse(f1.isDone()); assertFalse(f2.isDone()); assertFalse(f3.isDone()); TimeoutUtil.sleep(delay + 200 + 300); assertTrue(f1.isDone()); assertTrue(f2.isDone()); assertTrue(f3.isDone()); assertEquals(1, service.getBackendPoolExecutorSize()); assertEquals(3, log.size()); Set<Thread> usedThreads = new HashSet<>( Arrays.asList( log.get(0).currentThread, log.get(1).currentThread, log.get(2).currentThread)); if (usedThreads.size() != 1) { System.err.println(ThreadDumper.dumpThreadsToString()); } assertEquals(usedThreads.toString(), 1, usedThreads.size()); // must be executed in same thread service.shutdownAppScheduledExecutorService(); assertTrue(service.awaitTermination(10, TimeUnit.SECONDS)); }
public void run() { Comparator<Double> comp = new Comparator<Double>() { public int compare(Double i1, Double i2) { component.setValues(values, i1, i2); try { if (run) Thread.sleep(DELAY); else gate.acquire(); } catch (InterruptedException exception) { Thread.currentThread().interrupt(); } return i1.compareTo(i2); } }; Arrays.sort(values, comp); component.setValues(values, null, null); }
void discoverThread() { try { this.startupLatch.await(); } catch (final InterruptedException e) { e.printStackTrace(); return; } // Increase counter this.discoverThreadCounter.incrementAndGet(); // Create empty data. ServiceInfo[] infos = {}; // Magic: Get all network services of our type. try { this.jmdnsLock.lock(); // TODO: jmdsn can be null if no network card is present infos = this.jmdns.list(TYPE); } catch (final IllegalStateException e) { this.logger.status("discoverthread/exception", "message", e.getMessage()); } finally { this.jmdnsLock.unlock(); } // Reset all service infos try { this.serviceInfosLock.lock(); this.serviceInfos.clear(); this.serviceInfos.addAll(Arrays.asList(infos)); } finally { this.serviceInfosLock.unlock(); } // Process all callbacks. @SuppressWarnings("unused") final Collection<CallbackRequest> toRemove = new ArrayList<CallbackRequest>(); // // TODO: This callback handling needs improvement. Check for unsynchronized access to the // allRequest structure // }
public DLDFSolver( int[] s, int[] learning_startfactors, boolean singleThreaded, boolean useBacktracking) { log_info("Heuristic weights: %s", Arrays.deepToString(choicefactors)); log_info("Reported number of processors: %d", Runtime.getRuntime().availableProcessors()); nThreads = (singleThreaded || THREAD_COUNT == 1) ? 1 : THREAD_COUNT; log_info("No. of threads to be used: %d\n", nThreads); this.useBacktracking = useBacktracking; log_info("Use backtracking?: %s", useBacktracking ? "Yes" : "No"); this.tileSequence = new int[s.length]; System.arraycopy(s, 0, this.tileSequence, 0, s.length); if (learning_startfactors != null) { if (learning_startfactors.length != learning_starts.length) { throw new IllegalArgumentException("Invalid learning factor size"); } System.arraycopy(learning_startfactors, 0, learning_starts, 0, learning_starts.length); } }
@Test public void testStartWithWithScheduler() { TestScheduler scheduler = new TestScheduler(); Observable<Integer> observable = Observable.just(3, 4).startWith(Arrays.asList(1, 2)).subscribeOn(scheduler); Subscriber<Integer> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onNext(1); inOrder.verify(observer, times(1)).onNext(2); inOrder.verify(observer, times(1)).onNext(3); inOrder.verify(observer, times(1)).onNext(4); inOrder.verify(observer, times(1)).onComplete(); inOrder.verifyNoMoreInteractions(); }
/** * Executes the provided query asynchronously. * * <p>This method does not block. It returns as soon as the query has been passed to the * underlying network stack. In particular, returning from this method does not guarantee that the * query is valid or has even been submitted to a live node. Any exception pertaining to the * failure of the query will be thrown when accessing the {@link ResultSetFuture}. * * <p>Note that for queries that doesn't return a result (INSERT, UPDATE and DELETE), you will * need to access the ResultSetFuture (that is call one of its get method to make sure the query * was successful. * * @param query the CQL query to execute (that can be either a {@code Statement} or a {@code * BoundStatement}). If it is a {@code BoundStatement}, all variables must have been bound * (the statement must be ready). * @return a future on the result of the query. * @throws IllegalStateException if {@code query} is a {@code BoundStatement} but {@code * !query.isReady()}. */ public ResultSetFuture executeAsync(Query query) { if (query instanceof Statement) { return manager.executeQuery( new QueryMessage( ((Statement) query).getQueryString(), ConsistencyLevel.toCassandraCL(query.getConsistencyLevel())), query); } else { assert query instanceof BoundStatement : query; BoundStatement bs = (BoundStatement) query; return manager.executeQuery( new ExecuteMessage( bs.statement.id, Arrays.asList(bs.values), ConsistencyLevel.toCassandraCL(query.getConsistencyLevel())), query); } }