Skip to content

Commit 14a161a

Browse files
test: stabilize get cases in TestHiveStore
1 parent cc01c18 commit 14a161a

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

gora-hive/src/test/java/org/apache/gora/hive/store/TestHiveStore.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
package org.apache.gora.hive.store;
2020

21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertNull;
2123
import static org.junit.Assert.assertTrue;
24+
import static org.junit.Assert.fail;
2225

2326
import java.nio.ByteBuffer;
2427
import java.nio.charset.Charset;
@@ -34,8 +37,8 @@
3437
import org.apache.gora.store.DataStoreTestUtil;
3538
import org.apache.gora.util.GoraException;
3639
import org.apache.gora.util.StringUtils;
40+
import org.apache.metamodel.query.parser.QueryParserException;
3741
import org.junit.Ignore;
38-
import org.junit.Test;
3942

4043
/**
4144
* HiveStore Tests extending {@link DataStoreTestBase} which run the base JUnit test suite for
@@ -56,6 +59,25 @@ public void assertSchemaExists(String schemaName) throws Exception {
5659
assertTrue(employeeStore.schemaExists());
5760
}
5861

62+
private void awaitEmployeeSchema(String key) throws Exception {
63+
for (int attempt = 0; attempt < 100; attempt++) {
64+
employeeStore.flush();
65+
if (!employeeStore.schemaExists()) {
66+
Thread.sleep(100L);
67+
continue;
68+
}
69+
if (key == null) return;
70+
try {
71+
employeeStore.get(key, new String[] {"ssn"});
72+
return;
73+
} catch (QueryParserException e) {
74+
employeeStore.close();
75+
employeeStore = testDriver.createDataStore(String.class, Employee.class);
76+
}
77+
}
78+
fail("Hive employee schema or record was not visible");
79+
}
80+
5981
@Override
6082
public void assertPut(Employee employee) throws GoraException {
6183
employeeStore.put(employee.getSsn().toString(), employee);
@@ -64,12 +86,14 @@ public void assertPut(Employee employee) throws GoraException {
6486
@Override
6587
public void testGetWithFields() throws Exception {
6688
//Overrides DataStoreTestBase.testGetWithFields to avoid recursive field "boss"
89+
awaitEmployeeSchema(null);
6790
Employee employee = DataStoreTestUtil.createEmployee();
6891
WebPage webpage = DataStoreTestUtil.createWebPage();
6992
employee.setWebpage(webpage);
7093
String ssn = employee.getSsn().toString();
7194
employeeStore.put(ssn, employee);
7295
employeeStore.flush();
96+
awaitEmployeeSchema(ssn);
7397

7498
String[] fields = ((HiveStore<String, Employee>) employeeStore).getFields();
7599
for (Set<String> subset : StringUtils.powerset(fields)) {
@@ -92,17 +116,20 @@ public void testGet() throws Exception {
92116
//Overrides DataStoreTestBase.testGet to avoid recursive field "boss"
93117
log.info("test method: testGet");
94118
employeeStore.createSchema();
119+
awaitEmployeeSchema(null);
95120
Employee employee = DataStoreTestUtil.createEmployee();
96121
String ssn = employee.getSsn().toString();
97122
employeeStore.put(ssn, employee);
98123
employeeStore.flush();
124+
awaitEmployeeSchema(ssn);
99125
Employee after = employeeStore.get(ssn, null);
100126
DataStoreTestUtil.assertEqualEmployeeObjects(employee, after);
101127
}
102128

103129
@Override
104130
public void testGetNested() throws Exception {
105131
//Overrides DataStoreTestBase.testGetNested to avoid recursive field "boss"
132+
awaitEmployeeSchema(null);
106133
Employee employee = DataStoreTestUtil.createEmployee();
107134

108135
WebPage webpage = new BeanFactoryImpl<>(String.class, WebPage.class).newPersistent();
@@ -117,11 +144,37 @@ public void testGetNested() throws Exception {
117144

118145
employeeStore.put(ssn, employee);
119146
employeeStore.flush();
147+
awaitEmployeeSchema(ssn);
120148
Employee after = employeeStore.get(ssn, null);
121149
DataStoreTestUtil.assertEqualEmployeeObjects(employee, after);
122150
DataStoreTestUtil.assertEqualWebPageObjects(webpage, after.getWebpage());
123151
}
124152

153+
@Override
154+
public void testObjectFieldValue() throws Exception {
155+
employeeStore.createSchema();
156+
awaitEmployeeSchema(null);
157+
Employee employee = DataStoreTestUtil.createEmployee();
158+
String uuid = "employee-1234567890";
159+
employeeStore.put(uuid, employee);
160+
employeeStore.flush();
161+
awaitEmployeeSchema(uuid);
162+
Employee returnedEmployee = employeeStore.get(uuid, null);
163+
assertEquals(returnedEmployee.getValue(), new Utf8("random value"));
164+
}
165+
166+
@Override
167+
public void testGetNonExisting() throws Exception {
168+
employeeStore.createSchema();
169+
Employee dummy = DataStoreTestUtil.createEmployee();
170+
String uuid = "employee-1234567890";
171+
employeeStore.put(uuid, dummy);
172+
employeeStore.flush();
173+
awaitEmployeeSchema(uuid);
174+
Employee employee = employeeStore.get("_NON_EXISTING_SSN_FOR_EMPLOYEE_");
175+
assertNull(employee);
176+
}
177+
125178
@Ignore("Hive test server doesn't support deleting and updating entries")
126179
@Override
127180
public void testExists() throws Exception {

0 commit comments

Comments
 (0)