/** intValue returns current value. */
 public void testIntValue() {
   AtomicInteger ai = new AtomicInteger();
   assertEquals(0, ai.intValue());
   for (int x : VALUES) {
     ai.set(x);
     assertEquals(x, ai.intValue());
   }
 }
Example #2
0
  public void testWatchChildren() throws Exception {
    try (ZkConnection conn = new ZkConnection()) {
      final SolrZkClient zkClient = conn.getClient();
      final AtomicInteger cnt = new AtomicInteger();
      final CountDownLatch latch = new CountDownLatch(1);

      zkClient.makePath("/collections", true);

      zkClient.getChildren(
          "/collections",
          new Watcher() {

            @Override
            public void process(WatchedEvent event) {
              if (DEBUG) {
                System.out.println("children changed");
              }
              cnt.incrementAndGet();
              // remake watch
              try {
                zkClient.getChildren("/collections", this, true);
                latch.countDown();
              } catch (KeeperException | InterruptedException e) {
                throw new RuntimeException(e);
              }
            }
          },
          true);

      zkClient.makePath("/collections/collection99/shards", true);
      latch.await(); // wait until watch has been re-created

      zkClient.makePath("collections/collection99/config=collection1", true);

      zkClient.makePath("collections/collection99/config=collection3", true);

      zkClient.makePath("/collections/collection97/shards", true);

      if (DEBUG) {
        zkClient.printLayoutToStdOut();
      }

      // pause for the watches to fire
      Thread.sleep(700);

      if (cnt.intValue() < 2) {
        Thread.sleep(4000); // wait a bit more
      }

      if (cnt.intValue() < 2) {
        Thread.sleep(4000); // wait a bit more
      }

      assertEquals(2, cnt.intValue());
    }
  }
  @Test
  public void shouldFailUntilImportExecutes() throws Exception {
    final GremlinExecutor gremlinExecutor = GremlinExecutor.build().create();

    final Set<String> imports =
        new HashSet<String>() {
          {
            add("import java.awt.Color");
          }
        };

    final AtomicInteger successes = new AtomicInteger(0);
    final AtomicInteger failures = new AtomicInteger(0);

    // issue 1000 scripts in one thread using a class that isn't imported.  this will result in
    // failure.
    // while that thread is running start a new thread that issues an addImports to include that
    // class.
    // this should block further evals in the first thread until the import is complete at which
    // point
    // evals in the first thread will resume and start to succeed
    final Thread t1 =
        new Thread(
            () ->
                IntStream.range(0, 1000)
                    .mapToObj(i -> gremlinExecutor.eval("Color.BLACK"))
                    .forEach(
                        f -> {
                          f.exceptionally(t -> failures.incrementAndGet()).join();
                          if (!f.isCompletedExceptionally()) successes.incrementAndGet();
                        }));

    final Thread t2 =
        new Thread(
            () -> {
              while (failures.get() < 500) {}
              gremlinExecutor.getScriptEngines().addImports(imports);
            });

    t1.start();
    t2.start();

    t1.join();
    t2.join();

    assertTrue(successes.intValue() > 0);
    assertTrue(failures.intValue() >= 500);

    gremlinExecutor.close();
  }
Example #4
0
  @Test
  public void testFormulaDependency() {
    SBook book = SBooks.createBook("book1");
    SSheet sheet = book.createSheet("Sheet 1");

    SRanges.range(sheet, 0, 0).setEditText("999");
    SRanges.range(sheet, 0, 1).setValue("=SUM(A1)");

    SCell cell = sheet.getCell(0, 0);
    Assert.assertEquals(CellType.NUMBER, cell.getType());
    Assert.assertEquals(999, cell.getNumberValue().intValue());

    cell = sheet.getCell(0, 1);
    Assert.assertEquals(CellType.FORMULA, cell.getType());
    Assert.assertEquals(CellType.NUMBER, cell.getFormulaResultType());
    Assert.assertEquals("SUM(A1)", cell.getFormulaValue());
    Assert.assertEquals(999D, cell.getValue());

    final AtomicInteger a0counter = new AtomicInteger(0);
    final AtomicInteger b0counter = new AtomicInteger(0);
    final AtomicInteger unknowcounter = new AtomicInteger(0);

    book.addEventListener(
        new ModelEventListener() {
          public void onEvent(ModelEvent event) {
            if (event.getName().equals(ModelEvents.ON_CELL_CONTENT_CHANGE)) {
              CellRegion region = event.getRegion();
              if (region.getRow() == 0 && region.getColumn() == 0) {
                a0counter.incrementAndGet();
              } else if (region.getRow() == 0 && region.getColumn() == 1) {
                b0counter.incrementAndGet();
              } else {
                unknowcounter.incrementAndGet();
              }
            }
          }
        });

    SRanges.range(sheet, 0, 0).setEditText("888");
    Assert.assertEquals(1, b0counter.intValue());
    Assert.assertEquals(1, a0counter.intValue());
    Assert.assertEquals(0, unknowcounter.intValue());

    SRanges.range(sheet, 0, 0).setEditText("777");
    Assert.assertEquals(2, b0counter.intValue());
    Assert.assertEquals(2, a0counter.intValue());
    Assert.assertEquals(0, unknowcounter.intValue());

    SRanges.range(sheet, 0, 0)
        .setEditText("777"); // in last update, set edit text is always notify cell change
    Assert.assertEquals(3, b0counter.intValue());
    Assert.assertEquals(3, a0counter.intValue());
    Assert.assertEquals(0, unknowcounter.intValue());
  }
