|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 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 | + |
| 17 | +package com.google.cloud.sql.core; |
| 18 | + |
| 19 | +import com.google.cloud.sql.ConnectorConfig; |
| 20 | +import java.io.IOException; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Timer; |
| 23 | +import javax.net.ssl.HandshakeCompletedListener; |
| 24 | +import javax.net.ssl.SSLSession; |
| 25 | +import javax.net.ssl.SSLSocket; |
| 26 | +import org.junit.AfterClass; |
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +public class MonitoredCacheTest { |
| 31 | + private static final Timer timer = new Timer(true); |
| 32 | + |
| 33 | + @AfterClass |
| 34 | + public static void afterClass() { |
| 35 | + timer.cancel(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testMonitoredCacheHoldsSocketsWithDomainName() { |
| 40 | + CloudSqlInstanceName name = new CloudSqlInstanceName("proj:reg:inst", "db.example.com"); |
| 41 | + ConnectionConfig config = |
| 42 | + new ConnectionConfig.Builder() |
| 43 | + .withCloudSqlInstance("proj:reg:inst") |
| 44 | + .withDomainName("db.example.com") |
| 45 | + .build(); |
| 46 | + MockCache mockCache = new MockCache(config); |
| 47 | + |
| 48 | + MonitoredCache cache = new MonitoredCache(mockCache, timer, connectionConfig -> name); |
| 49 | + MockSslSocket socket = new MockSslSocket(); |
| 50 | + cache.addSocket(socket); |
| 51 | + Assert.assertEquals("1 socket in cache", 1, cache.getOpenSocketCount()); |
| 52 | + cache.close(); |
| 53 | + Assert.assertTrue("socket closed", socket.closed); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testMonitoredCachePurgesClosedSockets() throws InterruptedException { |
| 58 | + CloudSqlInstanceName name = new CloudSqlInstanceName("proj:reg:inst", "db.example.com"); |
| 59 | + // Purge sockets every 10ms. |
| 60 | + ConnectionConfig config = |
| 61 | + new ConnectionConfig.Builder() |
| 62 | + .withCloudSqlInstance("proj:reg:inst") |
| 63 | + .withDomainName("db.example.com") |
| 64 | + .withConnectorConfig( |
| 65 | + new ConnectorConfig.Builder().withFailoverPeriod(Duration.ofMillis(10)).build()) |
| 66 | + .build(); |
| 67 | + MockCache mockCache = new MockCache(config); |
| 68 | + |
| 69 | + MonitoredCache cache = new MonitoredCache(mockCache, timer, connectionConfig -> name); |
| 70 | + MockSslSocket socket = new MockSslSocket(); |
| 71 | + cache.addSocket(socket); |
| 72 | + Assert.assertEquals("1 socket in cache", 1, cache.getOpenSocketCount()); |
| 73 | + socket.close(); |
| 74 | + Thread.sleep(20); |
| 75 | + Assert.assertEquals("0 socket in cache", 0, cache.getOpenSocketCount()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testMonitoredCacheWithoutDomainNameIgnoresSockets() { |
| 80 | + CloudSqlInstanceName name = new CloudSqlInstanceName("proj:reg:inst"); |
| 81 | + ConnectionConfig config = |
| 82 | + new ConnectionConfig.Builder().withCloudSqlInstance("proj:reg:inst").build(); |
| 83 | + MockCache mockCache = new MockCache(config); |
| 84 | + |
| 85 | + MonitoredCache cache = new MonitoredCache(mockCache, timer, connectionConfig -> name); |
| 86 | + MockSslSocket socket = new MockSslSocket(); |
| 87 | + cache.addSocket(socket); |
| 88 | + Assert.assertEquals("0 socket in cache", 0, cache.getOpenSocketCount()); |
| 89 | + } |
| 90 | + |
| 91 | + private static class MockSslSocket extends SSLSocket { |
| 92 | + boolean closed; |
| 93 | + |
| 94 | + @Override |
| 95 | + public synchronized boolean isClosed() { |
| 96 | + return closed; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public synchronized void close() { |
| 101 | + this.closed = true; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String[] getSupportedCipherSuites() { |
| 106 | + return new String[0]; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String[] getEnabledCipherSuites() { |
| 111 | + return new String[0]; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void setEnabledCipherSuites(String[] suites) {} |
| 116 | + |
| 117 | + @Override |
| 118 | + public String[] getSupportedProtocols() { |
| 119 | + return new String[0]; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String[] getEnabledProtocols() { |
| 124 | + return new String[0]; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void setEnabledProtocols(String[] protocols) {} |
| 129 | + |
| 130 | + @Override |
| 131 | + public SSLSession getSession() { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {} |
| 137 | + |
| 138 | + @Override |
| 139 | + public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {} |
| 140 | + |
| 141 | + @Override |
| 142 | + public void startHandshake() throws IOException {} |
| 143 | + |
| 144 | + @Override |
| 145 | + public void setUseClientMode(boolean mode) {} |
| 146 | + |
| 147 | + @Override |
| 148 | + public boolean getUseClientMode() { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void setNeedClientAuth(boolean need) {} |
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean getNeedClientAuth() { |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void setWantClientAuth(boolean want) {} |
| 162 | + |
| 163 | + @Override |
| 164 | + public boolean getWantClientAuth() { |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void setEnableSessionCreation(boolean flag) {} |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean getEnableSessionCreation() { |
| 173 | + return false; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private static class MockCache implements ConnectionInfoCache { |
| 178 | + private final ConnectionConfig config; |
| 179 | + |
| 180 | + MockCache(ConnectionConfig config) { |
| 181 | + this.config = config; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public ConnectionMetadata getConnectionMetadata(long timeoutMs) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void forceRefresh() {} |
| 191 | + |
| 192 | + @Override |
| 193 | + public void refreshIfExpired() {} |
| 194 | + |
| 195 | + @Override |
| 196 | + public void close() {} |
| 197 | + |
| 198 | + @Override |
| 199 | + public boolean isClosed() { |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public ConnectionConfig getConfig() { |
| 205 | + return config; |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments