@Test public void testInterruptionDuringBlockingOp2() throws InterruptedException { HazelcastInstance hz = createHazelcastInstance(); final ILock lock = hz.getLock("lock"); lock.lock(); assertTrue(lock.isLockedByCurrentThread()); final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean interruptedFlag = new AtomicBoolean(false); final OpThread thread = new OpThread("Lock-Thread", latch, interruptedFlag) { protected void doOp() throws InterruptedException { assertTrue(lock.tryLock(1, TimeUnit.MINUTES)); } }; thread.start(); Thread.sleep(5000); thread.interrupt(); lock.unlock(); assertTrue(latch.await(1, TimeUnit.MINUTES)); if (thread.isInterruptionCaught()) { assertFalse("Thread interrupted flag should not be set!", interruptedFlag.get()); assertFalse("Lock should not be in 'locked' state!", lock.isLocked()); } else { assertTrue("Thread interrupted flag should be set! " + thread, interruptedFlag.get()); assertTrue("Lock should be 'locked' state!", lock.isLocked()); } }
@Test public void testInterruptionDuringBlockingOp1() throws InterruptedException { HazelcastInstance hz = createHazelcastInstance(); final IQueue<Object> q = hz.getQueue("queue"); final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean interruptedFlag = new AtomicBoolean(false); OpThread thread = new OpThread("Queue Thread", latch, interruptedFlag) { protected void doOp() throws InterruptedException { q.poll(1, TimeUnit.MINUTES); } }; thread.start(); Thread.sleep(5000); thread.interrupt(); q.offer("new item!"); assertTrue(latch.await(1, TimeUnit.MINUTES)); if (thread.isInterruptionCaught()) { assertFalse("Thread interrupted flag should not be set!", interruptedFlag.get()); assertFalse("Queue should not be empty!", q.isEmpty()); } else { assertTrue("Thread interrupted flag should be set!", interruptedFlag.get()); assertTrue("Queue should be empty!", q.isEmpty()); } }
private void open(String indexName) { DbgOutput.println(1, "open() " + indexName); cmd.reset(); cmd.cmdType = LibgistCommand.OPEN; cmd.indexName.append(indexName); opThread.dispatchCmd(cmd); }
public void windowClosing(WindowEvent e) { try { Libgist.close(); } catch (LibgistException exc) { // can't do very much at this point System.err.println("Could not close " + exc); } opThread.stopNow(); System.exit(0); }
public MainWindow() { JMenuItem showTextItem, showContentItem, showStatsItem; JMenuItem consoleWinItem, treeWinItem, traceWinItem; // our libgist execution thread opThread = new OpThread(this); Libgist.setBreakHandler(opThread); opThread.start(); cmd = new LibgistCommand(); menuBar = new JMenuBar(); setJMenuBar(menuBar); // create toolbar and console window with text area getContentPane().removeAll(); getContentPane().setLayout(new BorderLayout()); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); // desktop pane + console frame desktop = new JDesktopPane(); desktop.setOpaque(true); desktop.setBackground(Color.lightGray); consoleFrame = new ConsoleWindow(200, desktop); desktop.add(consoleFrame, JLayeredPane.PALETTE_LAYER); // debugging actions for toolbar: // notify opThread of what to do when it hits a breakpoint stepAction = new AbstractAction("Step") { public void actionPerformed(ActionEvent e) { opThread.step(); } }; cancelAction = new AbstractAction("Cancel") { public void actionPerformed(ActionEvent e) { scriptWasCancelled = true; // don't call it after next line! opThread.cancel(); } }; nextAction = new AbstractAction("Next") { public void actionPerformed(ActionEvent e) { opThread.next(); } }; contAction = new AbstractAction("Continue") { public void actionPerformed(ActionEvent e) { opThread.cont(); } }; // opThread is currently executing a script and we want it to stop: // tell libgist directly to suspend execution once opThread is done // with the current operation stopAction = new AbstractAction("Stop") { public void actionPerformed(ActionEvent e) { Libgist.singleStep(); } }; // toolbar JToolBar toolbar = new JToolBar(); toolbar.add(stepAction).setText("Step"); toolbar.add(nextAction).setText("Next"); toolbar.add(contAction).setText("Continue"); toolbar.add(stopAction).setText("Stop"); toolbar.add(cancelAction).setText("Cancel"); panel.add(toolbar, BorderLayout.NORTH); panel.add(desktop, BorderLayout.CENTER); getContentPane().add(panel); createFileMenu(); createOpsMenu(); createDebugMenu(); createTreeStatsMenu(); createAnalysisMenu(); createWindowsMenu(); setGuiState(INITSTATE); // nothing opened yet // addWindowListener(this); // So we do the right thing on window closing. }