Example #5
0
  public void testSelect() throws Exception {
    cache.put(new MyKey("a"), 1);
    cache.put(new MyKey("b"), 2);
    cache.put(new MyKey("c"), 3);
    final AtomicInteger count = new AtomicInteger();
    CachedObjectSelector<Serializable, Object> selector =
        new CachedObjectSelector<Serializable, Object>() {

          public void onSelect(
              ExoCache<? extends Serializable, ? extends Object> cache,
              Serializable key,
              ObjectCacheInfo<? extends Object> ocinfo)
              throws Exception {
            assertTrue(
                key.equals(new MyKey("a"))
                    || key.equals(new MyKey("b"))
                    || key.equals(new MyKey("c")));
            assertTrue(ocinfo.get().equals(1) || ocinfo.get().equals(2) || ocinfo.get().equals(3));
            count.incrementAndGet();
          }

          public boolean select(Serializable key, ObjectCacheInfo<? extends Object> ocinfo) {
            return true;
          }
        };
    try {
      cache.select(selector);
      assertEquals(3, count.intValue());
    } catch (UnsupportedOperationException e) {
      // OK
    }
  }
  @Test
  public void shouldNotExhaustThreads() throws Exception {
    final ScheduledExecutorService executorService =
        Executors.newScheduledThreadPool(2, testingThreadFactory);
    final GremlinExecutor gremlinExecutor =
        GremlinExecutor.build()
            .executorService(executorService)
            .scheduledExecutorService(executorService)
            .create();

    final AtomicInteger count = new AtomicInteger(0);
    assertTrue(
        IntStream.range(0, 1000)
            .mapToObj(i -> gremlinExecutor.eval("1+1"))
            .allMatch(
                f -> {
                  try {
                    return (Integer) f.get() == 2;
                  } catch (Exception ex) {
                    throw new RuntimeException(ex);
                  } finally {
                    count.incrementAndGet();
                  }
                }));

    assertEquals(1000, count.intValue());

    executorService.shutdown();
    executorService.awaitTermination(30000, TimeUnit.MILLISECONDS);
  }
Example #7
0
 public int getNextQueryNumber() {
   synchronized (queryCounter) {
     int i = queryCounter.intValue();
     queryCounter.set(i + 1);
     return i;
   }
 }
  /**
   * Load preferences from an input stream.
   *
   * <p>Each line is a different preference, with tab-separated fields indicating user, item, weight
   * and other information.
   *
   * @param <U> type of the users
   * @param <I> type of the items
   * @param in input stream to read from
   * @param uParser user type parser
   * @param iParser item type parser
   * @param dp double parse
   * @param uIndex user index
   * @param iIndex item index
   * @return a simple list-of-lists FastPreferenceData with the information read
   * @throws IOException when path does not exists of IO error
   */
  public static <U, I> SimpleFastPreferenceData<U, I> load(
      InputStream in,
      Parser<U> uParser,
      Parser<I> iParser,
      DoubleParser dp,
      FastUserIndex<U> uIndex,
      FastItemIndex<I> iIndex)
      throws IOException {
    AtomicInteger numPreferences = new AtomicInteger();

    List<List<IdxPref>> uidxList = new ArrayList<>();
    for (int uidx = 0; uidx < uIndex.numUsers(); uidx++) {
      uidxList.add(null);
    }

    List<List<IdxPref>> iidxList = new ArrayList<>();
    for (int iidx = 0; iidx < iIndex.numItems(); iidx++) {
      iidxList.add(null);
    }

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
      reader
          .lines()
          .forEach(
              l -> {
                CharSequence[] tokens = split(l, '\t', 4);
                U user = uParser.parse(tokens[0]);
                I item = iParser.parse(tokens[1]);
                double value;
                if (tokens.length >= 3) {
                  value = dp.parse(tokens[2]);
                } else {
                  value = dp.parse(null);
                }

                int uidx = uIndex.user2uidx(user);
                int iidx = iIndex.item2iidx(item);

                numPreferences.incrementAndGet();

                List<IdxPref> uList = uidxList.get(uidx);
                if (uList == null) {
                  uList = new ArrayList<>();
                  uidxList.set(uidx, uList);
                }
                uList.add(new IdxPref(iidx, value));

                List<IdxPref> iList = iidxList.get(iidx);
                if (iList == null) {
                  iList = new ArrayList<>();
                  iidxList.set(iidx, iList);
                }
                iList.add(new IdxPref(uidx, value));
              });
    }

    return new SimpleFastPreferenceData<>(
        numPreferences.intValue(), uidxList, iidxList, uIndex, iIndex);
  }
