List of usage examples for java.util.concurrent.atomic AtomicInteger addAndGet
public final int addAndGet(int delta)
From source file:org.apache.hadoop.hbase.mapreduce.TestLoadIncrementalHFilesSplitRecovery.java
@Test public void testGroupOrSplitWhenRegionHoleExistsInMeta() throws Exception { String tableName = "testGroupOrSplitWhenRegionHoleExistsInMeta"; byte[][] SPLIT_KEYS = new byte[][] { Bytes.toBytes("row_00000100") }; HTable table = new HTable(util.getConfiguration(), Bytes.toBytes(tableName)); setupTableWithSplitkeys(tableName, 10, SPLIT_KEYS); Path dir = buildBulkFiles(tableName, 2); final AtomicInteger countedLqis = new AtomicInteger(); LoadIncrementalHFiles loader = new LoadIncrementalHFiles(util.getConfiguration()) { protected List<LoadQueueItem> groupOrSplit(Multimap<ByteBuffer, LoadQueueItem> regionGroups, final LoadQueueItem item, final HTable htable, final Pair<byte[][], byte[][]> startEndKeys) throws IOException { List<LoadQueueItem> lqis = super.groupOrSplit(regionGroups, item, htable, startEndKeys); if (lqis != null) { countedLqis.addAndGet(lqis.size()); }// w w w . ja v a2 s .c o m return lqis; } }; // do bulkload when there is no region hole in hbase:meta. try { loader.doBulkLoad(dir, table); } catch (Exception e) { LOG.error("exeception=", e); } // check if all the data are loaded into the table. this.assertExpectedTable(tableName, ROWCOUNT, 2); dir = buildBulkFiles(tableName, 3); // Mess it up by leaving a hole in the hbase:meta CatalogTracker ct = new CatalogTracker(util.getConfiguration()); List<HRegionInfo> regionInfos = MetaReader.getTableRegions(ct, TableName.valueOf(tableName)); for (HRegionInfo regionInfo : regionInfos) { if (Bytes.equals(regionInfo.getStartKey(), HConstants.EMPTY_BYTE_ARRAY)) { MetaEditor.deleteRegion(ct, regionInfo); break; } } try { loader.doBulkLoad(dir, table); } catch (Exception e) { LOG.error("exeception=", e); assertTrue("IOException expected", e instanceof IOException); } table.close(); this.assertExpectedTable(tableName, ROWCOUNT, 2); }
From source file:com.datasnap.android.integration.IntegrationManagerTest.java
@Test public void testIntegrationSelection() { final String key = "Some Integration"; // create a settings cache that will insert fake provider settings for this key SettingsCache settingsCache = new SettingsCache(context, layer, Defaults.SETTINGS_CACHE_EXPIRY) { @Override//ww w .j a va 2 s .c o m public EasyJSONObject getSettings() { // get them directly from the server with blocking EasyJSONObject settings = requester.fetchSettings(); if (settings != null) settings.putObject(key, new JSONObject()); return settings; } }; // make the sure the settings cache has nothing in it right now settingsCache.reset(); IntegrationManager integrationManager = new IntegrationManager(settingsCache); // removes all the providers integrationManager.getIntegrations().clear(); final AtomicInteger identifies = new AtomicInteger(); final AtomicInteger tracks = new AtomicInteger(); final AtomicInteger screens = new AtomicInteger(); final AtomicInteger aliases = new AtomicInteger(); Integration provider = new SimpleIntegration() { @Override public void onCreate(Context context) { ready(); } @Override public String getKey() { return key; } @Override public void validate(EasyJSONObject settings) throws InvalidSettingsException { } @Override public void identify(Identify identify) { identifies.addAndGet(1); } @Override public void track(Track track) { tracks.addAndGet(1); } @Override public void screen(Screen screen) { screens.addAndGet(1); } @Override public void alias(Alias alias) { aliases.addAndGet(1); } }; // add a simple adding provider integrationManager.addIntegration(provider); // get the settings from the server, which won't include this provider integrationManager.refresh(); // call the method that enables it integrationManager.onCreate(context); // // Test the no specific context.providers added // integrationManager.identify(TestCases.identify()); assertThat(identifies.get()).isEqualTo(1); integrationManager.track(TestCases.track()); assertThat(tracks.get()).isEqualTo(1); integrationManager.screen(TestCases.screen()); assertThat(screens.get()).isEqualTo(1); integrationManager.alias(TestCases.alias()); assertThat(aliases.get()).isEqualTo(1); // // Assemble test values // Identify identify = TestCases.identify(); String userId = identify.getUserId(); Traits traits = identify.getTraits(); Calendar timestamp = Calendar.getInstance(); Track track = TestCases.track(); String event = TestCases.track().getEvent(); Props properties = track.getProperties(); Alias alias = TestCases.alias(); String from = alias.getPreviousId(); String to = alias.getUserId(); // // Test the integrations.all = false setting default to false // Options allFalseOptions = new Options().setTimestamp(timestamp).setIntegration("all", false); EasyJSONObject bundledIntegrations = new EasyJSONObject(); integrationManager.identify(new Identify(userId, traits, bundledIntegrations, allFalseOptions)); assertThat(identifies.get()).isEqualTo(1); integrationManager.track(new Track(userId, event, properties, bundledIntegrations, allFalseOptions)); assertThat(tracks.get()).isEqualTo(1); integrationManager.alias(new Alias(from, to, bundledIntegrations, allFalseOptions)); assertThat(aliases.get()).isEqualTo(1); // // Test the integrations[integration.key] = false turns it off // Options integrationFalseOptions = new Options().setTimestamp(timestamp).setIntegration(key, false); integrationManager.identify(new Identify(userId, traits, bundledIntegrations, integrationFalseOptions)); assertThat(identifies.get()).isEqualTo(1); integrationManager .track(new Track(userId, event, properties, bundledIntegrations, integrationFalseOptions)); assertThat(tracks.get()).isEqualTo(1); integrationManager.alias(new Alias(from, to, bundledIntegrations, integrationFalseOptions)); assertThat(aliases.get()).isEqualTo(1); // // Test the integrations[integration.key] = true, All=false keeps it on // Options integrationTrueOptions = new Options().setTimestamp(timestamp).setIntegration("all", false) .setIntegration(key, true); integrationManager.identify(new Identify(userId, traits, bundledIntegrations, integrationTrueOptions)); assertThat(identifies.get()).isEqualTo(2); integrationManager.track(new Track(userId, event, properties, bundledIntegrations, integrationTrueOptions)); assertThat(tracks.get()).isEqualTo(2); integrationManager.alias(new Alias(from, to, bundledIntegrations, integrationTrueOptions)); assertThat(aliases.get()).isEqualTo(2); }
From source file:com.indeed.lsmtree.recordcache.StandalonePersistentRecordCache.java
public Iterator<Either<Exception, P2<K, V>>> getStreaming(Iterator<K> keys, AtomicInteger progress, AtomicInteger skipped) {/*from www. j ava2 s . com*/ log.info("starting store lookups"); final List<Either<Exception, P2<K, V>>> ret = Lists.newArrayList(); int notFound = 0; while (keys.hasNext()) { final K key = keys.next(); final V value; try { value = index.get(key); } catch (IOException e) { log.error("error", e); return Iterators.singletonIterator(Left.<Exception, P2<K, V>>of(new IndexReadException(e))); } if (value != null) { ret.add(Right.<Exception, P2<K, V>>of(P.p(key, value))); } else { notFound++; } } if (progress != null) progress.addAndGet(notFound); if (skipped != null) skipped.addAndGet(notFound); log.info("store lookups complete"); return ret.iterator(); }
From source file:com.comcast.cqs.test.stress.CqsStressTester.java
public void addSendMessageCount(int second, int count) { AtomicInteger val = null; timeSendMessageCountMap.putIfAbsent(second, new AtomicInteger(0)); val = timeSendMessageCountMap.get(second); val.addAndGet(count); }
From source file:com.comcast.cqs.test.stress.CqsStressTester.java
public void addReceiveMessageCount(int second, int count) { AtomicInteger val = null; timeReceiveMessageCountMap.putIfAbsent(second, new AtomicInteger(0)); val = timeReceiveMessageCountMap.get(second); val.addAndGet(count); }
From source file:com.smartitengineering.cms.spi.impl.content.RubyGeneratorTest.java
@Test public void testMultiRubyRepGeneration() throws IOException { TypeRepresentationGenerator generator = new RubyRepresentationGenerator(); final RepresentationTemplate template = mockery.mock(RepresentationTemplate.class); WorkspaceAPIImpl impl = new WorkspaceAPIImpl() { @Override/* w w w .j a v a2 s. c o m*/ public RepresentationTemplate getRepresentationTemplate(WorkspaceId id, String name) { return template; } }; impl.setRepresentationGenerators(Collections.singletonMap(TemplateType.RUBY, generator)); final RepresentationProvider provider = new RepresentationProviderImpl(); final WorkspaceAPI api = impl; registerBeanFactory(api); final Content content = mockery.mock(Content.class); final Field field = mockery.mock(Field.class); final FieldValue value = mockery.mock(FieldValue.class); final Map<String, Field> fieldMap = mockery.mock(Map.class); final ContentType type = mockery.mock(ContentType.class); final Map<String, RepresentationDef> reps = mockery.mock(Map.class, "repMap"); final RepresentationDef def = mockery.mock(RepresentationDef.class); final int threadCount = new Random().nextInt(100); logger.info("Number of parallel threads " + threadCount); mockery.checking(new Expectations() { { exactly(threadCount).of(template).getTemplateType(); will(returnValue(TemplateType.RUBY)); exactly(threadCount).of(template).getTemplate(); final byte[] toByteArray = IOUtils.toByteArray( getClass().getClassLoader().getResourceAsStream("scripts/ruby/test-script.rb")); will(returnValue(toByteArray)); exactly(threadCount).of(template).getName(); will(returnValue(REP_NAME)); for (int i = 0; i < threadCount; ++i) { exactly(1).of(value).getValue(); will(returnValue(String.valueOf(i))); } exactly(threadCount).of(field).getValue(); will(returnValue(value)); exactly(threadCount).of(fieldMap).get(with(Expectations.<String>anything())); will(returnValue(field)); exactly(threadCount).of(content).getFields(); will(returnValue(fieldMap)); exactly(threadCount).of(content).getContentDefinition(); will(returnValue(type)); final ContentId contentId = mockery.mock(ContentId.class); exactly(2 * threadCount).of(content).getContentId(); will(returnValue(contentId)); final WorkspaceId wId = mockery.mock(WorkspaceId.class); exactly(threadCount).of(contentId).getWorkspaceId(); will(returnValue(wId)); exactly(2 * threadCount).of(type).getRepresentationDefs(); will(returnValue(reps)); exactly(2 * threadCount).of(reps).get(with(REP_NAME)); will(returnValue(def)); exactly(threadCount).of(def).getParameters(); will(returnValue(Collections.emptyMap())); exactly(threadCount).of(def).getMIMEType(); will(returnValue(GroovyGeneratorTest.MIME_TYPE)); final ResourceUri rUri = mockery.mock(ResourceUri.class); exactly(threadCount).of(def).getResourceUri(); will(returnValue(rUri)); exactly(threadCount).of(rUri).getValue(); will(returnValue("iUri")); } }); final Set<String> set = Collections.synchronizedSet(new LinkedHashSet<String>(threadCount)); final List<String> list = Collections.synchronizedList(new ArrayList<String>(threadCount)); final AtomicInteger integer = new AtomicInteger(0); Threads group = new Threads(); for (int i = 0; i < threadCount; ++i) { group.addThread(new Thread(new Runnable() { public void run() { Representation representation = provider.getRepresentation(REP_NAME, type, content); Assert.assertNotNull(representation); Assert.assertEquals(REP_NAME, representation.getName()); final String rep = StringUtils.newStringUtf8(representation.getRepresentation()); list.add(rep); set.add(rep); Assert.assertEquals(GroovyGeneratorTest.MIME_TYPE, representation.getMimeType()); integer.addAndGet(1); } })); } group.start(); try { group.join(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } logger.info("Generated reps list: " + list); logger.info("Generated reps set: " + set); Assert.assertEquals(threadCount, integer.get()); Assert.assertEquals(threadCount, list.size()); Assert.assertEquals(threadCount, set.size()); }
From source file:com.smartitengineering.cms.spi.impl.content.JavascriptGeneratorTest.java
@Test public void testMultiJavascriptRepGeneration() throws IOException { TypeRepresentationGenerator generator = new JavascriptRepresentationGenerator(); final RepresentationTemplate template = mockery.mock(RepresentationTemplate.class); WorkspaceAPIImpl impl = new WorkspaceAPIImpl() { @Override/*from w w w . j a v a 2s . com*/ public RepresentationTemplate getRepresentationTemplate(WorkspaceId id, String name) { return template; } }; impl.setRepresentationGenerators(Collections.singletonMap(TemplateType.JAVASCRIPT, generator)); final RepresentationProvider provider = new RepresentationProviderImpl(); registerBeanFactory(impl); final Content content = mockery.mock(Content.class); final Field field = mockery.mock(Field.class); final FieldValue value = mockery.mock(FieldValue.class); final Map<String, Field> fieldMap = mockery.mock(Map.class); final ContentType type = mockery.mock(ContentType.class); final Map<String, RepresentationDef> reps = mockery.mock(Map.class, "repMap"); final RepresentationDef def = mockery.mock(RepresentationDef.class); final int threadCount = new Random().nextInt(100); logger.info("Number of parallel threads " + threadCount); mockery.checking(new Expectations() { { exactly(threadCount).of(template).getTemplateType(); will(returnValue(TemplateType.JAVASCRIPT)); exactly(threadCount).of(template).getTemplate(); final byte[] toByteArray = IOUtils .toByteArray(getClass().getClassLoader().getResourceAsStream("scripts/js/test-script.js")); will(returnValue(toByteArray)); exactly(threadCount).of(template).getName(); will(returnValue(REP_NAME)); for (int i = 0; i < threadCount; ++i) { exactly(1).of(value).getValue(); will(returnValue(String.valueOf(i))); } exactly(threadCount).of(field).getValue(); will(returnValue(value)); exactly(threadCount).of(fieldMap).get(with(Expectations.<String>anything())); will(returnValue(field)); exactly(threadCount).of(content).getFields(); will(returnValue(fieldMap)); exactly(threadCount).of(content).getContentDefinition(); will(returnValue(type)); final ContentId contentId = mockery.mock(ContentId.class); exactly(2 * threadCount).of(content).getContentId(); will(returnValue(contentId)); final WorkspaceId wId = mockery.mock(WorkspaceId.class); exactly(threadCount).of(contentId).getWorkspaceId(); will(returnValue(wId)); exactly(2 * threadCount).of(type).getRepresentationDefs(); will(returnValue(reps)); exactly(2 * threadCount).of(reps).get(with(REP_NAME)); will(returnValue(def)); exactly(threadCount).of(def).getParameters(); will(returnValue(Collections.emptyMap())); exactly(threadCount).of(def).getMIMEType(); will(returnValue(GroovyGeneratorTest.MIME_TYPE)); final ResourceUri rUri = mockery.mock(ResourceUri.class); exactly(threadCount).of(def).getResourceUri(); will(returnValue(rUri)); exactly(threadCount).of(rUri).getValue(); will(returnValue("iUri")); } }); Assert.assertNotNull(SmartContentAPI.getInstance()); Assert.assertNotNull(SmartContentAPI.getInstance().getContentLoader()); final Set<String> set = Collections.synchronizedSet(new LinkedHashSet<String>(threadCount)); final List<String> list = Collections.synchronizedList(new ArrayList<String>(threadCount)); final AtomicInteger integer = new AtomicInteger(0); Threads group = new Threads(); for (int i = 0; i < threadCount; ++i) { group.addThread(new Thread(new Runnable() { public void run() { Representation representation = provider.getRepresentation(REP_NAME, type, content); Assert.assertNotNull(representation); Assert.assertEquals(REP_NAME, representation.getName()); final String rep = StringUtils.newStringUtf8(representation.getRepresentation()); list.add(rep); set.add(rep); Assert.assertEquals(GroovyGeneratorTest.MIME_TYPE, representation.getMimeType()); integer.addAndGet(1); } })); } group.start(); try { group.join(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } logger.info("Generated reps list: " + list); logger.info("Generated reps set: " + set); Assert.assertEquals(threadCount, integer.get()); Assert.assertEquals(threadCount, list.size()); Assert.assertEquals(threadCount, set.size()); }
From source file:com.smartitengineering.cms.spi.impl.content.GroovyGeneratorTest.java
@Test public void testMultiGroovyRepGeneration() throws IOException { TypeRepresentationGenerator generator = new GroovyRepresentationGenerator(); final RepresentationTemplate template = mockery.mock(RepresentationTemplate.class); WorkspaceAPIImpl impl = new WorkspaceAPIImpl() { @Override//from ww w . j a v a 2 s . c om public RepresentationTemplate getRepresentationTemplate(WorkspaceId id, String name) { return template; } }; impl.setRepresentationGenerators(Collections.singletonMap(TemplateType.GROOVY, generator)); final RepresentationProvider provider = new RepresentationProviderImpl(); final WorkspaceAPI api = impl; registerBeanFactory(api); final Content content = mockery.mock(Content.class); final Field field = mockery.mock(Field.class); final FieldValue value = mockery.mock(FieldValue.class); final ContentType type = mockery.mock(ContentType.class); final Map<String, RepresentationDef> reps = mockery.mock(Map.class, "repMap"); final RepresentationDef def = mockery.mock(RepresentationDef.class); final int threadCount = new Random().nextInt(100); logger.info("Number of parallel threads " + threadCount); mockery.checking(new Expectations() { { exactly(threadCount).of(template).getTemplateType(); will(returnValue(TemplateType.GROOVY)); exactly(threadCount).of(template).getTemplate(); final byte[] toByteArray = IOUtils.toByteArray(getClass().getClassLoader() .getResourceAsStream("scripts/groovy/GroovyTestRepresentationGenerator.groovy")); will(returnValue(toByteArray)); exactly(threadCount).of(template).getName(); will(returnValue(REP_NAME)); for (int i = 0; i < threadCount; ++i) { exactly(1).of(value).getValue(); will(returnValue(String.valueOf(i))); } exactly(threadCount).of(field).getValue(); will(returnValue(value)); exactly(threadCount).of(content).getField(with(Expectations.<String>anything())); will(returnValue(field)); exactly(threadCount).of(content).getContentDefinition(); will(returnValue(type)); final ContentId contentId = mockery.mock(ContentId.class); exactly(2 * threadCount).of(content).getContentId(); will(returnValue(contentId)); final WorkspaceId wId = mockery.mock(WorkspaceId.class); exactly(threadCount).of(contentId).getWorkspaceId(); will(returnValue(wId)); exactly(2 * threadCount).of(type).getRepresentationDefs(); will(returnValue(reps)); exactly(2 * threadCount).of(reps).get(with(REP_NAME)); will(returnValue(def)); exactly(threadCount).of(def).getParameters(); will(returnValue(Collections.emptyMap())); exactly(threadCount).of(def).getMIMEType(); will(returnValue(GroovyGeneratorTest.MIME_TYPE)); final ResourceUri rUri = mockery.mock(ResourceUri.class); exactly(threadCount).of(def).getResourceUri(); will(returnValue(rUri)); exactly(threadCount).of(rUri).getValue(); will(returnValue("iUri")); } }); final Set<String> set = Collections.synchronizedSet(new LinkedHashSet<String>(threadCount)); final List<String> list = Collections.synchronizedList(new ArrayList<String>(threadCount)); final AtomicInteger integer = new AtomicInteger(0); Threads group = new Threads(); for (int i = 0; i < threadCount; ++i) { group.addThread(new Thread(new Runnable() { public void run() { Representation representation = provider.getRepresentation(REP_NAME, type, content); Assert.assertNotNull(representation); Assert.assertEquals(REP_NAME, representation.getName()); final String rep = StringUtils.newStringUtf8(representation.getRepresentation()); list.add(rep); set.add(rep); Assert.assertEquals(GroovyGeneratorTest.MIME_TYPE, representation.getMimeType()); integer.addAndGet(1); } })); } group.start(); try { group.join(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } logger.info("Generated reps list: " + list); logger.info("Generated reps set: " + set); Assert.assertEquals(threadCount, integer.get()); Assert.assertEquals(threadCount, list.size()); Assert.assertEquals(threadCount, set.size()); }
From source file:org.apache.hadoop.net.unix.TestDomainSocket.java
@Test(timeout = 180000) public void testShutdown() throws Exception { final AtomicInteger bytesRead = new AtomicInteger(0); final AtomicBoolean failed = new AtomicBoolean(false); final DomainSocket[] socks = DomainSocket.socketpair(); Runnable reader = new Runnable() { @Override//from www .ja va 2s . c o m public void run() { while (true) { try { int ret = socks[1].getInputStream().read(); if (ret == -1) return; bytesRead.addAndGet(1); } catch (IOException e) { DomainSocket.LOG.error("reader error", e); failed.set(true); return; } } } }; Thread readerThread = new Thread(reader); readerThread.start(); socks[0].getOutputStream().write(1); socks[0].getOutputStream().write(2); socks[0].getOutputStream().write(3); Assert.assertTrue(readerThread.isAlive()); socks[0].shutdown(); readerThread.join(); Assert.assertFalse(failed.get()); Assert.assertEquals(3, bytesRead.get()); IOUtils.cleanup(null, socks); }
From source file:org.duracloud.snapshot.service.impl.SpaceItemWriterTest.java
/** * Distributes the items into sublists and runs them in separate threads. * @param items/*from w w w.j a v a 2s .c o m*/ * @param threads * @throws IOException */ private void writeItems(List<ContentItem> items, int threads) throws IOException { int itemCount = items.size(); final CountDownLatch countdownLatch = new CountDownLatch(threads); int bottomIndex = 0; int itemsPerThread = itemCount / threads; int remainder = itemCount % threads; int thread = 0; final AtomicInteger processed = new AtomicInteger(0); while (bottomIndex < itemCount) { thread++; final int fromIndex = bottomIndex; int topIndex = fromIndex + itemsPerThread; if (thread == threads) { topIndex += remainder; } final int toIndex = topIndex; final List<ContentItem> contents = items.subList(fromIndex, toIndex); new Thread(new Runnable() { /* (non-Javadoc) * @see java.lang.Runnable#run() */ @Override public void run() { try { writer.beforeWrite(contents); writer.write(contents); writer.afterWrite(contents); processed.addAndGet(contents.size()); countdownLatch.countDown(); } catch (IOException e) { throw new RuntimeException(e); } } }).start(); bottomIndex = topIndex; } try { assertTrue(countdownLatch.await(20, TimeUnit.SECONDS)); } catch (InterruptedException e) { e.printStackTrace(); } assertEquals(items.size(), processed.get()); }