cmunkey commented on code in PR #3444: URL: https://github.com/apache/hive/pull/3444#discussion_r932577367
########## ql/src/test/org/apache/hadoop/hive/ql/parse/repl/dump/TestDumpExporter.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.hadoop.hive.ql.parse.repl.dump; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.junit.Assert; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.junit.Test; + +import java.util.concurrent.Semaphore; + +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({LoggerFactory.class, DumpExporter.class}) + +public class TestDumpExporter { + + protected static final Logger LOG = LoggerFactory.getLogger(TestDumpExporter.class); + + @Mock + HiveConf conf; + + private static int taskNumber = 0; + + private final int totalTask = 10; + + private Semaphore sem; + + public void runParallelTask() { + Assert.assertTrue(sem.tryAcquire()); + ++taskNumber; + LOG.debug("Current task number is: {} and thread is: {} ", taskNumber, Thread.currentThread().getName()); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + public void runSequentialTask() { + ++taskNumber; + LOG.debug("Current task number is: {} and thread is: {} ", taskNumber, Thread.currentThread().getName()); + } + + protected void finalize() + { + for (int i = 0; i < totalTask; i++) { + sem.release(); + } + } + @Test + public void testDumpExporterWithParallelism() throws Exception { + when(conf.getIntVar(HiveConf.ConfVars.REPL_EXPORT_DUMP_PARALLELISM)).thenReturn(10); + sem = new Semaphore(totalTask); + taskNumber = 0; + Runnable r = () -> { + runParallelTask(); + }; + DumpExporter dumpExporter = new DumpExporter(conf); + try { + for (int i = 0; i < totalTask; i++) { + dumpExporter.submit(r); + } + final long actualNumTasksExecuted = dumpExporter.getTotalTaskEverExecuted(); + Assert.assertEquals(totalTask, actualNumTasksExecuted); + dumpExporter.finish(); + } catch (Exception e) { + LOG.info("DumpExporter task failed with exception {} ", e.getMessage()); + } finally { + finalize(); + } + } + + @Test + public void testDumpExporterWithNoParallelism() throws Exception { + taskNumber = 0; + Runnable r = () -> { + runSequentialTask(); + }; + DumpExporter dumpExporter = new DumpExporter(conf); + try { + for (int i = 0; i < totalTask; i++) { + dumpExporter.submit(r); + } + final long actualNumTasksExecuted = dumpExporter.getTotalTaskEverExecuted(); + Assert.assertEquals(0, actualNumTasksExecuted); + dumpExporter.finish(); + } catch (Exception e) { Review Comment: Test will pass if there is an exception, why are you catching here. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