Example #9
0
 public void returnSync(final String key) {
   synchronized (this.locks) {
     final AtomicInteger c = this.locks.get(key);
     if (c != null) {
       c.decrementAndGet();
       if (c.intValue() < 1) this.locks.remove(key);
     }
   }
 }
  public void process(Exchange exchange) throws Exception {

    if (count.intValue() % 2 == 1) {
      exchange.setException(new RecoverableException("Unexpected error, try again"));
      count.incrementAndGet();
    } else {
      logger.info(String.format("Receiving message=%s", exchange.getIn().getBody()));
    }
  }
Example #11
0
 @Override
 public void run() {
   while (true) {
     if (outerCounter.intValue() > innerCounter) {
       innerCounter = outerCounter.intValue();
       try {
         saveToFile();
       } catch (Exception e) {
         innerCounter--;
       }
       try {
         sleep(SLEEPING_TIME);
       } catch (InterruptedException e) {
         System.out.println("Interrupt sleeping saver ");
       }
     }
   }
 }
Example #12
0
 /** 1. 从Number中继承过来的几个方法, 并实现了它。 */
 public void testValue() {
   AtomicInteger ai = new AtomicInteger(22);
   System.out.println(ai.byteValue());
   System.out.println(ai.shortValue());
   System.out.println(ai.intValue());
   System.out.println(ai.longValue());
   System.out.println(ai.floatValue());
   System.out.println(ai.doubleValue());
 }
  public void doFinalReport() {
    await();
    doPeriodReport(false);
    test.processCacheStats();
    clusterTestCount.addAndGet(nodeTestCount.intValue());
    clusterReads.addAndGet(processor.getRead());
    clusterWrites.addAndGet(processor.getWrite());

    // * 100 to get better average as it will convert to Long
    if (getConfiguration().isStandalone()) {
      clusterLatency.addAndGet(
          (long) cumulativeLatencyStats.getTotal() * 100 / testCountAtLastReport);
    }
    await();
    long totalNode = nodeTestCount.get();
    long totalCluster = clusterTestCount.get();
    long totalRead = clusterReads.get();
    long totalWrite = clusterWrites.get();
    log.info("------- FINAL REPORT -------- ");
    long testDuration = (actualTestEndTime - testStartTime) / 1000;
    log.info(String.format("Node TPS: %.1f", (double) totalNode / testDuration));
    log.info(String.format("Cluster TPS: %.1f", (double) totalCluster / testDuration));
    log.info("------- Cache Stats -------- ");
    log.info(String.format("Cluster Cache Read TPS: %.1f", (double) totalRead / testDuration));
    log.info(String.format("Cluster Cache Write TPS: %.1f", (double) totalWrite / testDuration));
    log.info(
        String.format(
            "Cluster Cache Total TPS: %.1f", (double) (totalRead + totalWrite) / testDuration));

    if (getConfiguration().isStandalone()) {
      log.info(
          String.format(
              "Cluster Avg Latency: %.1f",
              (double) clusterLatency.get() / (configuration.getNodesNum() * 100)));
      log.info(String.format("Warmup Cache TPS: %d", clusterCacheWarmup.get()));
      log.info(
          String.format(
              "Warmup Cluster TPS: %d , Time taken for clusterCoherent: %d",
              clusterWarmup.get(), bulkLoadCompleteTime));
    }
    int totalWrites = test.getWritesCount();
    if (totalWrites > 0) {
      log.info(
          String.format(
              "Node Total Write operations: %d (%.1f %%)",
              totalWrites, (totalWrites * 100.0 / totalNode)));
    }

    int exceptions = nonstopCacheExceptionCount.get();
    if (exceptions > 0) {
      log.info(
          String.format(
              "Node NonstopCache Exception Count: %d (%.1f %%)",
              exceptions, (exceptions * 100.0 / totalNode)));
    }
  }
