|
| 1 | +/* |
| 2 | + * Copyright 2013-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.openfeign.support; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | +import java.util.concurrent.atomic.AtomicReference; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import org.springframework.beans.factory.ObjectProvider; |
| 30 | +import org.springframework.boot.http.converter.autoconfigure.ClientHttpMessageConvertersCustomizer; |
| 31 | +import org.springframework.http.converter.HttpMessageConverter; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | +import static org.mockito.Mockito.when; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests for {@link FeignHttpMessageConverters}. |
| 39 | + * |
| 40 | + * @author seonwoo_jung |
| 41 | + */ |
| 42 | +class FeignHttpMessageConvertersTests { |
| 43 | + |
| 44 | + @Test |
| 45 | + void shouldNotExposePartiallyInitializedConvertersToConcurrentCallers() throws Exception { |
| 46 | + CountDownLatch initStarted = new CountDownLatch(1); |
| 47 | + CountDownLatch allowInitToFinish = new CountDownLatch(1); |
| 48 | + |
| 49 | + // A customizer that blocks while the converter list is being built so that we can |
| 50 | + // observe what a second, concurrent caller sees mid-initialization. |
| 51 | + ClientHttpMessageConvertersCustomizer blockingCustomizer = builder -> { |
| 52 | + initStarted.countDown(); |
| 53 | + try { |
| 54 | + allowInitToFinish.await(); |
| 55 | + } |
| 56 | + catch (InterruptedException ex) { |
| 57 | + Thread.currentThread().interrupt(); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + @SuppressWarnings("unchecked") |
| 62 | + ObjectProvider<ClientHttpMessageConvertersCustomizer> customizers = mock(ObjectProvider.class); |
| 63 | + when(customizers.orderedStream()).thenReturn(Stream.of(blockingCustomizer)); |
| 64 | + @SuppressWarnings("unchecked") |
| 65 | + ObjectProvider<HttpMessageConverterCustomizer> cloudCustomizers = mock(ObjectProvider.class); |
| 66 | + when(cloudCustomizers.iterator()).thenReturn(Collections.emptyIterator()); |
| 67 | + |
| 68 | + FeignHttpMessageConverters feignConverters = new FeignHttpMessageConverters(customizers, cloudCustomizers); |
| 69 | + |
| 70 | + AtomicReference<List<HttpMessageConverter<?>>> initializerResult = new AtomicReference<>(); |
| 71 | + AtomicReference<List<HttpMessageConverter<?>>> readerResult = new AtomicReference<>(); |
| 72 | + AtomicBoolean readerObservedEmptyList = new AtomicBoolean(); |
| 73 | + |
| 74 | + Thread initializer = new Thread(() -> initializerResult.set(feignConverters.getConverters()), |
| 75 | + "converters-init"); |
| 76 | + Thread reader = new Thread(() -> { |
| 77 | + List<HttpMessageConverter<?>> converters = feignConverters.getConverters(); |
| 78 | + readerObservedEmptyList.set(converters.isEmpty()); |
| 79 | + readerResult.set(converters); |
| 80 | + }, "converters-reader"); |
| 81 | + |
| 82 | + initializer.start(); |
| 83 | + assertThat(initStarted.await(5, TimeUnit.SECONDS)).isTrue(); |
| 84 | + |
| 85 | + // The first caller is now stuck building the list. Start a concurrent caller and |
| 86 | + // wait until it has either blocked waiting for initialization to complete (fixed |
| 87 | + // behaviour) or already returned a value (buggy behaviour) before releasing. |
| 88 | + reader.start(); |
| 89 | + waitUntilBlockedOrFinished(reader); |
| 90 | + allowInitToFinish.countDown(); |
| 91 | + |
| 92 | + initializer.join(TimeUnit.SECONDS.toMillis(5)); |
| 93 | + reader.join(TimeUnit.SECONDS.toMillis(5)); |
| 94 | + |
| 95 | + assertThat(initializerResult.get()).isNotEmpty(); |
| 96 | + assertThat(readerObservedEmptyList).as("concurrent caller observed an empty converter list").isFalse(); |
| 97 | + assertThat(readerResult.get()).isSameAs(initializerResult.get()); |
| 98 | + } |
| 99 | + |
| 100 | + private static void waitUntilBlockedOrFinished(Thread thread) { |
| 101 | + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); |
| 102 | + while (System.nanoTime() < deadline) { |
| 103 | + Thread.State state = thread.getState(); |
| 104 | + if (state == Thread.State.BLOCKED || state == Thread.State.WAITING || state == Thread.State.TIMED_WAITING |
| 105 | + || state == Thread.State.TERMINATED) { |
| 106 | + return; |
| 107 | + } |
| 108 | + Thread.yield(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments