/** * Resets variables and settings, and cleans up if the store already exists. * @throws Exception Any unexpected exception. */ public HelixAccountServiceTest() throws Exception { helixConfigProps.setProperty( HelixPropertyStoreConfig.HELIX_PROPERTY_STORE_PREFIX + "zk.client.connection.timeout.ms", String.valueOf(ZK_CLIENT_CONNECTION_TIMEOUT_MS)); helixConfigProps.setProperty(HelixPropertyStoreConfig.HELIX_PROPERTY_STORE_PREFIX + "zk.client.session.timeout.ms", String.valueOf(ZK_CLIENT_SESSION_TIMEOUT_MS)); helixConfigProps.setProperty(HelixAccountServiceConfig.ZK_CLIENT_CONNECT_STRING_KEY, ZK_CONNECT_STRING); helixConfigProps.setProperty(HelixPropertyStoreConfig.HELIX_PROPERTY_STORE_PREFIX + "root.path", STORE_ROOT_PATH); accountBackupDir = Paths.get(TestUtils.getTempDir("account-backup")).toAbsolutePath(); helixConfigProps.setProperty(HelixAccountServiceConfig.BACKUP_DIRECTORY_KEY, accountBackupDir.toString()); vHelixConfigProps = new VerifiableProperties(helixConfigProps); storeConfig = new HelixPropertyStoreConfig(vHelixConfigProps); notifier = new MockNotifier<>(); mockHelixAccountServiceFactory = new MockHelixAccountServiceFactory(vHelixConfigProps, new MetricRegistry(), notifier, null); deleteStoreIfExists(); generateReferenceAccountsAndContainers(); }
/** * Writes a {@link ZNRecord} to {@link org.apache.helix.store.HelixPropertyStore}. * @param zNRecord The {@link ZNRecord} to write. * @throws Exception Any unexpected exception. */ private void writeZNRecordToHelixPropertyStore(ZNRecord zNRecord, boolean shouldNotify) throws Exception { HelixStoreOperator storeOperator = new HelixStoreOperator(mockHelixAccountServiceFactory.getHelixStore(ZK_CONNECT_STRING, storeConfig)); storeOperator.write(HelixAccountService.FULL_ACCOUNT_METADATA_PATH, zNRecord); if (shouldNotify) { notifier.publish(ACCOUNT_METADATA_CHANGE_TOPIC, FULL_ACCOUNT_METADATA_CHANGE_MESSAGE); } }
/** * Tests receiving a bad topic, it will not be recognized by {@link HelixAccountService}, but will also not * crash the service. * @throws Exception Any unexpected exception. */ @Test public void receiveBadTopic() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); updateAccountsAndAssertAccountExistence(idToRefAccountMap.values(), NUM_REF_ACCOUNT, true); notifier.publish("badTopic", FULL_ACCOUNT_METADATA_CHANGE_MESSAGE); assertEquals("The number of account in HelixAccountService is different from expected", NUM_REF_ACCOUNT, accountService.getAllAccounts().size()); }
/** * Tests receiving a bad message, it will not be recognized by {@link HelixAccountService}, but will also not * crash the service. * @throws Exception Any unexpected exception. */ @Test public void receiveBadMessage() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); updateAccountsAndAssertAccountExistence(idToRefAccountMap.values(), NUM_REF_ACCOUNT, true); notifier.publish(ACCOUNT_METADATA_CHANGE_TOPIC, "badMessage"); assertEquals("The number of account in HelixAccountService is different from expected", NUM_REF_ACCOUNT, accountService.getAllAccounts().size()); }
/** * Pre-populates a collection of {@link Account}s to the underlying {@link org.apache.helix.store.HelixPropertyStore} * using {@link com.github.ambry.clustermap.HelixStoreOperator} (not through the {@link HelixAccountService}). This method * does not check any conflict among the {@link Account}s to write. * @throws Exception Any unexpected exception. */ private void writeAccountsToHelixPropertyStore(Collection<Account> accounts, boolean shouldNotify) throws Exception { HelixStoreOperator storeOperator = new HelixStoreOperator(mockHelixAccountServiceFactory.getHelixStore(ZK_CONNECT_STRING, storeConfig)); ZNRecord zNRecord = new ZNRecord(String.valueOf(System.currentTimeMillis())); Map<String, String> accountMap = new HashMap<>(); for (Account account : accounts) { accountMap.put(String.valueOf(account.getId()), account.toJson(true).toString()); } zNRecord.setMapField(ACCOUNT_METADATA_MAP_KEY, accountMap); // Write account metadata into HelixPropertyStore. storeOperator.write(HelixAccountService.FULL_ACCOUNT_METADATA_PATH, zNRecord); if (shouldNotify) { notifier.publish(ACCOUNT_METADATA_CHANGE_TOPIC, FULL_ACCOUNT_METADATA_CHANGE_MESSAGE); } }