|
19 | 19 | import io.r2dbc.proxy.core.ConnectionInfo; |
20 | 20 | import io.r2dbc.proxy.core.MethodExecutionInfo; |
21 | 21 | import io.r2dbc.proxy.listener.LastExecutionAwareListener; |
| 22 | +import io.r2dbc.proxy.listener.ProxyExecutionListener; |
22 | 23 | import io.r2dbc.proxy.listener.ProxyMethodExecutionListener; |
23 | 24 | import io.r2dbc.spi.Connection; |
24 | 25 | import io.r2dbc.spi.ConnectionFactory; |
@@ -114,6 +115,63 @@ void createConnection() throws Throwable { |
114 | 115 | assertThat(afterMethod.getResult()).isSameAs(originalConnection); |
115 | 116 | } |
116 | 117 |
|
| 118 | + @Test |
| 119 | + void connectionInfoInBeforeAndAfterMethod() throws Throwable { |
| 120 | + ConnectionFactory connectionFactory = mock(ConnectionFactory.class); |
| 121 | + Connection originalConnection = mock(Connection.class); |
| 122 | + ConnectionIdManager idManager = mock(ConnectionIdManager.class); |
| 123 | + |
| 124 | + // mock call to original ConnectionFactory#create() |
| 125 | + doReturn(Mono.just(originalConnection)).when(connectionFactory).create(); |
| 126 | + |
| 127 | + String connectionId = "100"; |
| 128 | + when(idManager.getId(originalConnection)).thenReturn(connectionId); |
| 129 | + |
| 130 | + AtomicReference<Connection> connectionBeforeMethod = new AtomicReference<>(); |
| 131 | + AtomicReference<Connection> connectionAfterMethod = new AtomicReference<>(); |
| 132 | + AtomicReference<String> connectionIdBeforeMethod = new AtomicReference<>(); |
| 133 | + AtomicReference<String> connectionIdAfterMethod = new AtomicReference<>(); |
| 134 | + ProxyExecutionListener listener = new ProxyExecutionListener() { |
| 135 | + |
| 136 | + @Override |
| 137 | + public void beforeMethod(MethodExecutionInfo executionInfo) { |
| 138 | + ConnectionInfo connectionInfo = executionInfo.getConnectionInfo(); |
| 139 | + assertThat(connectionInfo).isNotNull(); |
| 140 | + connectionBeforeMethod.set(connectionInfo.getOriginalConnection()); |
| 141 | + connectionIdBeforeMethod.set(connectionInfo.getConnectionId()); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void afterMethod(MethodExecutionInfo executionInfo) { |
| 146 | + ConnectionInfo connectionInfo = executionInfo.getConnectionInfo(); |
| 147 | + assertThat(connectionInfo).isNotNull(); |
| 148 | + connectionAfterMethod.set(connectionInfo.getOriginalConnection()); |
| 149 | + connectionIdAfterMethod.set(connectionInfo.getConnectionId()); |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + ProxyConfig proxyConfig = ProxyConfig.builder() |
| 154 | + .connectionIdManager(idManager) |
| 155 | + .listener(listener) |
| 156 | + .build(); |
| 157 | + |
| 158 | + ConnectionFactoryCallbackHandler handler = new ConnectionFactoryCallbackHandler(connectionFactory, proxyConfig); |
| 159 | + |
| 160 | + Object result = handler.invoke(connectionFactory, CREATE_METHOD, null); |
| 161 | + |
| 162 | + assertThat(result).isInstanceOf(Publisher.class); |
| 163 | + |
| 164 | + StepVerifier.create((Publisher<?>) result) |
| 165 | + .expectSubscription() |
| 166 | + .expectNextCount(1) |
| 167 | + .verifyComplete(); |
| 168 | + |
| 169 | + assertThat(connectionBeforeMethod).hasValue(null); |
| 170 | + assertThat(connectionIdBeforeMethod).hasValue(null); |
| 171 | + assertThat(connectionAfterMethod).hasValue(originalConnection); |
| 172 | + assertThat(connectionIdAfterMethod).hasValue("100"); |
| 173 | + } |
| 174 | + |
117 | 175 | @Test |
118 | 176 | void getMetadata() throws Throwable { |
119 | 177 |
|
|
0 commit comments