Example #14
0
  @Override
  public void run() {

    while (true) {
      //			log.debug("Status available="+availableSlots+" used="+usedSlots+" queue="+queue.size());
      if (availableSlots.intValue() > usedSlots.intValue() && queue.size() > 0) {
        usedSlots.incrementAndGet();
        AnalysisRunner currentRun = queue.removeFirst();
        currentRun.addAnalysisListener(this);
        Thread t = new Thread(currentRun);
        t.start();
      }

      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }
  }
  @SmallTest
  @Feature({"Android-AppBase"})
  public void testFindAndUploadAllCrashes() throws IOException {
    // Setup prerequisites.
    final AtomicInteger numServiceStarts = new AtomicInteger(0);
    final File[] minidumpFiles = {
      new File(mCrashDir, "chromium_renderer-111.dmp1"),
      new File(mCrashDir, "chromium_renderer-222.dmp2"),
      new File(mCrashDir, "chromium_renderer-333.dmp3"),
    };
    MinidumpPreparationContext context =
        new MinidumpPreparationContext(getInstrumentation().getTargetContext()) {
          @Override
          public ComponentName startService(Intent intentToCheck) {
            String filePath =
                intentToCheck.getStringExtra(MinidumpUploadService.FILE_TO_UPLOAD_KEY);
            // Assuming numServicesStart value corresponds to minidumpFiles index.
            assertEquals(
                "Minidump path should be the absolute path",
                minidumpFiles[numServiceStarts.intValue()].getAbsolutePath(),
                filePath);
            assertTrue(
                "Should not call service more than number of files",
                numServiceStarts.incrementAndGet() <= minidumpFiles.length);
            assertEquals(
                "Action should be correct",
                MinidumpUploadService.ACTION_UPLOAD,
                intentToCheck.getAction());
            return new ComponentName(getPackageName(), MinidumpUploadService.class.getName());
          }
        };
    MinidumpUploadService service = new TestMinidumpUploadService(context);
    for (File minidumpFile : minidumpFiles) {
      setUpMinidumpFile(minidumpFile, BOUNDARY);
    }

    // Run test.
    Intent findAndUploadAllCrashesIntent =
        MinidumpUploadService.createFindAndUploadAllCrashesIntent(context);
    findAndUploadAllCrashesIntent.putExtra(
        MinidumpUploadService.FINISHED_LOGCAT_EXTRACTION_KEY, true);
    service.onCreate();
    service.onHandleIntent(findAndUploadAllCrashesIntent);

    // Verify.
    for (File minidumpFile : minidumpFiles) {
      assertTrue("Minidump file should exist: " + minidumpFile, minidumpFile.isFile());
    }
    assertEquals(
        "Should have called startService() same number of times as there are files",
        minidumpFiles.length,
        numServiceStarts.intValue());
  }
  private <T extends SingularityId> List<T> getChildrenAsIdsForParentsThrows(
      final String pathNameforLogs,
      final Collection<String> parents,
      final IdTranscoder<T> idTranscoder)
      throws Exception {
    if (parents.isEmpty()) {
      return Collections.emptyList();
    }

    final List<T> objects = Lists.newArrayListWithExpectedSize(parents.size());

    final CountDownLatch latch = new CountDownLatch(parents.size());
    final AtomicInteger missing = new AtomicInteger();

    final BackgroundCallback callback =
        new BackgroundCallback() {

          @Override
          public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
            if (event.getChildren() == null || event.getChildren().size() == 0) {
              LOG.trace("Expected children for node {} - but found none", event.getPath());

              missing.incrementAndGet();
              latch.countDown();

              return;
            }

            objects.addAll(Lists.transform(event.getChildren(), idTranscoder));

            latch.countDown();
          }
        };

    final long start = System.currentTimeMillis();

    for (String parent : parents) {
      curator.getChildren().inBackground(callback).forPath(parent);
    }

    checkLatch(latch, pathNameforLogs);

    LOG.trace(
        "Fetched {} objects from {} (missing {}) in {}",
        objects.size(),
        pathNameforLogs,
        missing.intValue(),
        JavaUtils.duration(start));

    return objects;
  }
