@Test
  public void testAccountUpdateConsumer() throws Exception {

    List<Collection<Account>> updatedAccountsReceivedByConsumers = new ArrayList<>();
    Consumer<Collection<Account>> accountUpdateConsumer = updatedAccountsReceivedByConsumers::add;

    // add consumers
    mySqlAccountService.addAccountUpdateConsumer(accountUpdateConsumer);

    // accounts and containers to be updated
    List<Container> containersList = new ArrayList<>();
    containersList.add(
        new ContainerBuilder((short) 1, "c1", Container.ContainerStatus.ACTIVE, "c1", (short) 1).build());
    containersList.add(
        new ContainerBuilder((short) 2, "c2", Container.ContainerStatus.ACTIVE, "c2", (short) 1).build());
    List<Account> accountsList = new ArrayList<>();
    accountsList.add(new AccountBuilder((short) 1, "a1", Account.AccountStatus.ACTIVE).build());
    accountsList.add(new AccountBuilder((short) 2, "a2", Account.AccountStatus.ACTIVE).build());

    when(mockMySqlAccountStore.getNewAccounts(anyLong())).thenReturn(accountsList);
    when(mockMySqlAccountStore.getNewContainers(anyLong())).thenReturn(containersList);

    // fetch updated accounts and containers from db
    mySqlAccountService.fetchAndUpdateCache();

    Set<Account> accountsInMap = new HashSet<>(mySqlAccountService.getAllAccounts());

    // verify consumers are notified of updated accounts
    assertEquals("Number of updates by consumer received should be 1", 1, updatedAccountsReceivedByConsumers.size());
    assertEquals("Mismatch in number of accounts received by consumer", accountsInMap.size(),
        updatedAccountsReceivedByConsumers.get(0).size());
    assertTrue("Mismatch in accounts received by consumer",
        accountsInMap.containsAll(updatedAccountsReceivedByConsumers.get(0)));

    //Remove consumer
    mySqlAccountService.removeAccountUpdateConsumer(accountUpdateConsumer);
    updatedAccountsReceivedByConsumers.clear();

    Container newContainer =
        new ContainerBuilder((short) 3, "c3", Container.ContainerStatus.ACTIVE, "c3", (short) 1).build();
    when(mockMySqlAccountStore.getNewContainers(anyLong())).thenReturn(Collections.singletonList(newContainer));

    // fetch updated accounts and containers from db
    mySqlAccountService.fetchAndUpdateCache();

    // verify consumers are not notified
    assertEquals("No updates should be received", 0, updatedAccountsReceivedByConsumers.size());
  }
Tests adding/removing {@link Consumer}.
@throws Exception
ambry-account/src/test/java/com/github/ambry/account/MySqlAccountServiceTest.java