@VisibleForTesting
  int removeExpired(final List<Account.UsernameHold> holds) {
    final Instant now = this.clock.instant();
    int holdsToRemove = 0;
    for (Iterator<Account.UsernameHold> it = holds.iterator(); it.hasNext(); ) {
      if (it.next().expirationSecs() < now.getEpochSecond()) {
        holdsToRemove++;
        it.remove();
      }
    }
    return holdsToRemove;
  }
@Test
  public void removeHolds() {
    final List<Account.UsernameHold> holds = IntStream.range(0, 100)
        .mapToObj(i -> new Account.UsernameHold(TestRandomUtil.nextBytes(32), i)).toList();
    final List<Account.UsernameHold> shuffled = new ArrayList<>(holds);
    Collections.shuffle(shuffled);

    final int currentTime = ThreadLocalRandom.current().nextInt(0, 100);
    final Clock clock = TestClock.pinned(Instant.EPOCH.plus(Duration.ofSeconds(currentTime)));
    final RemoveExpiredUsernameHoldsCommand removeExpiredUsernameHoldsCommand =
        new TestRemoveExpiredUsernameHoldsCommand(clock, mock(AccountsManager.class), false);

    final List<Account.UsernameHold> actual = new ArrayList<>(shuffled);
    final int numRemoved = removeExpiredUsernameHoldsCommand.removeExpired(actual);

    assertThat(numRemoved).isEqualTo(currentTime);
    assertThat(actual).hasSize(100 - currentTime);

    // should preserve order
    final Iterator<Account.UsernameHold> expected = shuffled.iterator();
    for (Account.UsernameHold hold : actual) {
      while (!Arrays.equals(expected.next().usernameHash(), hold.usernameHash())) {
        assertThat(expected).as("expected should be in order").hasNext();
      }
    }
  }