long waitTime = 5000;
ManualTimeKeeper timeKeeper = new ManualTimeKeeper();
timeKeeper.setCurrentTimeMillis(startTime);
ConnectionQueryServices services = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES);
StatsManager stats = new StatsManagerImpl(services, updateFreq, maxAge, timeKeeper);
MinKeyChange minKeyChange = new MinKeyChange(stats, table);
MaxKeyChange maxKeyChange = new MaxKeyChange(stats, table);
byte[] minKey = stats.getMinKey(table);
assertTrue(minKey == null);
assertTrue(waitForAsyncChange(minKeyChange,waitTime));
assertArrayEquals(KMIN, stats.getMinKey(table));
assertArrayEquals(KMAX, stats.getMaxKey(table));
minKeyChange = new MinKeyChange(stats, table);
url = PHOENIX_JDBC_URL + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts+2;
props = new Properties(TEST_PROPERTIES);
conn = DriverManager.getConnection(url, props);
PreparedStatement delStmt = conn.prepareStatement("delete from " + STABLE_NAME + " where id=?");
delStmt.setString(1, new String(KMIN));
delStmt.execute();
PreparedStatement upsertStmt = conn.prepareStatement("upsert into " + STABLE_NAME + " VALUES (?, ?)");
upsertStmt.setString(1, new String(KMIN2));
upsertStmt.setInt(2, 1);
upsertStmt.execute();
conn.commit();
assertFalse(waitForAsyncChange(minKeyChange,waitTime)); // Stats won't change until they're attempted to be retrieved again
timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + updateFreq);
minKeyChange = new MinKeyChange(stats, table); // Will kick off change, but will upate asynchronously
assertArrayEquals(KMIN, minKeyChange.value);
assertTrue(waitForAsyncChange(minKeyChange,waitTime));
assertArrayEquals(KMIN2, stats.getMinKey(table));
assertArrayEquals(KMAX, stats.getMaxKey(table));
minKeyChange = new MinKeyChange(stats, table);
timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + maxAge);
minKeyChange = new MinKeyChange(stats, table); // Will kick off change, but will upate asynchronously
assertTrue(null == minKeyChange.value);
assertTrue(waitForAsyncChange(minKeyChange,waitTime));
assertArrayEquals(KMIN2, stats.getMinKey(table));
assertArrayEquals(KMAX, stats.getMaxKey(table));
maxKeyChange = new MaxKeyChange(stats, table);
delStmt.setString(1, new String(KMAX));
delStmt.execute();
upsertStmt.setString(1, new String(KMAX2));
upsertStmt.setInt(2, 1);
upsertStmt.execute();
conn.commit();
conn.close();
assertFalse(waitForAsyncChange(maxKeyChange,waitTime)); // Stats won't change until they're attempted to be retrieved again
timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + updateFreq);
maxKeyChange = new MaxKeyChange(stats, table); // Will kick off change, but will upate asynchronously
assertArrayEquals(KMAX, maxKeyChange.value);
assertTrue(waitForAsyncChange(maxKeyChange,waitTime));
assertArrayEquals(KMAX2, stats.getMaxKey(table));
assertArrayEquals(KMIN2, stats.getMinKey(table));
maxKeyChange = new MaxKeyChange(stats, table);
timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + maxAge);
maxKeyChange = new MaxKeyChange(stats, table); // Will kick off change, but will upate asynchronously
assertTrue(null == maxKeyChange.value);
assertTrue(waitForAsyncChange(maxKeyChange,waitTime));
assertArrayEquals(KMIN2, stats.getMinKey(table));
assertArrayEquals(KMAX2, stats.getMaxKey(table));
}