public void testSetIfUnset() throws InterruptedException { final BlockingCell<String> cell = new BlockingCell<String>(); assertTrue(cell.setIfUnset("foo")); assertEquals("foo", cell.get()); assertFalse(cell.setIfUnset("bar")); assertEquals("foo", cell.get()); }
public void testGetWaitsUntilSet() throws InterruptedException { final BlockingCell<String> cell = new BlockingCell<String>(); final String value = "foo"; final AtomicReference<Object> valueHolder = new AtomicReference<Object>(); final AtomicBoolean doneHolder = new AtomicBoolean(false); Thread getterThread = new Thread() { @Override public void run() { try { valueHolder.set(cell.get()); } catch (InterruptedException ex) { fail("hit InterruptedException"); ex.printStackTrace(); } doneHolder.set(true); } }; getterThread.start(); Thread.sleep(300); assertFalse(doneHolder.get()); cell.set(value); getterThread.join(); assertTrue(doneHolder.get()); assertTrue(value == valueHolder.get()); }
public void testDoubleSet() throws InterruptedException { BlockingCell<String> cell = new BlockingCell<String>(); cell.set("one"); assertEquals("one", cell.get()); try { cell.set("two"); } catch (AssertionError ae) { return; } fail("Expected AssertionError"); }
@Override public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) { System.out.println( "Shutdown signal terminating consumer " + consumerTag + " with signal " + sig); if (sig.getCause() != null) { sig.printStackTrace(); } _blocker.setIfUnset(sig); }
public void report(boolean writeStats) throws IOException { Object sentinel = _blocker.uninterruptibleGet(); if (sentinel instanceof ShutdownSignalException) { System.out.println("Aborted with shutdown signal in consumer."); System.exit(1); } long totalDelta = _mostRecentTime - _startTime; long maxL, minL; double sumL; maxL = Long.MIN_VALUE; minL = Long.MAX_VALUE; sumL = 0.0; int messageCount = _received; for (int i = 0; i < messageCount; i++) { long v = _deltas[i]; if (v > maxL) maxL = v; if (v < minL) minL = v; sumL += v; } double avgL = sumL / messageCount; System.out.println("CONSUMER - Message count: " + messageCount); System.out.println("Total time, milliseconds: " + totalDelta); System.out.println("Overall messages-per-second: " + (messageCount / (totalDelta / 1000.0))); System.out.println("Min latency, milliseconds: " + minL); System.out.println("Avg latency, milliseconds: " + avgL); System.out.println("Max latency, milliseconds: " + maxL); if (writeStats) { PrintStream o = new PrintStream(new FileOutputStream("simple-latency-experiment.csv")); for (int i = 0; i < messageCount; i++) { o.println(i + "," + _deltas[i]); } o.close(); int[] bins = new int[(int) maxL + 1]; for (int i = 0; i < messageCount; i++) { if (_deltas[i] != 0) { bins[(int) _deltas[i]]++; } } o = new PrintStream(new FileOutputStream("simple-latency-bins.csv")); for (int i = 0; i < bins.length; i++) { o.println(i + "," + bins[i]); } o.close(); } }
public void testNullSet() throws InterruptedException { BlockingCell<Integer> c = new BlockingCell<Integer>(); c.set(null); assertNull(c.get()); }
public void testMultiGet() throws InterruptedException { final BlockingCell<String> cell = new BlockingCell<String>(); cell.set("one"); assertEquals("one", cell.get()); assertEquals("one", cell.get()); }
public void finish() throws IOException { if (!_autoAck) getChannel().basicAck(0, true); _blocker.setIfUnset(new Object()); }