refactor unit test
1.Add test thread timeout setting 2.Add junit5 to initialize different configurations according to the hook functionpull/2694/head
parent
2bfc4d2c15
commit
066587d3b5
@ -0,0 +1,37 @@
|
||||
services:
|
||||
# zookeeper:
|
||||
# image: zookeeper
|
||||
# ports:
|
||||
# - "2181:2181"
|
||||
# restart: on-failure
|
||||
|
||||
nacos:
|
||||
image: zhusaidong/nacos-server-m1:2.0.3
|
||||
container_name: nacos-standalone
|
||||
environment:
|
||||
- PREFER_HOST_MODE=hostname
|
||||
- MODE=standalone
|
||||
ports:
|
||||
- "8848:8848"
|
||||
healthcheck:
|
||||
test: "curl --fail http://127.0.0.1:8848/nacos/v1/console/health/liveness || exit 1"
|
||||
interval: 5s
|
||||
|
||||
# etcd:
|
||||
# image: "quay.io/coreos/etcd:latest"
|
||||
# container_name: etcd
|
||||
# environment:
|
||||
# - ETCDCTL_API=3
|
||||
# command: [
|
||||
# "etcd",
|
||||
# "--name=etcd0",
|
||||
# "--advertise-client-urls=http://127.0.0.1:2379",
|
||||
# "--listen-client-urls=http://0.0.0.0:2379",
|
||||
# "--initial-advertise-peer-urls=http://127.0.0.1:2380",
|
||||
# "--listen-peer-urls=http://0.0.0.0:2380",
|
||||
# "--initial-cluster=etcd0=http://127.0.0.1:2380",
|
||||
# ]
|
||||
# ports:
|
||||
# - "2379:2379"
|
||||
# - "2380:2380"
|
||||
# restart: always
|
@ -0,0 +1,16 @@
|
||||
configdata:
|
||||
user:
|
||||
age: 22
|
||||
name: freeman1123
|
||||
map:
|
||||
hobbies:
|
||||
- art
|
||||
- programming
|
||||
- movie
|
||||
intro: Hello, I'm freeman
|
||||
extra: yo~
|
||||
users:
|
||||
- name: dad
|
||||
age: 20
|
||||
- name: mom
|
||||
age: 18
|
@ -0,0 +1,37 @@
|
||||
services:
|
||||
# zookeeper:
|
||||
# image: zookeeper
|
||||
# ports:
|
||||
# - "2181:2181"
|
||||
# restart: on-failure
|
||||
|
||||
nacos:
|
||||
image: nacos/nacos-server:2.0.1
|
||||
container_name: nacos-standalone
|
||||
environment:
|
||||
- PREFER_HOST_MODE=hostname
|
||||
- MODE=standalone
|
||||
ports:
|
||||
- "8848:8848"
|
||||
healthcheck:
|
||||
test: "curl --fail http://127.0.0.1:8848/nacos/v1/console/health/liveness || exit 1"
|
||||
interval: 5s
|
||||
|
||||
# etcd:
|
||||
# image: "quay.io/coreos/etcd:latest"
|
||||
# container_name: etcd
|
||||
# environment:
|
||||
# - ETCDCTL_API=3
|
||||
# command: [
|
||||
# "etcd",
|
||||
# "--name=etcd0",
|
||||
# "--advertise-client-urls=http://127.0.0.1:2379",
|
||||
# "--listen-client-urls=http://0.0.0.0:2379",
|
||||
# "--initial-advertise-peer-urls=http://127.0.0.1:2380",
|
||||
# "--listen-peer-urls=http://0.0.0.0:2380",
|
||||
# "--initial-cluster=etcd0=http://127.0.0.1:2380",
|
||||
# ]
|
||||
# ports:
|
||||
# - "2379:2379"
|
||||
# - "2380:2380"
|
||||
# restart: always
|
@ -0,0 +1,16 @@
|
||||
configdata:
|
||||
user:
|
||||
age: 22
|
||||
name: freeman1123
|
||||
map:
|
||||
hobbies:
|
||||
- art
|
||||
- programming
|
||||
- movie
|
||||
intro: Hello, I'm freeman
|
||||
extra: yo~
|
||||
users:
|
||||
- name: dad
|
||||
age: 20
|
||||
- name: mom
|
||||
age: 18
|
@ -0,0 +1,9 @@
|
||||
package com.alibaba.cloud.testsupport;
|
||||
|
||||
public class Constant {
|
||||
|
||||
//超时默认设置为5s
|
||||
public static final long TIME_OUT = 5000;
|
||||
|
||||
public static final String REFRESH_CONFIG = "nacos-config-refresh.yml";
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Licensed to 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. Apache Software Foundation (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 com.alibaba.cloud.testsupport;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
@Inherited
|
||||
@Testcontainers
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
@ExtendWith(SpringCloudAlibabaExtension.class)
|
||||
public @interface SpringCloudAlibaba {
|
||||
String[] composeFiles();
|
||||
|
||||
String serviceName();
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Licensed to 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. Apache Software Foundation (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 com.alibaba.cloud.testsupport;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.testcontainers.Testcontainers;
|
||||
import org.testcontainers.containers.DockerComposeContainer;
|
||||
import org.testcontainers.shaded.org.awaitility.Awaitility;
|
||||
|
||||
@Slf4j
|
||||
final class SpringCloudAlibabaExtension
|
||||
implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback {
|
||||
private final boolean LOCAL_MODE = Objects.equals(System.getProperty("local"), "true");
|
||||
|
||||
|
||||
private DockerComposeContainer<?> compose;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
public void beforeAll(ExtensionContext context) throws IOException {
|
||||
Awaitility.setDefaultTimeout(Duration.ofSeconds(60));
|
||||
Awaitility.setDefaultPollInterval(Duration.ofSeconds(10));
|
||||
|
||||
|
||||
if (LOCAL_MODE) {
|
||||
runInLocal();
|
||||
} else {
|
||||
runInDockerContainer(context);
|
||||
}
|
||||
|
||||
//
|
||||
// final Class<?> clazz = context.getRequiredTestClass();
|
||||
// Stream.of(clazz.getDeclaredFields())
|
||||
// .filter(it -> Modifier.isStatic(it.getModifiers()));
|
||||
}
|
||||
|
||||
private void runInLocal() {
|
||||
Testcontainers.exposeHostPorts(3000);
|
||||
}
|
||||
|
||||
private void runInDockerContainer(ExtensionContext context) {
|
||||
compose = createDockerCompose(context);
|
||||
compose.start();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterAll(ExtensionContext context) {
|
||||
if (compose != null) {
|
||||
compose.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext context) {
|
||||
final Object instance = context.getRequiredTestInstance();
|
||||
Stream.of(instance.getClass().getDeclaredFields());
|
||||
}
|
||||
|
||||
private DockerComposeContainer<?> createDockerCompose(ExtensionContext context) {
|
||||
final Class<?> clazz = context.getRequiredTestClass();
|
||||
final SpringCloudAlibaba annotation = clazz.getAnnotation(
|
||||
SpringCloudAlibaba.class);
|
||||
final List<File> files = Stream.of(annotation.composeFiles())
|
||||
.map(it -> SpringCloudAlibaba.class.getClassLoader().getResource(it))
|
||||
.filter(Objects::nonNull)
|
||||
.map(URL::getPath)
|
||||
.map(File::new)
|
||||
.collect(Collectors.toList());
|
||||
compose = new DockerComposeContainer<>(files)
|
||||
.withPull(true)
|
||||
.withTailChildContainers(true);
|
||||
|
||||
return compose;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.alibaba.cloud.testsupport;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@ExtendWith(TestTimeoutExtension.class)
|
||||
public @interface TestExtend {
|
||||
|
||||
//设置超时时间
|
||||
long time();
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.alibaba.cloud.testsupport;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TestTimeoutExtension implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback {
|
||||
|
||||
@Override
|
||||
public void afterAll(ExtensionContext context) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeAll(ExtensionContext context) throws Exception {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext context) throws Exception {
|
||||
final Class<?> clazz = context.getRequiredTestClass();
|
||||
final TestExtend annotation = clazz.getAnnotation(
|
||||
TestExtend.class);
|
||||
ScheduledExecutorService singlonThread = Executors.newSingleThreadScheduledExecutor();
|
||||
while (!singlonThread.awaitTermination( annotation.time(), TimeUnit.MILLISECONDS)){
|
||||
singlonThread.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue