### Question:
NumShards { public int getNumShards(long date) { if (!initialized) { configure(); } return milliToNumShardsCache.floorEntry(date).getValue(); } NumShards(Configuration conf); int getNumShards(long date); int getNumShards(String date); String readMultipleNumShardsConfig(); boolean isCacheValid(); int getMaxNumShards(); int getMinNumShards(); int getShardCount(); boolean isInitialized(); void updateCache(); AccumuloHelper getaHelper(); void setaHelper(AccumuloHelper aHelper); static final String PREFETCHED_MULTIPLE_NUMSHARDS_CONFIGURATION; static final String ENABLE_MULTIPLE_NUMSHARDS; static final String MULTIPLE_NUMSHARDS_CACHE_PATH; static final String MULTIPLE_NUMSHARDS_CACHE_FILENAME; static final String MULTIPLE_NUMSHARDS_CACHE_TIMEOUT; static final Text NUM_SHARDS; static final Text NUM_SHARDS_CF; }### Answer:
@Test public void testMultipleNumShardsError() { Configuration conf = new Configuration(); conf.set(ShardIdFactory.NUM_SHARDS, "11"); conf.set(NumShards.ENABLE_MULTIPLE_NUMSHARDS, "true"); NumShards numShards = new NumShards(conf); try { numShards.getNumShards(0); fail("Multiple numshards cache is not found and prefetched multiple shard configuration isn't set. Should result in RuntimeException."); } catch (RuntimeException e) {} }
@Test public void testMultipleNumShardsCacheValidation() throws ParseException, IOException, InterruptedException { File multipleNumShardCache = File.createTempFile("numshards", ".txt"); multipleNumShardCache.deleteOnExit(); try (BufferedWriter writer = Files.newBufferedWriter(multipleNumShardCache.toPath())) { writer.write("20170101_13\n"); writer.write("20171101_17"); writer.flush(); } Thread.sleep(2); Configuration conf = new Configuration(); conf.set(ShardIdFactory.NUM_SHARDS, "11"); conf.set(NumShards.ENABLE_MULTIPLE_NUMSHARDS, "true"); conf.set(NumShards.MULTIPLE_NUMSHARDS_CACHE_PATH, multipleNumShardCache.getParent()); conf.set(NumShards.MULTIPLE_NUMSHARDS_CACHE_FILENAME, multipleNumShardCache.getName()); conf.set(NumShards.MULTIPLE_NUMSHARDS_CACHE_TIMEOUT, "1"); NumShards numShards = new NumShards(conf); try { numShards.getNumShards(0); fail("Multiple numshards cache is not found and prefetched multiple shard configuration isn't set. Should result in RuntimeException."); } catch (RuntimeException e) {} }
@Test public void testMultipleNumShardsCacheParsingError() throws ParseException, IOException { File multipleNumShardCache = File.createTempFile("numshards", ".txt"); multipleNumShardCache.deleteOnExit(); try (BufferedWriter writer = Files.newBufferedWriter(multipleNumShardCache.toPath())) { writer.write("20170101_1320171101_17"); writer.flush(); } Configuration conf = new Configuration(); conf.set(ShardIdFactory.NUM_SHARDS, "11"); conf.set(NumShards.ENABLE_MULTIPLE_NUMSHARDS, "true"); conf.set(NumShards.MULTIPLE_NUMSHARDS_CACHE_PATH, multipleNumShardCache.getParent()); conf.set(NumShards.MULTIPLE_NUMSHARDS_CACHE_FILENAME, multipleNumShardCache.getName()); NumShards numShards = new NumShards(conf); try { numShards.getNumShards(0); fail("Multiple numshards cache file isn't valid. Should result in RuntimeException."); } catch (RuntimeException e) {} }