Example #17
0
  public static void execute(
      final KyotoTycoonClient client, final int numOperations, final int numThreads)
      throws Exception {
    final CountDownLatch latch = new CountDownLatch(numOperations);

    client.start();
    client.clear();

    long start = System.currentTimeMillis();

    final AtomicInteger errorCount = new AtomicInteger(0);
    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
    Random r = new Random();
    for (int i = 0; i < numOperations; ++i) {
      final String key = "key" + i;
      final String value = String.valueOf(Math.abs(r.nextInt()));
      executor.submit(
          new Runnable() {
            @Override
            public void run() {
              try {
                client.set(key, value);
                assertEquals(value, client.get(key));
                // assertTrue(client.remove(key));
              } catch (RuntimeException e) {
                e.printStackTrace();
                fail();
              } finally {
                latch.countDown();
              }
            }
          });
    }
    executor.shutdown();
    latch.await();

    if (errorCount.intValue() > 0) {
      fail();
    }

    long timeElapsed = System.currentTimeMillis() - start;

    int qps = (int) ((numOperations / (float) timeElapsed) * 1000);
    System.out.println(
        client.getClass().getSimpleName() + " - result: " + timeElapsed + "ms, " + qps + "qps");

    client.clear();
    client.stop();
  }
  private <T> List<T> getAsyncThrows(
      final String pathNameForLogs, final Collection<String> paths, final Transcoder<T> transcoder)
      throws Exception {
    final List<T> objects = Lists.newArrayListWithCapacity(paths.size());

    if (paths.isEmpty()) {
      return objects;
    }

    final CountDownLatch latch = new CountDownLatch(paths.size());
    final AtomicInteger missing = new AtomicInteger();

    final BackgroundCallback callback =
        new BackgroundCallback() {

          @Override
          public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
            if (event.getData() == null || event.getData().length == 0) {
              LOG.trace("Expected active node {} but it wasn't there", event.getPath());

              missing.incrementAndGet();
              latch.countDown();

              return;
            }

            objects.add(transcoder.transcode(event.getData()));

            latch.countDown();
          }
        };

    final long start = System.currentTimeMillis();

    for (String path : paths) {
      curator.getData().inBackground(callback).forPath(path);
    }

    checkLatch(latch, pathNameForLogs);

    LOG.trace(
        "Fetched {} objects from {} (missing {}) in {}",
        objects.size(),
        pathNameForLogs,
        missing.intValue(),
        JavaUtils.duration(start));

    return objects;
  }
Example #19
0
  @Test
  public void testGet() {
    final AtomicInteger count = new AtomicInteger(0);
    final PId idToQuery = new PiId(messageId.toStringFull(), 0);

    koalaGCPastImpl =
        new KoalaGCPastImpl(
            node,
            storageManager,
            null,
            0,
            instance,
            new KoalaPastPolicy(),
            0,
            storageManager,
            KoalaNode.DEFAULT_NUMBER_OF_DHT_BACKUPS,
            koalaIdFactory,
            koalaPiEntityFactory) {
          @Override
          protected Continuation getResponseContinuation(PastMessage msg) {
            if (msg.equals(message)) return continuation;
            return null;
          }

          @Override
          protected void getHandles(rice.p2p.commonapi.Id id, int max, Continuation command) {
            NodeHandleSet nodeSet = mock(NodeHandleSet.class);
            when(nodeSet.size()).thenReturn(3);
            command.receiveResult(nodeSet);
          }

          @Override
          public void lookupHandle(
              rice.p2p.commonapi.Id id, NodeHandle handle, Continuation command) {
            assertEquals(Id.build(idToQuery.toStringFull()), id);
            count.addAndGet(1);
          }
        };

    // act
    koalaGCPastImpl.get(idToQuery, continuation);

    // assert
    assertEquals(3, count.intValue());
  }
  public void testPromptForSetting() throws Exception {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicReference<String> promptedSecretValue = new AtomicReference<>(null);
    final AtomicReference<String> promptedTextValue = new AtomicReference<>(null);
    final Terminal terminal =
        new MockTerminal() {
          @Override
          public char[] readSecret(String text, Object... args) {
            counter.incrementAndGet();
            assertThat(args, arrayContaining((Object) "foo.password"));
            return "changeit".toCharArray();
          }

          @Override
          public String readText(String text, Object... args) {
            counter.incrementAndGet();
            assertThat(args, arrayContaining((Object) "replace"));
            return "replaced";
          }
        };
    final NamedCommand cmd =
        new NamedCommand("noop", terminal) {
          @Override
          public CliTool.ExitStatus execute(Settings settings, Environment env) {
            promptedSecretValue.set(settings.get("foo.password"));
            promptedTextValue.set(settings.get("replace"));
            return OK;
          }
        };

    System.setProperty("es.foo.password", InternalSettingsPreparer.SECRET_PROMPT_VALUE);
    System.setProperty("es.replace", InternalSettingsPreparer.TEXT_PROMPT_VALUE);
    try {
      new SingleCmdTool("tool", terminal, cmd).execute();
    } finally {
      System.clearProperty("es.foo.password");
      System.clearProperty("es.replace");
    }

    assertThat(counter.intValue(), is(2));
    assertThat(promptedSecretValue.get(), is("changeit"));
    assertThat(promptedTextValue.get(), is("replaced"));
  }
