synchronized ActivateWorkResult activateWorkForKey(ExecutableWork executableWork) {
    ShardedKey shardedKey = executableWork.work().getShardedKey();
    Deque<ExecutableWork> workQueue = activeWork.getOrDefault(shardedKey, new ArrayDeque<>());
    // This key does not have any work queued up on it. Create one, insert Work, and mark the work
    // to be executed.
    if (!activeWork.containsKey(shardedKey) || workQueue.isEmpty()) {
      workQueue.addLast(executableWork);
      activeWork.put(shardedKey, workQueue);
      incrementActiveWorkBudget(executableWork.work());
      return ActivateWorkResult.EXECUTE;
    }

    // Check to see if we have this work token queued.
    Iterator<ExecutableWork> workIterator = workQueue.iterator();
    while (workIterator.hasNext()) {
      ExecutableWork queuedWork = workIterator.next();
      if (queuedWork.id().equals(executableWork.id())) {
        return ActivateWorkResult.DUPLICATE;
      }
      if (queuedWork.id().cacheToken() == executableWork.id().cacheToken()) {
        if (executableWork.id().workToken() > queuedWork.id().workToken()) {
          // Check to see if the queuedWork is active. We only want to remove it if it is NOT
          // currently active.
          if (!queuedWork.equals(workQueue.peek())) {
            workIterator.remove();
            decrementActiveWorkBudget(queuedWork.work());
          }
          // Continue here to possibly remove more non-active stale work that is queued.
        } else {
          return ActivateWorkResult.STALE;
        }
      }
    }

    // Queue the work for later processing.
    workQueue.addLast(executableWork);
    incrementActiveWorkBudget(executableWork.work());
    return ActivateWorkResult.QUEUED;
  }
@Test
  public void
      testActivateWorkForKey_matchingCacheTokens_newWorkTokenGreater_queuedWorkIsActive_QUEUED() {
    long cacheToken = 1L;
    long newWorkToken = 10L;
    long queuedWorkToken = newWorkToken / 2;
    ShardedKey shardedKey = shardedKey("someKey", 1L);

    ExecutableWork queuedWork = createWork(createWorkItem(queuedWorkToken, cacheToken, shardedKey));
    ExecutableWork newWork = createWork(createWorkItem(newWorkToken, cacheToken, shardedKey));

    activeWorkState.activateWorkForKey(queuedWork);
    ActivateWorkResult activateWorkResult = activeWorkState.activateWorkForKey(newWork);

    // newWork should be queued and queuedWork should not be removed since it is currently active.
    assertEquals(ActivateWorkResult.QUEUED, activateWorkResult);
    assertTrue(readOnlyActiveWork.get(shardedKey).contains(newWork));
    assertEquals(queuedWork, readOnlyActiveWork.get(shardedKey).peek());
  }