|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hive.service.cli.operation; |
| 20 | + |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import org.apache.hadoop.hive.conf.HiveConf; |
| 23 | +import org.apache.hadoop.hive.ql.IDriver; |
| 24 | +import org.apache.hadoop.hive.ql.session.SessionState; |
| 25 | +import org.apache.hive.service.cli.HandleIdentifier; |
| 26 | +import org.apache.hive.service.cli.OperationState; |
| 27 | +import org.apache.hive.service.cli.SessionHandle; |
| 28 | +import org.apache.hive.service.cli.session.HiveSession; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import org.mockito.InOrder; |
| 32 | + |
| 33 | +import java.lang.reflect.Field; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | +import static org.junit.Assert.assertNull; |
| 37 | +import static org.mockito.Mockito.inOrder; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +public class TestSQLOperationDriverCleanup { |
| 42 | + |
| 43 | + private HiveSession session; |
| 44 | + private IDriver driver; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() { |
| 48 | + HiveConf conf = new HiveConf(); |
| 49 | + session = mock(HiveSession.class); |
| 50 | + when(session.getHiveConf()).thenReturn(conf); |
| 51 | + when(session.getSessionState()).thenReturn(mock(SessionState.class)); |
| 52 | + when(session.getUserName()).thenReturn("user"); |
| 53 | + SessionHandle sessionHandle = mock(SessionHandle.class); |
| 54 | + when(sessionHandle.getHandleIdentifier()).thenReturn(new HandleIdentifier()); |
| 55 | + when(session.getSessionHandle()).thenReturn(sessionHandle); |
| 56 | + driver = mock(IDriver.class); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testSQLOperationCloseReleasesDriverInHPLSQLMode() throws Exception { |
| 61 | + SQLOperation operation = new SQLOperation(session, "insert into test values (1)", |
| 62 | + ImmutableMap.of(), false, 0L, true); |
| 63 | + setDriver(operation, driver); |
| 64 | + |
| 65 | + operation.close(); |
| 66 | + |
| 67 | + InOrder order = inOrder(driver); |
| 68 | + order.verify(driver).close(); |
| 69 | + order.verify(driver).destroy(); |
| 70 | + assertNull(getDriver(operation)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testSQLOperationCloseReleasesDriverInNonHPLSQLMode() throws Exception { |
| 75 | + SQLOperation operation = new SQLOperation(session, "insert into test values (1)", |
| 76 | + ImmutableMap.of(), false, 0L, false); |
| 77 | + setDriver(operation, driver); |
| 78 | + |
| 79 | + operation.close(); |
| 80 | + |
| 81 | + InOrder order = inOrder(driver); |
| 82 | + order.verify(driver).close(); |
| 83 | + order.verify(driver).destroy(); |
| 84 | + assertEquals(OperationState.CLOSED, operation.getStatus().getState()); |
| 85 | + assertNull(getDriver(operation)); |
| 86 | + } |
| 87 | + |
| 88 | + private static void setDriver(SQLOperation operation, IDriver driver) throws Exception { |
| 89 | + Field driverField = SQLOperation.class.getDeclaredField("driver"); |
| 90 | + driverField.setAccessible(true); |
| 91 | + driverField.set(operation, driver); |
| 92 | + } |
| 93 | + |
| 94 | + private static IDriver getDriver(SQLOperation operation) throws Exception { |
| 95 | + Field driverField = SQLOperation.class.getDeclaredField("driver"); |
| 96 | + driverField.setAccessible(true); |
| 97 | + return (IDriver) driverField.get(operation); |
| 98 | + } |
| 99 | +} |
0 commit comments