Example #21
0
  private void doTestDataStore() {
    log.info("Starting...");
    long start = System.currentTimeMillis();

    for (int i = 0; i < THREAD_COUNT; ++i) {
      new Thread(new Read1Runnable()).start();
    }

    for (int i = 0; i < THREAD_COUNT; ++i) {
      new Thread(new Read2Runnable()).start();
    }

    for (int i = 0; i < THREAD_COUNT; ++i) {
      new Thread(new SaveRunnable()).start();
    }

    while (!(read1Done.get() && read2Done.get() && saveDone.get())) {
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        log.error(e.getMessage(), e);
      }
    }

    if (exceptionThrownByThread != null) {
      throw new RuntimeException("One of the worker threads failed.", exceptionThrownByThread);
    }

    long duration = System.currentTimeMillis() - start;

    log.info("Took: " + duration + " ms");
    log.info("Save: " + saveCount.intValue() + " files, " + bytesWritten.get() + " bytes");
    log.info(
        "Read: " + (read1Count.get() + read2Count.get()) + " files, " + bytesRead.get() + " bytes");

    log.info("Average save time (ns): " + (double) saveTime.get() / (double) saveCount.get());

    assertEquals(0, failures.get());

    for (String s : sessionCounter.keySet()) {
      dataStore.removeData(s);
    }
  }
  public void classificate() {
    initializeMe();
    String[] attr = this.getAttrs();
    features:
    for (int i = 0; i < 600; i++) {
      String featureValue = attr[i];
      if (featureValue.charAt(0) == '1') {
        for (String label : Globals.categories) {
          Classification classTmp = classification.get(label);

          AtomicInteger valFeatureModel =
              Globals.modelDict.get(label + "_" + i); // Count of this feature in this label
          if (valFeatureModel == null) // This feature was unknown always, skip it
          classTmp.addFeature(0);
          else classTmp.addFeature(valFeatureModel.intValue());
        }
      }
    }
  }
Example #23
0
 private static File createExperimentalFolder(
     List<IDesign> problems, File experimentalFolder, String expFolderId) throws InPUTException {
   if (expFolderId != null) {
     File expFolder;
     synchronized (globalExperimentCounter) { // make sure that the
       // counter is
       // consistent
       expFolder = new File(expFolderId);
       while (expFolder.exists()) {
         repetitionCounter = globalExperimentCounter.incrementAndGet();
         expFolder = new File(initExperimentalFolder(expFolderId));
       }
       repetitionCounter = globalExperimentCounter.intValue();
     }
     expFolder.mkdirs();
     experimentalFolder = expFolder;
   }
   addProblems(experimentalFolder, problems);
   return experimentalFolder;
 }
Example #24
0
  private void insertPending() {
    Pair pair;
    for (Iterator iterator = mPendingItemsToInsert.iterator();
        iterator.hasNext();
        mInsertable.add(((Integer) pair.first).intValue(), pair.second)) {
      pair = (Pair) iterator.next();
      Iterator iterator1 = mActiveIndexes.iterator();
      do {
        if (!iterator1.hasNext()) {
          break;
        }
        AtomicInteger atomicinteger = (AtomicInteger) iterator1.next();
        if (atomicinteger.intValue() >= ((Integer) pair.first).intValue()) {
          atomicinteger.incrementAndGet();
        }
      } while (true);
      mActiveIndexes.add(new AtomicInteger(((Integer) pair.first).intValue()));
    }

    mPendingItemsToInsert.clear();
  }
  /**
   * Tests basic configuration with polling activation.
   *
   * @throws StreamException if test fails
   */
  @Test
  public void configureWithPollingTest() throws Exception {
    final AtomicInteger count = new AtomicInteger(0);

    final ContextBuilderConfigurator cfg =
        new ContextBuilderConfigurator() {

          /** {@inheritDoc} */
          @Override
          public int internalConfigure(final ContextBuilder ctxBuilder) {
            return 1;
          }

          /** {@inheritDoc} */
          @Override
          public String getTag() {
            return "test";
          }

          /** {@inheritDoc} */
          @Override
          protected Long getLastUpdateTimestampFor(final String path) throws StreamException {
            synchronized (ContextBuilderConfiguratorTest.class) {
              count.incrementAndGet();
              ContextBuilderConfiguratorTest.class.notify();
            }

            return System.currentTimeMillis();
          }
        };

    final ContextBuilder builder = Mockito.mock(ContextBuilder.class);
    cfg.configure(builder);

    synchronized (ContextBuilderConfiguratorTest.class) {
      ContextBuilderConfiguratorTest.class.wait(1500L);
    }

    Assert.assertEquals(count.intValue(), NumberUtils.TWO);
  }
  @SmallTest
  @Feature({"Android-AppBase"})
  public void testFindAndUploadLastCrash() throws IOException {
    // Setup prerequisites.
    final AtomicInteger numServiceStarts = new AtomicInteger(0);
    final File minidumpFile = new File(mCrashDir, "chromium_renderer-123.dmp");
    MinidumpPreparationContext context =
        new MinidumpPreparationContext(getInstrumentation().getTargetContext()) {
          @Override
          public ComponentName startService(Intent intentToCheck) {
            String filePath =
                intentToCheck.getStringExtra(MinidumpUploadService.FILE_TO_UPLOAD_KEY);
            assertEquals(
                "Minidump path should be the absolute path",
                minidumpFile.getAbsolutePath(),
                filePath);
            assertEquals("Should only call service once", 1, numServiceStarts.incrementAndGet());
            assertEquals(
                "Action should be correct",
                MinidumpUploadService.ACTION_UPLOAD,
                intentToCheck.getAction());
            return new ComponentName(getPackageName(), MinidumpUploadService.class.getName());
          }
        };
    MinidumpUploadService service = new TestMinidumpUploadService(context);
    setUpMinidumpFile(minidumpFile, BOUNDARY);

    // Run test.
    Intent findAndUploadLastCrashIntent =
        MinidumpUploadService.createFindAndUploadLastCrashIntent(context);
    service.onCreate();
    findAndUploadLastCrashIntent.putExtra(
        MinidumpUploadService.FINISHED_LOGCAT_EXTRACTION_KEY, true);
    service.onHandleIntent(findAndUploadLastCrashIntent);

    // Verify.
    assertTrue("Minidump file should exist", minidumpFile.isFile());
    assertEquals("Should have called startService() once", 1, numServiceStarts.intValue());
  }
