You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/07/07 04:35:21 UTC
[shardingsphere-elasticjob-lite] branch master updated: Add test
case for snapshot spring namespace (#968) (#978)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob-lite.git
The following commit(s) were added to refs/heads/master by this push:
new 2862573 Add test case for snapshot spring namespace (#968) (#978)
2862573 is described below
commit 2862573d7964929be798075542a7018ae4693421
Author: Tboy <gu...@immomo.com>
AuthorDate: Tue Jul 7 12:35:11 2020 +0800
Add test case for snapshot spring namespace (#968) (#978)
---
.../lite/internal/snapshot/SnapshotService.java | 7 +++
.../SnapshotSpringNamespaceDisableTest.java | 34 ++++++++++++++
.../SnapshotSpringNamespaceEnableTest.java | 35 ++++++++++++++
.../lite/spring/snapshot/SocketUtils.java | 53 ++++++++++++++++++++++
.../META-INF/snapshot/snapshotDisabled.xml | 38 ++++++++++++++++
.../META-INF/snapshot/snapshotEnabled.xml | 40 ++++++++++++++++
6 files changed, 207 insertions(+)
diff --git a/elastic-job-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/snapshot/SnapshotService.java b/elastic-job-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/snapshot/SnapshotService.java
index b3147ce..9d1c7c1 100644
--- a/elastic-job-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/snapshot/SnapshotService.java
+++ b/elastic-job-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/snapshot/SnapshotService.java
@@ -77,12 +77,19 @@ public final class SnapshotService {
try {
process(serverSocket.accept());
} catch (final IOException ex) {
+ if (isIgnoredException()) {
+ return;
+ }
log.error("Elastic job: Snapshot service open socket failure, error is: ", ex);
}
}
}).start();
}
+ private boolean isIgnoredException() {
+ return serverSocket.isClosed();
+ }
+
private void process(final Socket socket) throws IOException {
try (
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
diff --git a/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SnapshotSpringNamespaceDisableTest.java b/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SnapshotSpringNamespaceDisableTest.java
new file mode 100644
index 0000000..a8fa177
--- /dev/null
+++ b/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SnapshotSpringNamespaceDisableTest.java
@@ -0,0 +1,34 @@
+/*
+ * 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.shardingsphere.elasticjob.lite.spring.snapshot;
+
+import org.apache.shardingsphere.elasticjob.lite.internal.snapshot.SnapshotService;
+import org.apache.shardingsphere.elasticjob.lite.spring.test.AbstractZookeeperJUnit4SpringContextTests;
+import org.junit.Test;
+import org.springframework.test.context.ContextConfiguration;
+
+import java.io.IOException;
+
+@ContextConfiguration(locations = "classpath:META-INF/snapshot/snapshotDisabled.xml")
+public final class SnapshotSpringNamespaceDisableTest extends AbstractZookeeperJUnit4SpringContextTests {
+
+ @Test(expected = IOException.class)
+ public void assertSnapshotDisable() throws IOException {
+ SocketUtils.sendCommand(SnapshotService.DUMP_COMMAND, 9998);
+ }
+}
diff --git a/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SnapshotSpringNamespaceEnableTest.java b/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SnapshotSpringNamespaceEnableTest.java
new file mode 100644
index 0000000..889bf43
--- /dev/null
+++ b/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SnapshotSpringNamespaceEnableTest.java
@@ -0,0 +1,35 @@
+/*
+ * 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.shardingsphere.elasticjob.lite.spring.snapshot;
+
+import org.apache.shardingsphere.elasticjob.lite.spring.test.AbstractZookeeperJUnit4SpringContextTests;
+import org.junit.Test;
+import org.springframework.test.context.ContextConfiguration;
+
+import java.io.IOException;
+
+import static org.junit.Assert.assertNull;
+
+@ContextConfiguration(locations = "classpath:META-INF/snapshot/snapshotEnabled.xml")
+public final class SnapshotSpringNamespaceEnableTest extends AbstractZookeeperJUnit4SpringContextTests {
+
+ @Test
+ public void assertSnapshotEnable() throws IOException {
+ assertNull(SocketUtils.sendCommand("unknown_command", 9988));
+ }
+}
diff --git a/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SocketUtils.java b/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SocketUtils.java
new file mode 100644
index 0000000..98dc5fb
--- /dev/null
+++ b/elastic-job-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/snapshot/SocketUtils.java
@@ -0,0 +1,53 @@
+/*
+ * 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.shardingsphere.elasticjob.lite.spring.snapshot;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.Socket;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SocketUtils {
+
+ /**
+ * Send command.
+ *
+ * @param command command
+ * @param dumpPort the port for dumping data
+ * @return result of command
+ * @throws IOException io exception
+ */
+ public static String sendCommand(final String command, final int dumpPort) throws IOException {
+ try (
+ Socket socket = new Socket("127.0.0.1", dumpPort);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))
+ ) {
+ writer.write(command);
+ writer.newLine();
+ writer.flush();
+ return reader.readLine();
+ }
+ }
+}
diff --git a/elastic-job-lite-spring/src/test/resources/META-INF/snapshot/snapshotDisabled.xml b/elastic-job-lite-spring/src/test/resources/META-INF/snapshot/snapshotDisabled.xml
new file mode 100644
index 0000000..c42f7c4
--- /dev/null
+++ b/elastic-job-lite-spring/src/test/resources/META-INF/snapshot/snapshotDisabled.xml
@@ -0,0 +1,38 @@
+<?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"
+ xmlns:elasticjob="http://elasticjob.shardingsphere.apache.org/schema/elasticjob"
+ 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
+ http://elasticjob.shardingsphere.apache.org/schema/elasticjob
+ http://elasticjob.shardingsphere.apache.org/schema/elasticjob/elasticjob.xsd
+ ">
+
+ <context:property-placeholder location="classpath:conf/reg/conf.properties" ignore-unresolvable="true"/>
+ <elasticjob:zookeeper id="regCenter1" server-lists="${regCenter1.serverLists}" namespace="${regCenter1.namespace}"
+ base-sleep-time-milliseconds="${regCenter1.baseSleepTimeMilliseconds}"
+ max-sleep-time-milliseconds="${regCenter1.maxSleepTimeMilliseconds}"
+ max-retries="${regCenter1.maxRetries}"/>
+
+ <elasticjob:snapshot id="jobSnapshotDisabled" registry-center-ref="regCenter1" dump-port="-1"/>
+</beans>
diff --git a/elastic-job-lite-spring/src/test/resources/META-INF/snapshot/snapshotEnabled.xml b/elastic-job-lite-spring/src/test/resources/META-INF/snapshot/snapshotEnabled.xml
new file mode 100644
index 0000000..5791966
--- /dev/null
+++ b/elastic-job-lite-spring/src/test/resources/META-INF/snapshot/snapshotEnabled.xml
@@ -0,0 +1,40 @@
+<?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"
+ xmlns:elasticjob="http://elasticjob.shardingsphere.apache.org/schema/elasticjob"
+ 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
+ http://elasticjob.shardingsphere.apache.org/schema/elasticjob
+ http://elasticjob.shardingsphere.apache.org/schema/elasticjob/elasticjob.xsd
+ ">
+
+ <context:property-placeholder location="classpath:conf/job/conf.properties" ignore-unresolvable="true"/>
+
+ <elasticjob:zookeeper id="regCenter" server-lists="${regCenter.serverLists}" namespace="${regCenter.namespace}"
+ base-sleep-time-milliseconds="${regCenter.baseSleepTimeMilliseconds}"
+ max-sleep-time-milliseconds="${regCenter.maxSleepTimeMilliseconds}"
+ max-retries="${regCenter.maxRetries}"/>
+
+ <elasticjob:snapshot id="jobSnapshotEnabled" registry-center-ref="regCenter" dump-port="9988"/>
+
+</beans>