@Override public Topic<T> broadcast() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>boundedNonBlockingQueue(1000) .build() .withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); return topic; Queue<T> queue = QueueFactories.<T>boundedNonBlockingQueue(1000) .build() .withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); Subscription s = source.subscribe(topic::offer, e -> topic.close(), () -> topic.close());
@Test public void publishToAndMerge(){ com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t= new Thread( ()-> { while(true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing!"); queue.close(); } }); t.start(); assertThat(Spouts.of(1,2,3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(), Matchers.equalTo(Arrays.asList(1,1,2,2,3,3))); } @Test
@Test public void publishTest() { for (int k = 0; k < ITERATIONS; k++) { Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { try { System.out.println("Sleeping!"); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Waking!"); System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); of(1, 2, 3).peek(i -> System.out.println("publishing " + i)) .publishTo(queue) .forEach(System.out::println); assertThat(queue.stream().collect(Collectors.toList()), equalTo(Arrays.asList(1, 2, 3))); t = null; System.gc(); } }
@Test public void publishToAndMerge(){ com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t= new Thread( ()-> { while(true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing!"); queue.close(); } }); t.start(); assertThat(ReactiveSeq.of(1,2,3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(),equalTo(Arrays.asList(1,1,2,2,3,3))); }
@Test public void mergeAdapterTest() { for (int k = 0; k < ITERATIONS; k++) { Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { queue.add(1); queue.add(2); queue.add(3); try { System.out.println("Sleeping!"); Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Waking!"); System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); assertThat(this.<Integer>of().peek(i -> System.out.println("publishing " + i)) .merge(queue).collect(Collectors.toList()), equalTo(Arrays.asList(1, 2, 3))); t = null; System.gc(); } }
@Test public void publishToAndMerge() { for (int k = 0; k < ITERATIONS; k++) { System.out.println("Publish toNested and zip iteration " + k); com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); AtomicBoolean complete = new AtomicBoolean(false); AtomicBoolean start = new AtomicBoolean(false); List<Integer> list = of(1, 2, 3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(); assertThat(list, hasItems(1, 2, 3)); assertThat(list.size(), equalTo(6)); System.gc(); } }
@Test public void mergeAdapterTest1() { for (int k = 0; k < ITERATIONS; k++) { System.out.println("Test iteration " + k); Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { queue.add(1); queue.add(2); queue.add(3); try { // System.out.println("Sleeping!"); Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } // System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); assertThat(this.<Integer>of(10).peek(i -> System.out.println("publishing " + i)) .merge(queue).collect(Collectors.toList()), hasItems(10, 1, 2, 3)); t = null; System.gc(); } }
/** * Use an Agrona ManyToOneConcurrentArrayQueue for the next operations (wait-free, mechanical sympathy). * Note Queued data will be somewhat limited by configured concurrency level, but that flatMap operations * can increase the amount of data to be buffered significantly. * * <pre> * {@code * FutureStream.of(col) * .boundedWaitFree(110) * .flatMap(Collection::stream) * .toList(); * } * </pre> * * @param size Buffer size * @return FutureStream backed by an Agrona ManyToOneConcurrentArrayQueue */ default FutureStream<U> boundedWaitFree(final int size) { return this.withQueueFactory(QueueFactories.boundedNonBlockingQueue(size)); }
@Override public Topic<T> broadcast() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>boundedNonBlockingQueue(1000) .build() .withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); return topic; Queue<T> queue = QueueFactories.<T>boundedNonBlockingQueue(1000) .build() .withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); Subscription s = source.subscribe(topic::offer, e -> topic.close(), () -> topic.close());