You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2016/04/29 11:01:54 UTC

ignite git commit: ignite-3024 Added test.

Repository: ignite
Updated Branches:
  refs/heads/ignite-3024 cf71130bc -> 6d1fa2168


ignite-3024 Added test.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/6d1fa216
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/6d1fa216
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/6d1fa216

Branch: refs/heads/ignite-3024
Commit: 6d1fa21686c5968fe14e4f0cb74aaf412c7908f3
Parents: cf71130
Author: sboikov <sb...@gridgain.com>
Authored: Fri Apr 29 12:01:45 2016 +0300
Committer: sboikov <sb...@gridgain.com>
Committed: Fri Apr 29 12:01:45 2016 +0300

----------------------------------------------------------------------
 .../GridServiceInjectionSpringResourceTest.java | 245 +++++++++++++++++++
 .../spring/injection/spring-resource.tmpl.xml   |  66 +++++
 .../testsuites/IgniteSpringTestSuite.java       |   3 +
 3 files changed, 314 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/6d1fa216/modules/spring/src/test/java/org/apache/ignite/spring/injection/GridServiceInjectionSpringResourceTest.java
----------------------------------------------------------------------
diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/injection/GridServiceInjectionSpringResourceTest.java b/modules/spring/src/test/java/org/apache/ignite/spring/injection/GridServiceInjectionSpringResourceTest.java
new file mode 100644
index 0000000..2c1f247
--- /dev/null
+++ b/modules/spring/src/test/java/org/apache/ignite/spring/injection/GridServiceInjectionSpringResourceTest.java
@@ -0,0 +1,245 @@
+/*
+ * 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.spring.injection;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.lang.IgniteCallable;
+import org.apache.ignite.resources.ServiceResource;
+import org.apache.ignite.resources.SpringResource;
+import org.apache.ignite.services.Service;
+import org.apache.ignite.services.ServiceContext;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.FileSystemXmlApplicationContext;
+
+import javax.annotation.PostConstruct;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+/**
+ * Tests for injected service.
+ */
+public class GridServiceInjectionSpringResourceTest extends GridCommonAbstractTest {
+    /** Service name. */
+    private static final String SERVICE_NAME = "testService";
+
+    /** Bean name. */
+    private static final String DUMMY_BEAN = "dummyResourceBean";
+
+    /** */
+    private static final int NODES = 8;
+
+    /** */
+    private static final int TEST_ITERATIONS = 5;
+
+    /** */
+    private static FileSystem FS = FileSystems.getDefault();
+
+    /** */
+    private static final String springCfgFileTemplate =
+        "src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml";
+
+    /** */
+    private static String springConfigFileOutTmplName = "spring-resource.xml-";
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+
+        for (int i = 0; i < NODES; ++i)
+            Files.deleteIfExists(FS.getPath(springConfigFileOutTmplName + i));
+
+        super.afterTest();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testDeployServiceWithSpring() throws Exception {
+        generateConfigXmls(NODES);
+
+        for (int i = 0; i < TEST_ITERATIONS; ++i) {
+            log.info("Iteration: " + i);
+
+            doOneTestIteration();
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    private void doOneTestIteration() throws Exception {
+        startNodes();
+
+        for (int i = 0; i < 10; ++i) {
+            for (Ignite ignite : G.allGrids())
+                ignite.compute().call(new TestJob());
+        }
+
+        stopAllGrids();
+    }
+
+    /**
+     *
+     * @throws Exception If failed.
+     */
+    private void startNodes() throws Exception {
+        AbstractApplicationContext ctxSpring = new FileSystemXmlApplicationContext(springConfigFileOutTmplName + 0);
+
+        // We have to deploy Services for service is available at the bean creation time for other nodes.
+        Ignite ignite = (Ignite)ctxSpring.getBean("testIgnite");
+
+        ignite.services().deployMultiple(SERVICE_NAME, new DummyServiceImpl(), NODES, 1);
+
+        // Add other nodes.
+        for (int i = 1; i < NODES; ++i)
+            new FileSystemXmlApplicationContext(springConfigFileOutTmplName + i);
+
+        assertEquals(NODES, G.allGrids().size());
+        assertEquals(NODES, ignite.cluster().nodes().size());
+    }
+
+    /**
+     * @param nodes Nodes.
+     * @throws IOException on read/write config error
+     */
+    private void generateConfigXmls(int nodes) throws IOException {
+        StringBuilder cfg = new StringBuilder();
+
+        try (BufferedReader br = Files.newBufferedReader(FS.getPath(springCfgFileTemplate), UTF_8)) {
+            while (true) {
+                String str = br.readLine();
+
+                if (str == null)
+                    break;
+
+                cfg.append(str);
+            }
+        }
+
+        for (int i = 0; i < nodes; ++i) {
+            try (BufferedWriter bw = Files.newBufferedWriter(FS.getPath(springConfigFileOutTmplName + i), UTF_8)) {
+                String str = cfg.toString();
+
+                str = str.replaceAll("@GRID_IDX@", Integer.toString(i));
+
+                bw.write(str);
+            }
+        }
+    }
+
+    /**
+     *
+     */
+    private static class TestJob implements IgniteCallable {
+        /** */
+        @ServiceResource(serviceName = SERVICE_NAME, proxyInterface = DummyService.class)
+        private DummyService svc;
+
+        /** {@inheritDoc} */
+        @Override public Object call() throws Exception {
+            assertNotNull(svc);
+
+            svc.noop();
+
+            return null;
+        }
+    }
+
+    /**
+     * Dummy Service.
+     */
+    public interface DummyService {
+        /**
+         *
+         */
+        void noop();
+    }
+
+    /**
+     * No-op test service.
+     */
+    public static class DummyServiceImpl implements DummyService, Service {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** */
+        @SpringResource(resourceName = DUMMY_BEAN)
+        private transient DummyResourceBean dummyResourceBean;
+
+        /** {@inheritDoc} */
+        @Override public void noop() {
+            System.out.println("DummyServiceImpl.noop()");
+        }
+
+        /** {@inheritDoc} */
+        @Override public void cancel(ServiceContext ctx) {
+            System.out.println("Cancelling service: " + ctx.name());
+        }
+
+        /** {@inheritDoc} */
+        @Override public void init(ServiceContext ctx) throws Exception {
+            System.out.println("Initializing service: " + ctx.name());
+        }
+
+        /** {@inheritDoc} */
+        @Override public void execute(ServiceContext ctx) {
+            System.out.println("Executing service: " + ctx.name());
+        }
+    }
+
+    /**
+     * Dummy resource bean.
+     */
+    public static class DummyResourceBean {
+        /** */
+        private transient Ignite ignite;
+
+        /**
+         * @return Ignite.
+         */
+        public Ignite getIgnite() {
+            return ignite;
+        }
+
+        /**
+         * @param ignite Ignite.
+         */
+        public void setIgnite(Ignite ignite) {
+            this.ignite = ignite;
+        }
+
+        /**
+         * @throws Exception If failed.
+         */
+        @PostConstruct
+        public void init() throws Exception {
+            DummyService srv = ignite.services().serviceProxy(SERVICE_NAME, DummyService.class, false);
+
+            assertNotNull(srv);
+        }
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/6d1fa216/modules/spring/src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml
----------------------------------------------------------------------
diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml b/modules/spring/src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml
new file mode 100644
index 0000000..aeee4ec
--- /dev/null
+++ b/modules/spring/src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xsi:schemaLocation="
+        http://www.springframework.org/schema/beans
+        http://www.springframework.org/schema/beans/spring-beans.xsd
+        http://www.springframework.org/schema/context
+        http://www.springframework.org/schema/context/spring-context.xsd">
+
+    <context:annotation-config/>
+
+    <bean id="dummyResourceBean"
+          class="org.apache.ignite.spring.injection.GridServiceInjectionSpringResourceTest$DummyResourceBean">
+        <property name="ignite" ref="testIgnite"/>
+    </bean>
+
+
+    <bean id="testIgnite" class="org.apache.ignite.IgniteSpringBean">
+        <property name="configuration">
+            <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
+                <property name="localHost" value="127.0.0.1"/>
+
+                <property name="gridName" value="springTest@GRID_IDX@"/>
+
+                <property name="discoverySpi">
+                    <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
+                        <property name="ipFinder">
+                            <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
+                                <property name="addresses">
+                                    <list>
+                                        <value>127.0.0.1:47500</value>
+                                        <value>127.0.0.1:47501</value>
+                                        <value>127.0.0.1:47502</value>
+                                        <value>127.0.0.1:47503</value>
+                                        <value>127.0.0.1:47504</value>
+                                        <value>127.0.0.1:47505</value>
+                                        <value>127.0.0.1:47506</value>
+                                        <value>127.0.0.1:47507</value>
+                                    </list>
+                                </property>
+                            </bean>
+                        </property>
+                    </bean>
+                </property>
+            </bean>
+        </property>
+    </bean>
+</beans>

http://git-wip-us.apache.org/repos/asf/ignite/blob/6d1fa216/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
index c42c7e0..4fc159e 100644
--- a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
+++ b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
@@ -28,6 +28,7 @@ import org.apache.ignite.p2p.GridP2PUserVersionChangeSelfTest;
 import org.apache.ignite.spring.GridSpringCacheManagerSelfTest;
 import org.apache.ignite.spring.IgniteExcludeInConfigurationTest;
 import org.apache.ignite.spring.IgniteStartFromStreamConfigurationTest;
+import org.apache.ignite.spring.injection.GridServiceInjectionSpringResourceTest;
 import org.apache.ignite.transactions.spring.GridSpringTransactionManagerSelfTest;
 
 /**
@@ -65,6 +66,8 @@ public class IgniteSpringTestSuite extends TestSuite {
 
         suite.addTest(new TestSuite(GridSpringTransactionManagerSelfTest.class));
 
+        suite.addTestSuite(GridServiceInjectionSpringResourceTest.class);
+
         return suite;
     }
 }