Example #27
0
  public void insert(List list) {
    if (mActiveIndexes.isEmpty() && mPendingItemsToInsert.isEmpty()) {
      Pair pair;
      for (list = list.iterator();
          list.hasNext();
          mInsertable.add(((Integer) pair.first).intValue(), pair.second)) {
        pair = (Pair) list.next();
        Iterator iterator = mActiveIndexes.iterator();
        do {
          if (!iterator.hasNext()) {
            break;
          }
          AtomicInteger atomicinteger = (AtomicInteger) iterator.next();
          if (atomicinteger.intValue() >= ((Integer) pair.first).intValue()) {
            atomicinteger.incrementAndGet();
          }
        } while (true);
        mActiveIndexes.add(new AtomicInteger(((Integer) pair.first).intValue()));
      }

    } else {
      mPendingItemsToInsert.addAll(list);
    }
  }
  @Override
  public void scheduleCleanup() {

    if (!cleanupEnabled || scheduledCleanup.intValue() > 2) {
      // Scheduled cleanup was already scheduled before.. never mind!
      // or we have cleanup disabled
      return;
    }

    scheduledCleanup.incrementAndGet();

    executor.execute(
        new Runnable() {
          public void run() {
            storageManager.setContext(storageManager.newSingleThreadContext());
            try {
              cleanup();
            } finally {
              storageManager.clearContext();
              scheduledCleanup.decrementAndGet();
            }
          }
        });
  }
