dbwong commented on code in PR #1430: URL: https://github.com/apache/phoenix/pull/1430#discussion_r910690607
########## phoenix-core/src/test/java/org/apache/phoenix/jdbc/FailoverPhoenixConnectionTest.java: ########## @@ -0,0 +1,217 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.phoenix.jdbc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.phoenix.exception.FailoverSQLException; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.jdbc.HighAvailabilityGroup.HAGroupInfo; + +/** + * Unit test for {@link FailoverPhoenixConnection}. + * + * @see FailoverPhoenixConnectionIT + */ +public class FailoverPhoenixConnectionTest { + private static final Logger LOG = LoggerFactory.getLogger(FailoverPhoenixConnectionTest.class); + + @Mock PhoenixConnection connection1; + @Mock PhoenixConnection connection2; + @Mock HighAvailabilityGroup haGroup; + + final HAGroupInfo haGroupInfo = new HAGroupInfo("fake", "zk1", "zk2"); + FailoverPhoenixConnection failoverConnection; // this connection itself is not mocked or spied. + + @Before + public void init() throws SQLException { + MockitoAnnotations.initMocks(this); + when(haGroup.getGroupInfo()).thenReturn(haGroupInfo); + when(haGroup.connectActive(any(Properties.class))).thenReturn(connection1); + + failoverConnection = new FailoverPhoenixConnection(haGroup, new Properties()); + } + + /** + * Test helper method {@link FailoverPhoenixConnection#wrapActionDuringFailover}. + */ + @Test + public void testWrapActionDuringFailover() throws SQLException { + // Test SupplierWithSQLException which returns a value + String str = "Hello, World!"; + assertEquals(str, failoverConnection.wrapActionDuringFailover(() -> str)); + + // Test RunWithSQLException which does not return value + final AtomicInteger counter = new AtomicInteger(0); + failoverConnection.wrapActionDuringFailover(counter::incrementAndGet); + assertEquals(1, counter.get()); + } + + /** + * Test that after calling failover(), the old connection got closed with FailoverSQLException, + * and a new Phoenix connection is opened. + */ + @Test + public void testFailover() throws SQLException { + // Make HAGroup return a different phoenix connection when it gets called next time + when(haGroup.connectActive(any(Properties.class))).thenReturn(connection2); + + // explicit call failover + failoverConnection.failover(1000L); + + // The old connection should have been closed due to failover + verify(connection1, times(1)).close(any(FailoverSQLException.class)); + // A new Phoenix connection is wrapped underneath + assertEquals(connection2, failoverConnection.getWrappedConnection()); + } + + /** + * Test static {@link FailoverPhoenixConnection#failover(Connection, long)} method. + */ + @Test + public void testFailoverStatic() throws SQLException { + try { + FailoverPhoenixConnection.failover(connection1, 1000L); + fail("Should have failed since plain phoenix connection can not failover!"); + } catch (SQLException e) { + LOG.info("Got expected exception when trying to failover on non-HA connection", e); Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
