|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 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 | + * http://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 | +package org.springframework.http.client.reactive; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | +import reactor.netty.http.HttpResources; |
| 22 | +import reactor.netty.resources.ConnectionProvider; |
| 23 | +import reactor.netty.resources.LoopResources; |
| 24 | + |
| 25 | +import static org.junit.Assert.*; |
| 26 | +import static org.mockito.Mockito.*; |
| 27 | + |
| 28 | +/** |
| 29 | + * Unit tests for {@link ReactorResourceFactory}. |
| 30 | + * @author Rossen Stoyanchev |
| 31 | + */ |
| 32 | +public class ReactorResourceFactoryTests { |
| 33 | + |
| 34 | + private final ReactorResourceFactory resourceFactory = new ReactorResourceFactory(); |
| 35 | + |
| 36 | + private final ConnectionProvider connectionProvider = mock(ConnectionProvider.class); |
| 37 | + |
| 38 | + private final LoopResources loopResources = mock(LoopResources.class); |
| 39 | + |
| 40 | + |
| 41 | + @Test |
| 42 | + public void globalResources() throws Exception { |
| 43 | + |
| 44 | + this.resourceFactory.setUseGlobalResources(true); |
| 45 | + this.resourceFactory.afterPropertiesSet(); |
| 46 | + |
| 47 | + HttpResources globalResources = HttpResources.get(); |
| 48 | + assertSame(globalResources, this.resourceFactory.getConnectionProvider()); |
| 49 | + assertSame(globalResources, this.resourceFactory.getLoopResources()); |
| 50 | + assertFalse(globalResources.isDisposed()); |
| 51 | + |
| 52 | + this.resourceFactory.destroy(); |
| 53 | + |
| 54 | + assertTrue(globalResources.isDisposed()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void globalResourcesWithConsumer() throws Exception { |
| 59 | + |
| 60 | + AtomicBoolean invoked = new AtomicBoolean(false); |
| 61 | + |
| 62 | + this.resourceFactory.addGlobalResourcesConsumer(httpResources -> invoked.set(true)); |
| 63 | + this.resourceFactory.afterPropertiesSet(); |
| 64 | + |
| 65 | + assertTrue(invoked.get()); |
| 66 | + this.resourceFactory.destroy(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void localResources() throws Exception { |
| 71 | + |
| 72 | + this.resourceFactory.setUseGlobalResources(false); |
| 73 | + this.resourceFactory.afterPropertiesSet(); |
| 74 | + |
| 75 | + ConnectionProvider connectionProvider = this.resourceFactory.getConnectionProvider(); |
| 76 | + LoopResources loopResources = this.resourceFactory.getLoopResources(); |
| 77 | + |
| 78 | + assertNotSame(HttpResources.get(), connectionProvider); |
| 79 | + assertNotSame(HttpResources.get(), loopResources); |
| 80 | + |
| 81 | + // The below does not work since ConnectionPoolProvider simply checks if pool is empty. |
| 82 | + // assertFalse(connectionProvider.isDisposed()); |
| 83 | + assertFalse(loopResources.isDisposed()); |
| 84 | + |
| 85 | + this.resourceFactory.destroy(); |
| 86 | + |
| 87 | + assertTrue(connectionProvider.isDisposed()); |
| 88 | + assertTrue(loopResources.isDisposed()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void localResourcesViaSupplier() throws Exception { |
| 93 | + |
| 94 | + this.resourceFactory.setUseGlobalResources(false); |
| 95 | + this.resourceFactory.setConnectionProviderSupplier(() -> this.connectionProvider); |
| 96 | + this.resourceFactory.setLoopResourcesSupplier(() -> this.loopResources); |
| 97 | + this.resourceFactory.afterPropertiesSet(); |
| 98 | + |
| 99 | + ConnectionProvider connectionProvider = this.resourceFactory.getConnectionProvider(); |
| 100 | + LoopResources loopResources = this.resourceFactory.getLoopResources(); |
| 101 | + |
| 102 | + assertSame(this.connectionProvider, connectionProvider); |
| 103 | + assertSame(this.loopResources, loopResources); |
| 104 | + |
| 105 | + verifyNoMoreInteractions(this.connectionProvider, this.loopResources); |
| 106 | + |
| 107 | + this.resourceFactory.destroy(); |
| 108 | + |
| 109 | + // Managed (destroy disposes).. |
| 110 | + verify(this.connectionProvider).dispose(); |
| 111 | + verify(this.loopResources).dispose(); |
| 112 | + verifyNoMoreInteractions(this.connectionProvider, this.loopResources); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void externalResources() throws Exception { |
| 117 | + |
| 118 | + this.resourceFactory.setUseGlobalResources(false); |
| 119 | + this.resourceFactory.setConnectionProvider(this.connectionProvider); |
| 120 | + this.resourceFactory.setLoopResources(this.loopResources); |
| 121 | + this.resourceFactory.afterPropertiesSet(); |
| 122 | + |
| 123 | + ConnectionProvider connectionProvider = this.resourceFactory.getConnectionProvider(); |
| 124 | + LoopResources loopResources = this.resourceFactory.getLoopResources(); |
| 125 | + |
| 126 | + assertSame(this.connectionProvider, connectionProvider); |
| 127 | + assertSame(this.loopResources, loopResources); |
| 128 | + |
| 129 | + verifyNoMoreInteractions(this.connectionProvider, this.loopResources); |
| 130 | + |
| 131 | + this.resourceFactory.destroy(); |
| 132 | + |
| 133 | + // Not managed (destroy has no impact).. |
| 134 | + verifyNoMoreInteractions(this.connectionProvider, this.loopResources); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments