/** @throws Exception If failed. */
  @SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter"})
  public void testStartMultipleGridsFromSpring() throws Exception {
    File cfgFile =
        GridTestUtils.resolveIgnitePath(
            GridTestProperties.getProperty("loader.self.multipletest.config"));

    assert cfgFile != null;

    String path = cfgFile.getAbsolutePath();

    info("Loading Grid from configuration file: " + path);

    final GridTuple<IgniteState> gridState1 = F.t(null);
    final GridTuple<IgniteState> gridState2 = F.t(null);

    final Object mux = new Object();

    IgnitionListener factoryLsnr =
        new IgnitionListener() {
          @Override
          public void onStateChange(String name, IgniteState state) {
            synchronized (mux) {
              if ("grid-factory-test-1".equals(name)) gridState1.set(state);
              else if ("grid-factory-test-2".equals(name)) gridState2.set(state);
            }
          }
        };

    G.addListener(factoryLsnr);

    G.start(path);

    assert G.ignite("grid-factory-test-1") != null;
    assert G.ignite("grid-factory-test-2") != null;

    synchronized (mux) {
      assert gridState1.get() == STARTED
          : "Invalid grid state [expected=" + STARTED + ", returned=" + gridState1 + ']';
      assert gridState2.get() == STARTED
          : "Invalid grid state [expected=" + STARTED + ", returned=" + gridState2 + ']';
    }

    G.stop("grid-factory-test-1", true);
    G.stop("grid-factory-test-2", true);

    synchronized (mux) {
      assert gridState1.get() == STOPPED
          : "Invalid grid state [expected=" + STOPPED + ", returned=" + gridState1 + ']';
      assert gridState2.get() == STOPPED
          : "Invalid grid state [expected=" + STOPPED + ", returned=" + gridState2 + ']';
    }
  }
  /** @throws Exception If failed. */
  public void testStartMultipleNonDefaultGrids() throws Exception {
    try {
      multithreaded(
          new Callable<Object>() {
            @Nullable
            @Override
            public Object call() throws Exception {
              try {
                IgniteConfiguration cfg = new IgniteConfiguration();

                cfg.setGridName("TEST_NAME");
                cfg.setConnectorConfiguration(null);

                G.start(cfg);
              } catch (Throwable t) {
                error("Caught exception while starting grid.", t);
              }

              info("Thread finished.");

              return null;
            }
          },
          5,
          "grid-starter");

      assert G.allGrids().size() == 1;

      assert G.ignite("TEST_NAME") != null;
    } finally {
      G.stopAll(true);
    }
  }
    /**
     * Checks state of the bean.
     *
     * @param gridName Grid name.
     * @param exec Try to execute something on the grid.
     */
    void checkState(String gridName, boolean exec) {
      assert log != null;
      assert appCtx != null;

      assert F.eq(gridName, ignite.name());

      if (exec)
        // Execute any grid method.
        G.ignite(gridName).events().localQuery(F.<Event>alwaysTrue());
    }
  /** @throws Exception If failed. */
  @SuppressWarnings({"AssignmentToCatchBlockParameter"})
  public void testCancel() throws Exception {
    Ignite ignite = G.ignite(getTestGridName());

    ignite
        .compute()
        .localDeployTask(GridCancelTestTask.class, GridCancelTestTask.class.getClassLoader());

    ComputeTaskFuture<?> res0 =
        executeAsync(
            ignite.compute().withTimeout(maxJobExecTime * 2),
            GridCancelTestTask.class.getName(),
            null);

    try {
      Object res = res0.get();

      info("Cancel test result: " + res);

      synchronized (mux) {
        // Every execute must be called.
        assert execCnt <= SPLIT_COUNT : "Invalid execute count: " + execCnt;

        // Job returns 1 if was cancelled.
        assert (Integer) res <= SPLIT_COUNT : "Invalid task result: " + res;

        // Should be exactly the same as Jobs number.
        assert cancelCnt <= SPLIT_COUNT : "Invalid cancel count: " + cancelCnt;

        // One per start and one per stop and some that come with heartbeats.
        assert colResolutionCnt > SPLIT_COUNT + 1
            : "Invalid collision resolution count: " + colResolutionCnt;
      }
    } catch (ComputeTaskTimeoutException e) {
      error("Task execution got timed out.", e);
    } catch (Exception e) {
      assert e.getCause() != null;

      if (e.getCause() instanceof IgniteCheckedException) e = (Exception) e.getCause();

      if (e.getCause() instanceof IOException) e = (Exception) e.getCause();

      assert e.getCause() instanceof InterruptedException
          : "Invalid exception cause: " + e.getCause();
    }
  }
  /**
   * Checks that GridTaskListener is only called once per task.
   *
   * @throws Exception If failed.
   */
  @SuppressWarnings({"BusyWait", "unchecked"})
  public void testGridTaskListener() throws Exception {
    final AtomicInteger cnt = new AtomicInteger(0);

    IgniteInClosure<IgniteFuture<?>> lsnr =
        new CI1<IgniteFuture<?>>() {
          @Override
          public void apply(IgniteFuture<?> fut) {
            assert fut != null;

            cnt.incrementAndGet();
          }
        };

    Ignite ignite = G.ignite(getTestGridName());

    assert ignite != null;

    ignite.compute().localDeployTask(TestTask.class, TestTask.class.getClassLoader());

    ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), TestTask.class.getName(), null);

    fut.listen(lsnr);

    fut.get();

    while (cnt.get() == 0) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        error("Got interrupted while sleep.", e);

        break;
      }
    }

    assert cnt.get() == 1
        : "Unexpected GridTaskListener apply count [count=" + cnt.get() + ", expected=1]";
  }
  /**
   * Aspect implementation which executes grid-enabled methods on remote nodes.
   *
   * <p>{@inheritDoc}
   */
  @SuppressWarnings({
    "ProhibitedExceptionDeclared",
    "ProhibitedExceptionThrown",
    "CatchGenericClass",
    "unchecked"
  })
  @Override
  public Object invoke(MethodInvocation invoc) throws Throwable {
    Method mtd = invoc.getMethod();

    Gridify ann = mtd.getAnnotation(Gridify.class);

    assert ann != null : "Intercepted method does not have gridify annotation.";

    // Since annotations in Java don't allow 'null' as default value
    // we have accept an empty string and convert it here.
    // NOTE: there's unintended behavior when user specifies an empty
    // string as intended grid name.
    // NOTE: the 'ann.gridName() == null' check is added to mitigate
    // annotation bugs in some scripting languages (e.g. Groovy).
    String gridName = F.isEmpty(ann.gridName()) ? null : ann.gridName();

    if (G.state(gridName) != STARTED)
      throw new IgniteCheckedException("Grid is not locally started: " + gridName);

    // Initialize defaults.
    GridifyArgument arg =
        new GridifyArgumentAdapter(
            mtd.getDeclaringClass(),
            mtd.getName(),
            mtd.getParameterTypes(),
            invoc.getArguments(),
            invoc.getThis());

    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
      // Check interceptor first.
      if (!ann.interceptor().newInstance().isGridify(ann, arg)) return invoc.proceed();
    }

    if (!ann.taskClass().equals(GridifyDefaultTask.class) && !ann.taskName().isEmpty())
      throw new IgniteCheckedException(
          "Gridify annotation must specify either Gridify.taskName() or "
              + "Gridify.taskClass(), but not both: "
              + ann);

    try {
      Ignite ignite = G.ignite(gridName);

      if (!ann.taskClass().equals(GridifyDefaultTask.class))
        return ignite
            .compute()
            .withTimeout(ann.timeout())
            .execute((Class<? extends ComputeTask<GridifyArgument, Object>>) ann.taskClass(), arg);

      // If task name was not specified.
      if (ann.taskName().isEmpty())
        return ignite
            .compute()
            .withTimeout(ann.timeout())
            .execute(new GridifyDefaultTask(invoc.getMethod().getDeclaringClass()), arg);

      // If task name was specified.
      return ignite.compute().withTimeout(ann.timeout()).execute(ann.taskName(), arg);
    } catch (Exception e) {
      for (Class<?> ex : invoc.getMethod().getExceptionTypes()) {
        // Descend all levels down.
        Throwable cause = e.getCause();

        while (cause != null) {
          if (ex.isAssignableFrom(cause.getClass())) throw cause;

          cause = cause.getCause();
        }

        if (ex.isAssignableFrom(e.getClass())) throw e;
      }

      throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e);
    }
  }
 /** @throws Exception If test failed. */
 public void testCollisionJobContext() throws Exception {
   G.ignite(getTestGridName()).compute().execute(new GridTestTask(), "some-arg");
 }
  /** {@inheritDoc} */
  @Override
  protected void beforeTestsStarted() throws Exception {
    Ignite ignite = G.ignite(getTestGridName());

    assert ignite != null;
  }