### Question:
ParseKeyValueCache { static void saveToKeyValueCache(String key, String value) { synchronized (MUTEX_IO) { File prior = getKeyValueCacheFile(key); if (prior != null) { prior.delete(); } File f = createKeyValueCacheFile(key); try { ParseFileUtils.writeByteArrayToFile(f, value.getBytes("UTF-8")); } catch (IOException e) { } File[] files = getKeyValueCacheDir().listFiles(); if (files == null || files.length == 0) { return; } int numFiles = files.length; int numBytes = 0; for (File file : files) { numBytes += file.length(); } if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { return; } Arrays.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { int dateCompare = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); if (dateCompare != 0) { return dateCompare; } else { return f1.getName().compareTo(f2.getName()); } } }); for (File file : files) { numFiles--; numBytes -= file.length(); file.delete(); if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { break; } } } }    }### Answer:
@Test public void testMultipleAsynchronousWrites() throws ParseException { int max = 100; ParseKeyValueCache.maxKeyValueCacheFiles = max; for (int i = 0; i < max; i++) { ParseKeyValueCache.saveToKeyValueCache("key " + i, "test"); } List<Task<Void>> tasks = new ArrayList<>(); for (int i = 0; i < 1000; i++) { tasks.add(Task.call(new Callable<Void>() { @Override public Void call() throws Exception { ParseKeyValueCache.saveToKeyValueCache("foo", "test"); return null; } }, Task.BACKGROUND_EXECUTOR)); } ParseTaskUtils.wait(Task.whenAll(tasks)); }
@Test public void testMultipleAsynchronousWrites() throws ParseException { int max = 100; ParseKeyValueCache.maxKeyValueCacheFiles = max; for (int i = 0; i < max; i++) { ParseKeyValueCache.saveToKeyValueCache("key " + i, "test"); } List<Task<Void>> tasks = new ArrayList<>(); for (int i = 0; i < 1000; i++) { tasks.add(Task.call(new Callable<Void>() { @Override public Void call() { ParseKeyValueCache.saveToKeyValueCache("foo", "test"); return null; } }, Task.BACKGROUND_EXECUTOR)); } ParseTaskUtils.wait(Task.whenAll(tasks)); }
@Test public void testSaveToKeyValueCacheWithoutCacheDir() throws Exception { assertTrue(keyValueCacheDir.exists()); keyValueCacheDir.delete(); assertFalse(keyValueCacheDir.exists()); ParseKeyValueCache.saveToKeyValueCache("key", "value"); assertEquals(1, keyValueCacheDir.listFiles().length); assertArrayEquals( "value".getBytes(), ParseFileUtils.readFileToByteArray(keyValueCacheDir.listFiles()[0])); }