| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You 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 org.apache.ignite.internal; |
| |
| import java.util.Collection; |
| import org.apache.ignite.GridTestJob; |
| import org.apache.ignite.GridTestTask; |
| import org.apache.ignite.Ignite; |
| import org.apache.ignite.IgniteLogger; |
| import org.apache.ignite.configuration.IgniteConfiguration; |
| import org.apache.ignite.internal.util.typedef.G; |
| import org.apache.ignite.resources.LoggerResource; |
| import org.apache.ignite.spi.IgniteSpiAdapter; |
| import org.apache.ignite.spi.IgniteSpiException; |
| import org.apache.ignite.spi.IgniteSpiMultipleInstancesSupport; |
| import org.apache.ignite.spi.collision.CollisionContext; |
| import org.apache.ignite.spi.collision.CollisionExternalListener; |
| import org.apache.ignite.spi.collision.CollisionJobContext; |
| import org.apache.ignite.spi.collision.CollisionSpi; |
| import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; |
| import org.apache.ignite.testframework.junits.common.GridCommonTest; |
| import org.junit.Test; |
| |
| /** |
| * Collision job context test. |
| */ |
| @GridCommonTest(group = "Kernal Self") |
| public class GridCollisionJobsContextSelfTest extends GridCommonAbstractTest { |
| /** */ |
| public GridCollisionJobsContextSelfTest() { |
| super(/*start grid*/true); |
| } |
| |
| /** {@inheritDoc} */ |
| @Override protected void beforeTestsStarted() throws Exception { |
| Ignite ignite = G.ignite(getTestIgniteInstanceName()); |
| |
| assert ignite != null; |
| } |
| |
| /** {@inheritDoc} */ |
| @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { |
| IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); |
| |
| cfg.setCollisionSpi(new TestCollisionSpi()); |
| |
| return cfg; |
| } |
| |
| /** |
| * @throws Exception If test failed. |
| */ |
| @Test |
| public void testCollisionJobContext() throws Exception { |
| G.ignite(getTestIgniteInstanceName()).compute().execute(new GridTestTask(), "some-arg"); |
| } |
| |
| /** */ |
| @SuppressWarnings( {"PublicInnerClass"}) |
| @IgniteSpiMultipleInstancesSupport(true) |
| public static class TestCollisionSpi extends IgniteSpiAdapter implements CollisionSpi { |
| /** Grid logger. */ |
| @LoggerResource |
| private IgniteLogger log; |
| |
| /** {@inheritDoc} */ |
| @Override public void onCollision(CollisionContext ctx) { |
| Collection<CollisionJobContext> activeJobs = ctx.activeJobs(); |
| Collection<CollisionJobContext> waitJobs = ctx.waitingJobs(); |
| |
| assert waitJobs != null; |
| assert activeJobs != null; |
| |
| for (CollisionJobContext job : waitJobs) { |
| assert job.getJob() != null; |
| assert job.getJobContext() != null; |
| assert job.getTaskSession() != null; |
| |
| assert job.getJob() instanceof GridTestJob : job.getJob(); |
| |
| job.activate(); |
| } |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public void spiStart(String igniteInstanceName) throws IgniteSpiException { |
| // Start SPI start stopwatch. |
| startStopwatch(); |
| |
| // Ack start. |
| if (log.isInfoEnabled()) |
| log.info(startInfo()); |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public void spiStop() throws IgniteSpiException { |
| // Ack stop. |
| if (log.isInfoEnabled()) |
| log.info(stopInfo()); |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public void setExternalCollisionListener(CollisionExternalListener lsnr) { |
| // No-op. |
| } |
| } |
| } |