Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring HaeinsaTestBase to use TestingContext #20

Merged
merged 2 commits into from
May 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions src/test/java/kr/co/vcnc/haeinsa/HBaseMigrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class HBaseMigrationTest extends HaeinsaTestBase {
*/
@Test
public void testMigrationByGet() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down Expand Up @@ -73,9 +73,9 @@ public void testMigrationByGet() throws Exception {
*/
@Test
public void testMigrationByPut() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down Expand Up @@ -117,9 +117,9 @@ public void testMigrationByPut() throws Exception {
*/
@Test
public void testMigrationByDelete() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down Expand Up @@ -162,9 +162,9 @@ public void testMigrationByDelete() throws Exception {
*/
@Test
public void testMigrationByInterRowScan() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down Expand Up @@ -237,9 +237,9 @@ public void testMigrationByInterRowScan() throws Exception {
*/
@Test
public void testMigrationByIntraRowScan() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down Expand Up @@ -305,9 +305,9 @@ public void testMigrationByIntraRowScan() throws Exception {
*/
@Test
public void testMigrationByMixedPutAndGet() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down Expand Up @@ -369,9 +369,9 @@ public void testMigrationByMixedPutAndGet() throws Exception {
*/
@Test
public void testMigrationByMixedPutAndDelete() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down Expand Up @@ -436,9 +436,9 @@ public void testMigrationByMixedPutAndDelete() throws Exception {
*/
@Test
public void testMigrationByMixedInterRowScanAndPut() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down Expand Up @@ -521,9 +521,9 @@ public void testMigrationByMixedInterRowScanAndPut() throws Exception {
*/
@Test
public void testMigrationByMixedIntraRowScanAndPut() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

// Put sample data to HBase cluster by primitive HBase operation.
// Lock will not created because it is just a primitive HBase operation.
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/kr/co/vcnc/haeinsa/Haeinsa67BugTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
import org.testng.annotations.Test;

/**
* https://issues.vcnc.co.kr/browse/HAEINSA-67 When haeinsa recovers rows, we
* https://issues.vcnc.co.kr/browse/HAEINSA-67 When Haeinsa recovers rows, we
* should make stable rows into new committimestamp's stable rows. This method
* prevents making dangling row locks of long running transactions.
*/
public class Haeinsa67BugTest extends HaeinsaTestBase {

@Test
public void testRecover() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable(currentTableName());
final HTableInterface hTestTable = CLUSTER.getHbaseTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");
final HTableInterface hTestTable = context().getHTableInterface("test");

{
TRowKey primaryRowKey = new TRowKey().setTableName(testTable.getTableName()).setRow(Bytes.toBytes("Andrew"));
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/kr/co/vcnc/haeinsa/HaeinsaComplexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class HaeinsaComplexTest extends HaeinsaTestBase {
*/
@Test
public void testSimepleIncrement() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable("HaeinsaComplexTest.testSimepleIncrement.test");
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");

HaeinsaTransaction tx;

Expand Down Expand Up @@ -94,8 +94,8 @@ public void testSimepleIncrement() throws Exception {
*/
@Test
public void testConcurrentRandomIncrement() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable("HaeinsaComplexTest.testConcurrentRandomIncrement.test");
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");

HaeinsaTransaction tx;

Expand Down Expand Up @@ -191,8 +191,8 @@ public void run() {
*/
@Test
public void testSerializability() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface testTable = CLUSTER.getHaeinsaTable("HaeinsaComplexTest.testSerializability.test");
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test");

HaeinsaTransaction tx;

Expand Down
114 changes: 99 additions & 15 deletions src/test/java/kr/co/vcnc/haeinsa/HaeinsaTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@
import java.lang.reflect.Method;

import org.apache.hadoop.hbase.client.HTableInterface;
import org.hsqldb.TransactionManager;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;

/**
* Basic test class for Haeinsa unit tests.
* This class automaticall create CLUSTER, and table name of each test method.
* You can use CLUSTER to get {@link HaeinsaTableIface} and {@link HTableInterface}.
* You can also use {@link #currentTestName() currentTableName} to get current table name for test
* This class automatically create CLUSTER, and table name of each test method.
* You can get context of the test with {@link #context()}.
*/
public class HaeinsaTestBase {
protected static HaeinsaTestingCluster CLUSTER;

protected final ThreadLocal<String> currentTableName = new ThreadLocal<>();
private static HaeinsaTestingCluster CLUSTER;
private static final ThreadLocal<TestingContext> CONTEXT = new ThreadLocal<>();

@BeforeClass
public static void setUpHbase() throws Exception {
Expand All @@ -42,22 +41,107 @@ public static void setUpHbase() throws Exception {
public void generateTableName(Method method) {
final String className = method.getDeclaringClass().getName();
final String methodName = method.getName();
currentTableName.set(String.format("%s.%s", className, methodName));
CONTEXT.set(new TestingContext(className, methodName));
}

@AfterMethod
public void releaseTableName() {
currentTableName.remove();
CONTEXT.remove();
}

public static TestingContext context() {
return CONTEXT.get();
}

/**
* Get table name for current test method.
* Table name is automatically generate before execution of the test method.
* This method construct with name of the test class and name of the test method.
* So, this table name is unique for specific test.
* @return table name for current test method
* Context of current test. You can create {@link HTableInterface} and {@link HaeinsaTableIface}
* with this class. You can also get instance of {@link TransactionManager} with
* {@link #getTransactionManager()}.
*/
public String currentTableName() {
return currentTableName.get();
public static final class TestingContext {
private final String className;
private final String methodName;

private TestingContext(String className, String methodName) {
this.className = className;
this.methodName = methodName;
}

/**
* Class name of the current test.
*
* @return class name of the current test
*/
public String getClassName() {
return className;
}

/**
* Method name of the current test.
*
* @return method name of the current test
*/
public String getMethodName() {
return methodName;
}

/**
* Get {@link HaeinsaTestingCluster} instance for test.
* Since creating new testing cluster takes long time, each test case reuse the cluster.
* For this reason, the testing cluster is managed as singleton,
* so this method always returns same instance of {@link HaeinsaTestingCluster}.
*
* @return instance of {@link HaeinsaTestingCluster}
*/
public HaeinsaTestingCluster getCluster() {
return CLUSTER;
}

/**
* Get {@link HaeinsaTransactionManager} instance for test.
*
* @return instance of {@link HaeinsaTransactionManager}
*/
public HaeinsaTransactionManager getTransactionManager() {
return CLUSTER.getTransactionManager();
}

/**
* Create {@link HaeinsaTableIface} with table name.
* The name of the table will be created with {@link #createContextedTableName(String)}.
*
* @param tableName table name of the HaeinsaTable
* @return instance of {@link HaeinsaTableIface}
* @throws Exception if there is a problem instantiating the HaeinsaTable
*/
public HaeinsaTableIface getHaeinsaTableIface(String tableName) throws Exception {
return getCluster().getHaeinsaTable(createContextedTableName(tableName));
}

/**
* Create {@link HTableInterface} with table name.
* The name of the table will be created with {@link #createContextedTableName(String)}.
*
* @param tableName table name of the HTable
* @return instance of {@link HTableInterface}
* @throws Exception if there is a problem instantiating the HTable
*/
public HTableInterface getHTableInterface(String tableName) throws Exception {
return getCluster().getHbaseTable(createContextedTableName(tableName));
}

/**
* Create table name for current test method.
* This method create table name with class name and method name of the test.
* Since, tests in Haeinsa reuse {@link HaeinsaTestingCluster}, data in the cluster would
* affect to each test cases. To avoid this, each test should use it's own table.
* This is why this method is needed.
*
* @param tableName table name
* @return prefixed table name with class and method name of the test
*/
public String createContextedTableName(String tableName) {
return String.format("%s.%s.%s", className, methodName, tableName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class HaeinsaTransactionLocalsTest extends HaeinsaTestBase {

@Test
public void testBasicOperations() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTransactionManager tm = context().getTransactionManager();

HaeinsaTransaction tx1 = tm.begin();
Assert.assertFalse(LOCAL.isSet(tx1));
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/kr/co/vcnc/haeinsa/HaeinsaTransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class HaeinsaTransactionTest extends HaeinsaTestBase {

@Test
public void testHasChanges() throws Exception {
final HaeinsaTransactionManager tm = CLUSTER.getTransactionManager();
final HaeinsaTableIface table = CLUSTER.getHaeinsaTable(currentTableName());
final HaeinsaTransactionManager tm = context().getTransactionManager();
final HaeinsaTableIface table = context().getHaeinsaTableIface("test");

// Tests read-only transaction
{
Expand Down
Loading