Skip to content

Commit

Permalink
apache#1238 Fill BackendConnectionTest.assertGetConnectionSizeLessTha…
Browse files Browse the repository at this point in the history
…nCache.
  • Loading branch information
cherrylzhao authored and maxiaoguang64 committed Nov 25, 2018
1 parent 1e0be82 commit 7018c26
Showing 1 changed file with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package io.shardingsphere.shardingproxy.backend.jdbc.connection;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import io.shardingsphere.core.constant.ConnectionMode;
import io.shardingsphere.shardingproxy.backend.jdbc.datasource.JDBCBackendDataSource;
import io.shardingsphere.shardingproxy.runtime.schema.LogicSchema;
Expand All @@ -27,7 +29,9 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -59,17 +63,19 @@ public void setup() {
}

@Test
@SneakyThrows
public void assertGetConnectionCacheIsEmpty() {
public void assertGetConnectionCacheIsEmpty() throws SQLException {
when(backendDataSource.getConnections((ConnectionMode) any(), anyString(), eq(2))).thenReturn(mockNewConnections(2));
List<Connection> actualConnections = backendConnection.getConnections(ConnectionMode.MEMORY_STRICTLY, "ds1", 2);
assertThat(actualConnections.size(), is(2));
assertThat(backendConnection.getConnectionSize(), is(2));
}

@Test
public void assertGetConnectionSizeLessThanCache() {

public void assertGetConnectionSizeLessThanCache() throws SQLException {
setCachedConnections("ds1", 10);
List<Connection> actualConnections = backendConnection.getConnections(ConnectionMode.MEMORY_STRICTLY, "ds1", 2);
assertThat(actualConnections.size(), is(2));
assertThat(backendConnection.getConnectionSize(), is(10));
}

@Test
Expand All @@ -95,4 +101,13 @@ private List<Connection> mockNewConnections(final int connectionSize) {
}
return result;
}

@SneakyThrows
private void setCachedConnections(final String dsName, final int connectionSize) {
Multimap<String, Connection> cachedConnections = HashMultimap.create();
cachedConnections.putAll(dsName, mockNewConnections(connectionSize));
Field field = backendConnection.getClass().getDeclaredField("cachedConnections");
field.setAccessible(true);
field.set(backendConnection, cachedConnections);
}
}

0 comments on commit 7018c26

Please sign in to comment.