乐趣区

关于spring-cloud:SpringCloud升级之路20200x版40-spock-单元测试封装的-WebClient上

本系列代码地址:https://github.com/JoJoTec/sp…

咱们来测试下后面封装好的 WebClient,这里开始,咱们应用 spock 编写 groovy 单元测试,这种编写进去的单元测试,代码更加简洁,同时更加灵便,咱们在接下来的单元测试代码中就能看进去。

编写基于 spock 的 spring-boot context 测试

咱们退出后面设计的配置,编写测试类:

@SpringBootTest(
        properties = [
                "webclient.configs.testServiceWithCannotConnect.baseUrl=http://testServiceWithCannotConnect",
                "webclient.configs.testServiceWithCannotConnect.serviceName=testServiceWithCannotConnect",
                "webclient.configs.testService.baseUrl=http://testService",
                "webclient.configs.testService.serviceName=testService",
                "webclient.configs.testService.responseTimeout=1s",
                "webclient.configs.testService.retryablePaths[0]=/delay/3",
                "webclient.configs.testService.retryablePaths[1]=/status/4*",
                "spring.cloud.loadbalancer.zone=zone1",
                "resilience4j.retry.configs.default.maxAttempts=3",
                "resilience4j.circuitbreaker.configs.default.failureRateThreshold=50",
                "resilience4j.circuitbreaker.configs.default.slidingWindowType=TIME_BASED",
                "resilience4j.circuitbreaker.configs.default.slidingWindowSize=5",
                // 因为重试是 3 次,为了避免断路器关上影响测试,设置为正好比重试多一次的次数,避免触发
                // 同时咱们在测试的时候也须要手动清空断路器统计
                "resilience4j.circuitbreaker.configs.default.minimumNumberOfCalls=4",
                "resilience4j.circuitbreaker.configs.default.recordExceptions=java.lang.Exception"
        ],
        classes = MockConfig
)
class WebClientUnitTest extends Specification {
    @SpringBootApplication
    static class MockConfig {}}

咱们退出三个服务实例供单元测试调用:

