public static MockSnmpAgent createAgentAndRun(URL moFile, String bindAddress) throws InterruptedException { setupLogging(); try { InputStream in = moFile.openStream(); if (in == null) { throw new IllegalArgumentException( "could not get InputStream mock object resource; does it exist? Resource: " + moFile); } in.close(); } catch (IOException e) { throw new RuntimeException( "Got IOException while checking for existence of mock object file: " + e, e); } final MockSnmpAgent agent = new MockSnmpAgent(new File("/dev/null"), moFile, bindAddress); Thread thread = new Thread(agent, agent.getClass().getSimpleName() + '-' + agent.hashCode()); thread.start(); try { while (!agent.isRunning() && thread.isAlive()) { Thread.sleep(10); } } catch (final InterruptedException e) { s_log.warn("MockSnmpAgent: Agent interrupted while starting: " + e.getLocalizedMessage()); thread.interrupt(); agent.shutDownAndWait(); throw e; } if (!thread.isAlive()) { agent.m_running.set(false); agent.m_stopped.set(true); throw new IllegalStateException( "MockSnmpAgent: agent failed to start on address " + bindAddress, agent.m_failure.get()); } if (System.getProperty(PROPERTY_SLEEP_ON_CREATE) != null) { long sleep = Long.parseLong(System.getProperty(PROPERTY_SLEEP_ON_CREATE)); Thread.sleep(sleep); } return agent; }
@After public void tearDown() throws Exception { if (m_agent != null) { m_agent.shutDownAndWait(); } }