public void testSimple() throws Exception { HostMessenger msg1 = new HostMessenger(getConfig()); msg1.start(); HostMessenger msg2 = new HostMessenger(getConfig()); msg2.start(); System.out.println("Waiting for socketjoiners..."); msg1.waitForGroupJoin(2); System.out.println("Finished socket joiner for msg1"); msg2.waitForGroupJoin(2); System.out.println("Finished socket joiner for msg2"); assertEquals(msg1.getHostId(), 0); assertEquals(msg2.getHostId(), 1); Mailbox mb1 = msg1.createMailbox(); Mailbox mb2 = msg2.createMailbox(); long siteId2 = mb2.getHSId(); MsgTest.initWithSize(16); MsgTest mt = new MsgTest(); mt.setValues(); mb1.send(siteId2, mt); MsgTest mt2 = null; while (mt2 == null) { mt2 = (MsgTest) mb2.recv(); } assertTrue(mt2.verify()); // Do it again MsgTest.initWithSize(32); mt = new MsgTest(); mt.setValues(); mb1.send(siteId2, mt); mt2 = null; while (mt2 == null) { mt2 = (MsgTest) mb2.recv(); } assertTrue(mt2.verify()); // Do it a final time with a message that should block on write. // spin on a complete network message here - maybe better to write // the above cases this way too? for (int i = 0; i < 3; ++i) { MsgTest.initWithSize(4280034); mt = new MsgTest(); mt.setValues(); mb1.send(siteId2, mt); mt2 = null; while (mt2 == null) { mt2 = (MsgTest) mb2.recv(); } assertTrue(mt.verify()); } msg1.shutdown(); msg2.shutdown(); }
public void terminate() { tag = 0xdeadbeef; if (!starting.get()) { slotSync.lock(); try { // Check whether termination was already underway, but interrupted and now // restarted. boolean restarted = terminating; terminating = true; // First attempt to terminate the context. if (!restarted) { // First send stop command to sockets so that any blocking calls // can be interrupted. If there are no sockets we can ask reaper // thread to stop. for (SocketBase socket : sockets) { socket.stop(); } if (sockets.isEmpty()) { reaper.stop(); } } } finally { slotSync.unlock(); } // Wait till reaper thread closes all the sockets. Command cmd = termMailbox.recv(-1); if (cmd == null) { throw new IllegalStateException(); } assert (cmd.type() == Command.Type.DONE); slotSync.lock(); try { assert (sockets.isEmpty()); } finally { slotSync.unlock(); } } // Deallocate the resources. try { destroy(); } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void testSingleHost() throws Exception { HostMessenger hm = createHostMessenger(0); Mailbox m1 = hm.createMailbox(); SiteMailbox sm = new SiteMailbox(hm, (-2L << 32)); hm.createMailbox(sm.getHSId(), sm); sm.send(m1.getHSId(), new LocalObjectMessage(null)); m1.send(sm.getHSId(), new LocalObjectMessage(null)); LocalObjectMessage lom = (LocalObjectMessage) m1.recv(); assertEquals(lom.m_sourceHSId, sm.getHSId()); lom = (LocalObjectMessage) sm.recv(); assertEquals(lom.m_sourceHSId, m1.getHSId()); }
public void testMultiMailbox() throws Exception { HostMessenger msg1 = new HostMessenger(getConfig()); msg1.start(); HostMessenger msg2 = new HostMessenger(getConfig()); msg2.start(); HostMessenger msg3 = new HostMessenger(getConfig()); msg3.start(); System.out.println("Waiting for socketjoiners..."); msg1.waitForGroupJoin(3); System.out.println("Finished socket joiner for msg1"); msg2.waitForGroupJoin(3); System.out.println("Finished socket joiner for msg2"); msg3.waitForGroupJoin(3); System.out.println("Finished socket joiner for msg3"); assertTrue(msg1.getHostId() != msg2.getHostId() && msg2.getHostId() != msg3.getHostId()); // assertTrue(msg2.getHostId() == 1); // assertTrue(msg3.getHostId() == 2); Mailbox mb1 = msg1.createMailbox(); Mailbox mb2 = msg2.createMailbox(); Mailbox mb3 = msg3.createMailbox(); Mailbox mb4 = msg3.createMailbox(); Mailbox mb5 = msg1.createMailbox(); long siteId5 = mb5.getHSId(); long siteId2 = mb2.getHSId(); long siteId3 = mb3.getHSId(); long siteId4 = mb4.getHSId(); MsgTest.initWithSize(16); MsgTest mt = new MsgTest(); mt.setValues(); int msgCount = 0; mb1.send(new long[] {siteId2, siteId3, siteId5, siteId4}, mt); long now = System.currentTimeMillis(); MsgTest mt2 = null, mt3 = null, mt4 = null, mt5 = null; // run (for no more than 5s) until all 4 messages have arrived // this code is really weird, but it is more accurate than just // running until you get 4 messages. It actually makes sure they // are the right messages. while (msgCount < 4) { assertTrue((System.currentTimeMillis() - now) < 5000); if (mt2 == null) { mt2 = (MsgTest) mb2.recv(); if (mt2 != null) { assertTrue(mt2.verify()); msgCount++; } } if (mt3 == null) { mt3 = (MsgTest) mb3.recv(); if (mt3 != null) { assertTrue(mt3.verify()); msgCount++; } } if (mt4 == null) { mt4 = (MsgTest) mb4.recv(); if (mt4 != null) { assertTrue(mt4.verify()); msgCount++; } } if (mt5 == null) { mt5 = (MsgTest) mb5.recv(); if (mt5 != null) { assertTrue(mt5.verify()); msgCount++; } } } mb3.send(new long[] {siteId5}, mt); assertEquals(((SiteMailbox) mb2).getWaitingCount(), 0); assertEquals(((SiteMailbox) mb3).getWaitingCount(), 0); assertEquals(((SiteMailbox) mb4).getWaitingCount(), 0); // check that there is a single message for mb5 // again, weird code, but I think it's right (jhugg) int wc = 0; now = System.currentTimeMillis(); while (wc != 1) { assertTrue((System.currentTimeMillis() - now) < 5000); wc = ((SiteMailbox) mb5).getWaitingCount(); if (wc == 0) assertTrue(wc < 2); } msg1.shutdown(); msg2.shutdown(); msg3.shutdown(); }