List of usage examples for java.util.concurrent Exchanger exchange
@SuppressWarnings("unchecked") public V exchange(V x) throws InterruptedException
From source file:com.streamsets.datacollector.execution.runner.common.TestProductionPipeline.java
@Test public void testRateLimit() throws Exception { final TestProducer p = new TestProducer(); MockStages.setSourceCapture(p);/*from ww w . java2 s . c om*/ final ProductionPipeline pipeline = createProductionPipeline(DeliveryGuarantee.AT_MOST_ONCE, true, 10L, false/*source not committer*/); pipeline.registerStatusListener(new MyStateListener()); final Exchanger<Double> rate = new Exchanger<>(); new Thread() { @Override public void run() { try { long start = System.nanoTime(); pipeline.run(); rate.exchange(p.count.doubleValue() * 1000 * 1000 * 1000 / (System.nanoTime() - start)); } catch (Exception ex) { } } }.start(); Thread.sleep(10000); pipeline.stop(); Double rateAchieved = rate.exchange(0.0); // To account for the slight loss of precision, we compare the "long-ified" versions. Assert.assertTrue(rateAchieved.longValue() <= 10); }
From source file:org.jdesktop.swingworker.AccumulativeRunnable.java
public final void testDone() throws Exception { final String testString = "test"; final Exchanger<Boolean> exchanger = new Exchanger<Boolean>(); SwingWorker<?,?> test = new SwingWorker<String, Object>() { @Override/*from ww w . java2 s . c o m*/ protected String doInBackground() throws Exception { return testString; } @Override protected void done() { try { exchanger.exchange( testString == get() && SwingUtilities.isEventDispatchThread()); } catch (Exception ignore) { } } }; test.execute(); assertTrue(exchanger.exchange(null, TIME_OUT, TIME_OUT_UNIT)); }
From source file:org.jdesktop.swingworker.AccumulativeRunnable.java
public final void testPropertyChange() throws Exception { final Exchanger<Boolean> boolExchanger = new Exchanger<Boolean>(); final SwingWorker<?,?> test = new SwingWorker<Object, Object>() { @Override//ww w .jav a2 s . co m protected Object doInBackground() throws Exception { firePropertyChange("test", null, "test"); return null; } }; test.addPropertyChangeListener( new PropertyChangeListener() { boolean isOnEDT = true; public void propertyChange(PropertyChangeEvent evt) { isOnEDT &= SwingUtilities.isEventDispatchThread(); if ("state".equals(evt.getPropertyName()) && StateValue.DONE == evt.getNewValue()) { try { boolExchanger.exchange(isOnEDT); } catch (Exception ignore) { ignore.printStackTrace(); } } } }); test.execute(); assertTrue(boolExchanger.exchange(null, TIME_OUT, TIME_OUT_UNIT)); }
From source file:org.jdesktop.swingworker.AccumulativeRunnable.java
public final void testWorkFlow() throws Exception { final List<Object> goldenSequence = Arrays.asList(new Object[]{StateValue.STARTED, "done", StateValue.DONE}); final List<Object> sequence = Collections.synchronizedList(new ArrayList<Object>()); final Exchanger<List<Object>> listExchanger = new Exchanger<List<Object>>(); /*from ww w .j av a 2s . c o m*/ final SwingWorker<?,?> test = new SwingWorker<Object,Object>() { @Override protected Object doInBackground() throws Exception { return null; } @Override protected void done() { sequence.add("done"); } }; test.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if ("state".equals(evt.getPropertyName())) { sequence.add(evt.getNewValue()); if (StateValue.DONE == evt.getNewValue()) { try { listExchanger.exchange(sequence); } catch (Exception ignore) { ignore.printStackTrace(); } } } } }); test.execute(); assertEquals(goldenSequence, listExchanger.exchange(null, TIME_OUT, TIME_OUT_UNIT)); }
From source file:org.jdesktop.swingworker.AccumulativeRunnable.java
public final void test6493680() throws Exception { final AtomicInteger lastProgressValue = new AtomicInteger(-1); final Exchanger<Boolean> exchanger = new Exchanger<Boolean>(); class Test {/*from w w w . java 2 s. c om*/ private final AtomicInteger lastProgressValue = new AtomicInteger(-1); private final Exchanger<Boolean> exchanger = new Exchanger<Boolean>(); boolean test() throws Exception { TestSwingWorker swingWorker = new TestSwingWorker(); swingWorker.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if ("progress" == evt.getPropertyName()) { lastProgressValue.set((Integer) evt.getNewValue()); } } }); swingWorker.execute(); return exchanger.exchange(true); } class TestSwingWorker extends SwingWorker<Void, Void> { @Override protected Void doInBackground() throws Exception { for (int i = 0; i <= 100; i++) { Thread.sleep(1); setProgress(i); } return null; } @Override protected void done() { boolean isPassed = (lastProgressValue.get() == 100); try { exchanger.exchange(isPassed); } catch (Exception ingore) { } } } } /* * because timing is involved in this bug we will run the test * NUMBER_OF_TRIES times. * the tes`t passes if it does not fail once. */ final int NUMBER_OF_TRIES = 50; for (int i = 0; i < NUMBER_OF_TRIES; i++) { assertTrue((new Test()).test()); } }
From source file:org.jdesktop.swingworker.AccumulativeRunnable.java
public final void testPublishAndProcess() throws Exception { final Exchanger<List<Integer>> listExchanger = new Exchanger<List<Integer>>(); final Exchanger<Boolean> boolExchanger = new Exchanger<Boolean>(); SwingWorker<List<Integer>,Integer> test = new SwingWorker<List<Integer>, Integer>() { List<Integer> receivedArgs = Collections.synchronizedList(new ArrayList<Integer>()); Boolean isOnEDT = Boolean.TRUE; final int NUMBERS = 100; @Override//from w ww .j ava2 s.c o m protected List<Integer> doInBackground() throws Exception { List<Integer> ret = Collections.synchronizedList( new ArrayList<Integer>(NUMBERS)); for (int i = 0; i < NUMBERS; i++) { publish(i); ret.add(i); } return ret; } @Override protected void process(List<Integer> args) { for(Integer i : args) { receivedArgs.add(i); } isOnEDT = isOnEDT && SwingUtilities.isEventDispatchThread(); if (receivedArgs.size() == NUMBERS) { try { boolExchanger.exchange(isOnEDT); listExchanger.exchange(receivedArgs); } catch (InterruptedException ignore) { ignore.printStackTrace(); } } } }; test.execute(); assertTrue(boolExchanger.exchange(null, TIME_OUT, TIME_OUT_UNIT)); assertEquals(test.get(TIME_OUT, TIME_OUT_UNIT), listExchanger.exchange(null, TIME_OUT, TIME_OUT_UNIT)); }