JobExecutor { @VisibleForTesting @SuppressLint("UseSparseArrays") void cleanUpRoutine(LruCache<Integer, WeakReference<Job>> cache) { Map<Integer, WeakReference<Job>> snapshot = new HashMap<>(cache.snapshot()); for (Integer key : snapshot.keySet()) { if (snapshot.get(key) == null || snapshot.get(key).get() == null) { cache.remove(key); } } } JobExecutor(); synchronized Future<Job.Result> execute(@NonNull Context context, @NonNull JobRequest request, @Nullable Job job, @NonNull Bundle transientExtras); synchronized Job getJob(int jobId); synchronized Set<Job> getAllJobs(); synchronized Set<Job> getAllJobsForTag(String tag); SparseArray<Job.Result> getAllJobResults(); synchronized void markJobRequestStarting(@NonNull JobRequest request); synchronized boolean isRequestStarting(JobRequest request);  }
@Test public void verifyCleanUpRoutine() { LruCache<Integer, WeakReference<Job>> cache = new LruCache<>(20); cache.put(1, new WeakReference<>(createJobMock(1))); cache.put(2, new WeakReference<Job>(null)); new JobExecutor().cleanUpRoutine(cache); assertThat(cache.size()).isEqualTo(1); }