class WebClientUnitTest extends Specification {def zone1Instance1 = new DefaultServiceInstance(instanceId: "instance1", host: "www.httpbin.org", port: 80, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
    def zone1Instance2 = new DefaultServiceInstance(instanceId: "instance2", host: "www.httpbin.org", port: 8081, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
    def zone1Instance3 = new DefaultServiceInstance(instanceId: "instance3", host: "httpbin.org", port: 80, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
}

咱们要动静的指定负载平衡获取服务实例列表的响应,即去 Mock 负载均衡器的 ServiceInstanceListSupplier 并笼罩:

class WebClientUnitTest extends Specification {

    @Autowired
    private Tracer tracer
    @Autowired
    private ServiceInstanceMetrics serviceInstanceMetrics
    
    RoundRobinWithRequestSeparatedPositionLoadBalancer loadBalancerClientFactoryInstance = Spy();
    ServiceInstanceListSupplier serviceInstanceListSupplier = Spy();
    
    // 所有测试的办法执行前会调用的办法
    def setup() {
        // 初始化 loadBalancerClientFactoryInstance 负载均衡器
        loadBalancerClientFactoryInstance.setTracer(tracer)
        loadBalancerClientFactoryInstance.setServiceInstanceMetrics(serviceInstanceMetrics)
        loadBalancerClientFactoryInstance.setServiceInstanceListSupplier(serviceInstanceListSupplier)
    }
}

之后,咱们能够通过上面的 groovy 代码,动静指定微服务返回实例:

// 指定 testService 微服务的 LoadBalancer 为 loadBalancerClientFactoryInstance
loadBalancerClientFactory.getInstance("testService") >> loadBalancerClientFactoryInstance
// 指定 testService 微服务实例列表为 zone1Instance1, zone1Instance3
serviceInstanceListSupplier.get() >> Flux.just(Lists.newArrayList(zone1Instance1, zone1Instance3))

测试断路器异样重试以及断路器级别

咱们须要验证:

  • 对于断路器关上的异样,因为没有申请收回去,所以 须要间接重试其余的实例 。咱们能够设立一个微服务,蕴含两个实例,将 其中一个实例的某个门路断路器关上 ,之后屡次调用这个微服务的这个门路接口,看是否都调用胜利(因为有重试,所以每次调用都会胜利)。同时验证,对于负载均衡器获取服务实例的调用, 多于调用次数(每次重试都会调用负载均衡器获取一个新的实例用于调用)
  • 某个门路断路器关上的时候,其余门路断路器不会关上 。在下面关上一个微服务某个实例的一个门路的断路器之后,咱们调用其余的门路, 无论多少次,都胜利并且调用负载均衡器获取服务实例的次数等于调用次数,代表没有重试,也就是没有断路器异样

编写代码:

@SpringBootTest(
        properties = [
                "webclient.configs.testServiceWithCannotConnect.baseUrl=http://testServiceWithCannotConnect",
                "webclient.configs.testServiceWithCannotConnect.serviceName=testServiceWithCannotConnect",
                "webclient.configs.testService.baseUrl=http://testService",
                "webclient.configs.testService.serviceName=testService",
                "webclient.configs.testService.responseTimeout=1s",
                "webclient.configs.testService.retryablePaths[0]=/delay/3",
                "webclient.configs.testService.retryablePaths[1]=/status/4*",
                "spring.cloud.loadbalancer.zone=zone1",
                "resilience4j.retry.configs.default.maxAttempts=3",
                "resilience4j.circuitbreaker.configs.default.failureRateThreshold=50",
                "resilience4j.circuitbreaker.configs.default.slidingWindowType=TIME_BASED",
                "resilience4j.circuitbreaker.configs.default.slidingWindowSize=5",
                // 因为重试是 3 次,为了避免断路器关上影响测试,设置为正好比重试多一次的次数,避免触发
                // 同时咱们在测试的时候也须要手动清空断路器统计
                "resilience4j.circuitbreaker.configs.default.minimumNumberOfCalls=4",
                "resilience4j.circuitbreaker.configs.default.recordExceptions=java.lang.Exception"
        ],
        classes = MockConfig
)
class WebClientUnitTest extends Specification {
    @SpringBootApplication
    static class MockConfig { }
    @SpringBean
    private LoadBalancerClientFactory loadBalancerClientFactory = Mock()

    @Autowired
    private CircuitBreakerRegistry circuitBreakerRegistry
    @Autowired
    private Tracer tracer
    @Autowired
    private ServiceInstanceMetrics serviceInstanceMetrics
    @Autowired
    private WebClientNamedContextFactory webClientNamedContextFactory

    // 不同的测试方法的类对象不是同一个对象,会从新生成,保障相互没有影响
    def zone1Instance1 = new DefaultServiceInstance(instanceId: "instance1", host: "www.httpbin.org", port: 80, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
    def zone1Instance2 = new DefaultServiceInstance(instanceId: "instance2", host: "www.httpbin.org", port: 8081, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
    def zone1Instance3 = new DefaultServiceInstance(instanceId: "instance3", host: "httpbin.org", port: 80, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
    RoundRobinWithRequestSeparatedPositionLoadBalancer loadBalancerClientFactoryInstance = Spy();
    ServiceInstanceListSupplier serviceInstanceListSupplier = Spy();

    // 所有测试的办法执行前会调用的办法
    def setup() {
        // 初始化 loadBalancerClientFactoryInstance 负载均衡器
        loadBalancerClientFactoryInstance.setTracer(tracer)
        loadBalancerClientFactoryInstance.setServiceInstanceMetrics(serviceInstanceMetrics)
        loadBalancerClientFactoryInstance.setServiceInstanceListSupplier(serviceInstanceListSupplier)
    }

    def "测试断路器异样重试以及断路器级别"() {
        given: "设置 testService 的实例都是失常实例"
            loadBalancerClientFactory.getInstance("testService") >> loadBalancerClientFactoryInstance
            serviceInstanceListSupplier.get() >> Flux.just(Lists.newArrayList(zone1Instance1, zone1Instance3))
        when: "断路器关上"
            // 革除断路器影响
            circuitBreakerRegistry.getAllCircuitBreakers().forEach({ c -> c.reset() })
            loadBalancerClientFactoryInstance = (RoundRobinWithRequestSeparatedPositionLoadBalancer) loadBalancerClientFactory.getInstance("testService")
            def breaker
            try {breaker = circuitBreakerRegistry.circuitBreaker("httpbin.org:80/anything", "testService")
            } catch (ConfigurationNotFoundException e) {breaker = circuitBreakerRegistry.circuitBreaker("httpbin.org:80/anything")
            }
            // 关上实例 3 的断路器
            breaker.transitionToOpenState()
            // 调用 10 次
            for (i in 0..<10) {Mono<String> stringMono = webClientNamedContextFactory.getWebClient("testService")
                                                                      .get().uri("/anything").retrieve()
                                                                      .bodyToMono(String.class)
                println(stringMono.block())
            }
        then:"调用至多 10 次负载均衡器且没有异样即胜利"
            (10.._) * loadBalancerClientFactoryInstance.getInstanceResponseByRoundRobin(*_)
        when: "调用不同的门路,验证断路器在这个门路上都是敞开"
            // 调用 10 次
            for (i in 0..<10) {Mono<String> stringMono = webClientNamedContextFactory.getWebClient("testService")
                                                                      .get().uri("/status/200").retrieve()
                                                                      .bodyToMono(String.class)
                println(stringMono.block())
            }
        then: "调用必须为正好 10 次代表没有重试,一次胜利,断路器之间互相隔离"
            10 * loadBalancerClientFactoryInstance.getInstanceResponseByRoundRobin(*_)
    }
}

测试针对 connectTimeout 重试

对于连贯超时,咱们须要验证:无论是否能够重试的办法或者门路,都必须重试,因为申请并没有真的收回去。能够这样验证:设置微服务 testServiceWithCannotConnect 一个实例失常,另一个实例会连贯超时,咱们配置了重试 3 次,所以每次申请应该都能胜利,并且随着程序运行,前面的调用不可用的实例还会被断路,照样能够胜利调用。

@SpringBootTest(
        properties = [
                "webclient.configs.testServiceWithCannotConnect.baseUrl=http://testServiceWithCannotConnect",
                "webclient.configs.testServiceWithCannotConnect.serviceName=testServiceWithCannotConnect",
                "webclient.configs.testService.baseUrl=http://testService",
                "webclient.configs.testService.serviceName=testService",
                "webclient.configs.testService.responseTimeout=1s",
                "webclient.configs.testService.retryablePaths[0]=/delay/3",
                "webclient.configs.testService.retryablePaths[1]=/status/4*",
                "spring.cloud.loadbalancer.zone=zone1",
                "resilience4j.retry.configs.default.maxAttempts=3",
                "resilience4j.circuitbreaker.configs.default.failureRateThreshold=50",
                "resilience4j.circuitbreaker.configs.default.slidingWindowType=TIME_BASED",
                "resilience4j.circuitbreaker.configs.default.slidingWindowSize=5",
                // 因为重试是 3 次,为了避免断路器关上影响测试,设置为正好比重试多一次的次数,避免触发
                // 同时咱们在测试的时候也须要手动清空断路器统计
                "resilience4j.circuitbreaker.configs.default.minimumNumberOfCalls=4",
                "resilience4j.circuitbreaker.configs.default.recordExceptions=java.lang.Exception"
        ],
        classes = MockConfig
)
class WebClientUnitTest extends Specification {
    @SpringBootApplication
    static class MockConfig { }
    @SpringBean
    private LoadBalancerClientFactory loadBalancerClientFactory = Mock()

    @Autowired
    private CircuitBreakerRegistry circuitBreakerRegistry
    @Autowired
    private Tracer tracer
    @Autowired
    private ServiceInstanceMetrics serviceInstanceMetrics
    @Autowired
    private WebClientNamedContextFactory webClientNamedContextFactory

    // 不同的测试方法的类对象不是同一个对象,会从新生成,保障相互没有影响
    def zone1Instance1 = new DefaultServiceInstance(instanceId: "instance1", host: "www.httpbin.org", port: 80, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
    def zone1Instance2 = new DefaultServiceInstance(instanceId: "instance2", host: "www.httpbin.org", port: 8081, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
    def zone1Instance3 = new DefaultServiceInstance(instanceId: "instance3", host: "httpbin.org", port: 80, metadata: Map.ofEntries(Map.entry("zone", "zone1")))
    RoundRobinWithRequestSeparatedPositionLoadBalancer loadBalancerClientFactoryInstance = Spy();
    ServiceInstanceListSupplier serviceInstanceListSupplier = Spy();

    // 所有测试的办法执行前会调用的办法
    def setup() {
        // 初始化 loadBalancerClientFactoryInstance 负载均衡器
        loadBalancerClientFactoryInstance.setTracer(tracer)
        loadBalancerClientFactoryInstance.setServiceInstanceMetrics(serviceInstanceMetrics)
        loadBalancerClientFactoryInstance.setServiceInstanceListSupplier(serviceInstanceListSupplier)
    }

    def "测试针对 connectTimeout 重试"() {
        given: "设置微服务 testServiceWithCannotConnect 一个实例失常,另一个实例会连贯超时"
            loadBalancerClientFactory.getInstance("testServiceWithCannotConnect") >> loadBalancerClientFactoryInstance
            serviceInstanceListSupplier.get() >> Flux.just(Lists.newArrayList(zone1Instance1, zone1Instance2))
        when:
            // 因为咱们针对 testService 返回了两个实例,一个能够失常连贯,一个不能够,然而咱们配置了重试 3 次,所以每次申请应该都能胜利,并且随着程序运行,前面的调用不可用的实例还会被断路
            // 这里次要测试针对 connect time out 还有 断路器关上的状况都会重试,并且无论是 GET 办法还是其余的
            Span span = tracer.nextSpan()
            for (i in 0..<10) {Tracer.SpanInScope cleared = tracer.withSpanInScope(span)
                try {
                    // 测试 get 办法(默认 get 办法会重试)Mono<String> stringMono = webClientNamedContextFactory.getWebClient("testServiceWithCannotConnect")
                                                                          .get().uri("/anything").retrieve()
                                                                          .bodyToMono(String.class)
                    println(stringMono.block())
                    // 测试 post 办法(默认 post 办法针对申请曾经收回的不会重试,这里没有发出请求所以还是会重试的)stringMono = webClientNamedContextFactory.getWebClient("testServiceWithCannotConnect")
                                                             .post().uri("/anything").retrieve()
                                                             .bodyToMono(String.class)
                    println(stringMono.block())
                }
                finally {cleared.close()
                }
            }
        then:"调用至多 20 次负载均衡器且没有异样即胜利"
            (20.._) * loadBalancerClientFactoryInstance.getInstanceResponseByRoundRobin(*_)
    }
}

微信搜寻“我的编程喵”关注公众号,每日一刷,轻松晋升技术,斩获各种 offer

退出移动版