@Test public void testPollWait() throws InterruptedException, IOException { final FileBlockingQueue<String> queue = getFileBlockingQueue(); final AtomicLong start = new AtomicLong(System.currentTimeMillis()); String m = queue.poll(1000, TimeUnit.MILLISECONDS); final AtomicLong duration = new AtomicLong(System.currentTimeMillis() - start.get()); assertTrue(duration.get() >= 1000 && duration.get() <= 2000); assertNull(m); ExecutorService e = Executors.newFixedThreadPool(1); e.execute( new Runnable() { @Override public void run() { start.set(System.currentTimeMillis()); String m = null; try { m = queue.poll(5000, TimeUnit.MILLISECONDS); } catch (InterruptedException e1) { throw new RuntimeException(e1); } assertNotNull(m); duration.set(System.currentTimeMillis() - start.get()); } }); Thread.sleep(1000); queue.offer("testString"); assertTrue(duration.get() < 4000); }
@Override public boolean offer(Message msg) { try { return queue.offer(msg); } catch (Exception e) { log.error("Exception on offer: " + e.getMessage(), e); return false; } }
@Override public long size() { return queue.size(); }
@Override public boolean isEmpty() { return queue.isEmpty(); }
@Override public void close() { queue.close(); }
@Override public Message poll(long timeout, TimeUnit unit) throws InterruptedException { return queue.poll(timeout, unit); }
@Override public int drain(int batchSize, List<Message> msgList) { return queue.drainTo(msgList, batchSize); }
private void createFile(FileBlockingQueue<String> queue, int count) throws IOException { for (int i = 0; i < count; ++i) { assertTrue(queue.offer("testString" + i)); } }