public boolean isAllowable() { long now = System.currentTimeMillis(); if (now > lastResetTime + interval) { token.set(rate); lastResetTime = now; } int value = token.get(); boolean flag = false; while (value > 0 && !flag) { flag = token.compareAndSet(value, value - 1); value = token.get(); } return flag; }
private boolean isSkip() { int skip = connectSkip.get(); // Growth of skipping times if (skip >= 10) { // If the number of skipping times increases by more than 10, take the random number if (connectRandom == 0) { connectRandom = ThreadLocalRandom.current().nextInt(10); } skip = 10 + connectRandom; } if (connectSkipped.getAndIncrement() < skip) { // Check the number of skipping times return true; } connectSkip.incrementAndGet(); connectSkipped.set(0); connectRandom = 0; return false; }
private int getCurrentId() { if (idGenerator.get() > MAX_ID) { idGenerator.set(0); } return idGenerator.incrementAndGet(); }
@Test public void testSynchronousDisconnect() { final AtomicInteger effectCounter = new AtomicInteger(); Observable<Integer> source = Observable.just(1, 2, 3, 4) .doOnNext(new Consumer<Integer>() { effectCounter.set(0); System.out.printf("- %d -%n", i); result.subscribe(new Consumer<Integer>() { assertEquals(2, effectCounter.get());
@Test public void testOnSuccessCalled() { final AtomicInteger sequence = new AtomicInteger(0); final AtomicInteger action = new AtomicInteger(); final AtomicInteger onSuccess = new AtomicInteger(); Continuations CONT = new Continuations(); CONT.doSubmit(() -> { action.set(sequence.incrementAndGet()); }); CONT.doSubmit(() -> { onSuccess.set(sequence.incrementAndGet()); }); assertEquals(action.get(), 1); assertEquals(onSuccess.get(), 2); }
@Override public List<Integer> chooseTasks(int taskId, List<Object> values) { int rightNow; while (true) { rightNow = current.incrementAndGet(); if (rightNow < capacity) { return rets[choices[rightNow]]; } else if (rightNow == capacity) { current.set(0); return rets[choices[0]]; } //race condition with another thread, and we lost // try again } }
@Test public void testBilto1() { context.runOnContext(v1 -> { buffer = new InboundBuffer<>(context, 4L); AtomicInteger drained = new AtomicInteger(); AtomicInteger expectedDrained = new AtomicInteger(); buffer.drainHandler(v2 -> { switch (drained.getAndIncrement()) { case 0: // Check that emitting again will not drain again expectedDrained.set(1); assertTrue(emit()); context.runOnContext(v -> { assertEquals(1, drained.get()); testComplete(); }); break; } }); buffer.handler(val -> { if (val == 0) { // This will set writable to false fill(); } assertEquals(expectedDrained.get(), drained.get()); }); assertTrue(emit()); }); await(); }
/** * The {@code FileRecords.open} methods should be used instead of this constructor whenever possible. * The constructor is visible for tests. */ FileRecords(File file, FileChannel channel, int start, int end, boolean isSlice) throws IOException { this.file = file; this.channel = channel; this.start = start; this.end = end; this.isSlice = isSlice; this.size = new AtomicInteger(); if (isSlice) { // don't check the file size if this is just a slice view size.set(end - start); } else { if (channel.size() > Integer.MAX_VALUE) throw new KafkaException("The size of segment " + file + " (" + channel.size() + ") is larger than the maximum allowed segment size of " + Integer.MAX_VALUE); int limit = Math.min((int) channel.size(), end); size.set(limit - start); // if this is not a slice, update the file pointer to the end of the file // set the file position to the last byte in the file channel.position(limit); } batches = batchesFrom(start); }
@Test(dataProvider = "unsuccessful") public void getWhenSuccessful_fails(CompletableFuture<?> future) { if ((future != null) && !future.isDone()) { AtomicInteger result = new AtomicInteger(); ConcurrentTestHarness.execute(() -> { result.set(1); Object value = Async.getWhenSuccessful(future); result.set((value == null) ? 2 : 3); }); Awaits.await().untilAtomic(result, is(1)); future.obtrudeException(new IllegalStateException()); Awaits.await().untilAtomic(result, is(not(1))); assertThat(result.get(), is(2)); } assertThat(Async.getWhenSuccessful(future), is(nullValue())); }
@Test public void testOnSuccessNotCalledWhenException() { final AtomicInteger sequence = new AtomicInteger(0); final AtomicInteger onSuccess = new AtomicInteger(); Continuations CONT = new Continuations(); try { CONT.doSubmit(() -> { throw new RuntimeException("test"); }); CONT.doSubmit(() -> { onSuccess.set(sequence.incrementAndGet()); }); fail("should have thrown exception"); } catch (Exception e) { assertEquals(e.getMessage(), "test"); } assertEquals(onSuccess.get(), 0); }
/** * select 1/freq */ public boolean countCheck() { i.incrementAndGet(); if (i.get() > freq) { target = r.nextInt(freq); i.set(0); } return i.get() == target; }
public boolean isAllowable() { long now = System.currentTimeMillis(); if (now > lastResetTime + interval) { token.set(rate); lastResetTime = now; } int value = token.get(); boolean flag = false; while (value > 0 && !flag) { flag = token.compareAndSet(value, value - 1); value = token.get(); } return flag; }
private boolean isSkip() { int skip = connectSkip.get(); // Growth of skipping times if (skip >= 10) { // If the number of skipping times increases by more than 10, take the random number if (connectRandom == 0) { connectRandom = ThreadLocalRandom.current().nextInt(10); } skip = 10 + connectRandom; } if (connectSkipped.getAndIncrement() < skip) { // Check the number of skipping times return true; } connectSkip.incrementAndGet(); connectSkipped.set(0); connectRandom = 0; return false; }
@Override public List<Integer> chooseTasks(int taskId, List<Object> values) { int rightNow; int size = choices.size(); while (true) { rightNow = current.incrementAndGet(); if (rightNow < size) { return choices.get(rightNow); } else if (rightNow == size) { current.set(0); return choices.get(0); } } // race condition with another thread, and we lost. try again } }
@Test public void filters() { AtomicInteger filterCount = new AtomicInteger(); assertEquals(4, filterCount.get()); }) .verifyComplete(); filterCount.set(0);
@Test public void getWhenSuccessful_success_async() { CompletableFuture<Integer> future = new CompletableFuture<Integer>(); AtomicInteger result = new AtomicInteger(); ConcurrentTestHarness.execute(() -> { result.set(1); result.set(Async.getWhenSuccessful(future)); }); Awaits.await().untilAtomic(result, is(1)); future.obtrudeValue(2); Awaits.await().untilAtomic(result, is(2)); }
@Test public void testSubscribe(){ List<Integer> expected = Arrays.asList( 10, 9, 9, 8, 7, 6, 6, 5, 5, 5, 4, 3, 3, 2, 2, 1, 1, 0); final AtomicInteger i = new AtomicInteger(); Observable.from(expected).doOnNext(x -> { logger.info("print " + x); i.set(x); }).doOnError(e -> logger.error(e.getMessage())).subscribe(); logger.info("last"); assertTrue(i.get()==0); }
@Test public void testConsistentCloseOrder() { final AtomicInteger closed1 = new AtomicInteger(); final AtomicInteger closed2 = new AtomicInteger(); final AtomicInteger counter = new AtomicInteger(); Sequence<Integer> sequence = Sequences .simple(Arrays.asList(1, 2, 3)) .withBaggage(() -> closed1.set(counter.incrementAndGet())) .withBaggage(() -> closed2.set(counter.incrementAndGet())); // Run sequence via accumulate sequence.toList(); Assert.assertEquals(1, closed1.get()); Assert.assertEquals(2, closed2.get()); // Ensure sequence runs via Yielder, because LimitedSequence extends YieldingSequenceBase Sequence<Integer> yieldingSequence = sequence.limit(1); yieldingSequence.toList(); Assert.assertEquals(3, closed1.get()); Assert.assertEquals(4, closed2.get()); } }