Example #29
0
  public static void main(String[] args) {

    if (args.length < 8) {
      System.err.println(
          "use: -dbhost -dbname  -downloadHost -downloadFolderName -user -password"
              + "-sleeptime -maxthread");

      return;
    }

    dbHost = args[0];
    dbName = args[1];
    downloadHost = args[2];

    downloadFolderName = args[3];

    username = args[4];
    password = args[5];
    try {
      sleeptime = Long.parseLong(args[6]);
      maxthread = Integer.parseInt(args[7]);
    } catch (Exception e) {
      System.err.println("Error: -sleeptime and -maxthread must be valid integer values");
      return;
    }

    try {

      java.sql.Connection c = DBConnector.connect(dbName, dbHost, username, password);
      DBConnector.setConnect(c);

      AtomicInteger count = new AtomicInteger(0);

      while (true) {

        System.out.println(
            "Searching for tasks.. Still available to start: " + (maxthread - count.intValue()));

        try {
          List<Task> tasks = fetchTasks(count);

          for (Task t : tasks) {
            for (String r : t.initializeResource()) DBConnector.addScheduledTasks(t, r);
          }

          List<Task> scheduled_tasks = fetchScheduledTasks(count);
          c.commit();

          for (Task scheduledTask : scheduled_tasks) {

            if (!check(scheduledTask))
              switch (scheduledTask.getType()) {
                case Constants.IMAGE:
                  new ImageDownloader(scheduledTask, count).start();
                  break;
                case Constants.TEXT:
                  new TextDownloader(scheduledTask, count).start();
                  break;

                case Constants.VIDEO:
                  new VideoDownloader(scheduledTask, count).start();
                  break;

                case Constants.SOUND:
                  new SoundDownloader(scheduledTask, count).start();
                  break;
                case Constants._3D:
                  new Downloader3D(scheduledTask, count).start();
                  break;
              }
          }

        } catch (SQLException e) {
          c.rollback();
          System.out.println("rollbacking");
        }

        System.out.println("sleeping");
        Thread.sleep(sleeptime);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #30
0
  public void run() {
    String dbChangeId;
    String dbPluginType;
    // Order db changes by create date before starting to run them
    ArrayList<String> unsortedDbChangeIds = new ArrayList<String>();
    ArrayList<String> sortedDbChangeIds = new ArrayList<String>();
    for (int i = 0; i < dbChanges.length(); i++) {
      unsortedDbChangeIds.add(dbChanges.getString(i));
    }
    sortedDbChangeIds = repHandler.getDbChangesSortedByCreateDate(unsortedDbChangeIds);
    for (int i = 0; i < sortedDbChangeIds.size(); i++) {
      dbChangeId = sortedDbChangeIds.get(i);
      String dbChangeScript = repHandler.getDatabaseChangeScript(dbChangeId);
      dbPluginType = repHandler.getDBPluginTypeForDbChange(dbChangeId);
      DBService dbService = DBService.getDBService(dbPluginType);
      HashMap<String, String> globalParameters = null;
      try {
        String dbcadServerHostname = InetAddress.getLocalHost().getHostName();
        globalParameters = repHandler.getDBPluginConfig(dbPluginType, dbcadServerHostname);
      } catch (UnknownHostException e) {
        e.printStackTrace();
      }
      ArrayList<DeploymentDetails> deploymentDetailsList =
          repHandler.getDeployableDatabaseInstancesAndSchemaNamesForLobIdAndDbChange(
              dbChangeId, lobId);
      for (DeploymentDetails deploymentDetails : deploymentDetailsList) {
        if (!repHandler.isDbChangeDeployed(
            dbChangeId,
            deploymentDetails.getDbGroupId(),
            deploymentDetails.getDbInstance().getDbId())) {
          if (!markOnly) {
            repHandler.markDbChangeDeploymentStatus(
                dbChangeId,
                deploymentDetails.getDbGroupId(),
                deploymentDetails.getDbInstance().getDbId(),
                DBCADController.RUNNING_STATUS);
            HashMap<String, HashMap<String, String>> parameterAttributes =
                dbService.getInstanceParameterAttributes();
            String dbSchemaName = deploymentDetails.getSchemaName();
            Cipher aes = null;
            try {
              aes = Cipher.getInstance("AES");
              // SecretKeySpec k = new SecretKeySpec(DBCADController.ENCRYPTION_KEY.getBytes(),
              // "AES");
              aes.init(Cipher.DECRYPT_MODE, DBCADController.ENC_KEY);

              HashMap<String, String> pluginInstanceParameters =
                  deploymentDetails.getDbInstance().getPluginInstanceParameters();
              for (Map.Entry<String, String> entry : pluginInstanceParameters.entrySet()) {
                String key = entry.getKey();
                if (parameterAttributes.get(key) != null
                    && parameterAttributes.get(key).get("ENCRYPTED").equals("TRUE")) {
                  BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
                  textEncryptor.setPassword(DBCADController.ENCRYPTION_KEY);
                  String encTextValue = (String) pluginInstanceParameters.get(key);
                  pluginInstanceParameters.put(key, textEncryptor.decrypt(encTextValue));
                }
              }

            } catch (Exception e) {
              e.printStackTrace();
            }

            dbService.initializeDBService(deploymentDetails.getDbInstance(), globalParameters);
            String output;
            AtomicInteger exitCode = new AtomicInteger(0);
            output = dbService.runScript(dbChangeScript, dbSchemaName, exitCode);
            repHandler.addDeploymentLog(dbChangeId, deploymentDetails.getDbInstance(), output);
            dbService.close();

            if (exitCode.intValue() == 0) {
              repHandler.markDbChangeDeploymentStatus(
                  dbChangeId,
                  deploymentDetails.getDbGroupId(),
                  deploymentDetails.getDbInstance().getDbId(),
                  DBCADController.OK_STATUS);
            } else {
              repHandler.markDbChangeDeploymentStatus(
                  dbChangeId,
                  deploymentDetails.getDbGroupId(),
                  deploymentDetails.getDbInstance().getDbId(),
                  DBCADController.ERROR_STATUS);
            }
          } else {
            repHandler.addDeploymentLog(
                dbChangeId, deploymentDetails.getDbInstance(), "-- MARK ONLY --");
            repHandler.markDbChangeDeploymentStatus(
                dbChangeId,
                deploymentDetails.getDbGroupId(),
                deploymentDetails.getDbInstance().getDbId(),
                DBCADController.OK_STATUS);
          }
        }
      }
    }
  }