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 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(); }
@Override public void run() { // create a site int hostId = mySiteId % hostCount; HostMessenger currentMessenger = null; Mailbox[] mbox = new Mailbox[(int) mailboxCount]; System.out.printf("Starting up host: %d, site: %d\n", hostId, mySiteId); hostMessengerLock.lock(); { // get the host if possible currentMessenger = messengers[hostId]; // create the host if needed if (currentMessenger == null) { boolean isPrimary = hostId == 0; if (isPrimary) { System.out.printf( "Host/Site %d/%d is initializing the HostMessenger class.\n", hostId, mySiteId); } System.out.printf("Host/Site %d/%d is creating a new HostMessenger.\n", hostId, mySiteId); HostMessenger.Config config = new HostMessenger.Config(m_portGenerator); final HostMessenger messenger = new HostMessenger(config); currentMessenger = messenger; messengers[hostId] = currentMessenger; new Thread() { @Override public void run() { try { messenger.start(); } catch (Exception e) { e.printStackTrace(); } } }; } else System.out.printf("Host/Site %d/%d found existing HostMessenger.\n", hostId, mySiteId); } hostMessengerLock.unlock(); HostMessenger messenger = currentMessenger; messenger.waitForGroupJoin(hostCount); // create the mailboxes for (int i = 0; i < mailboxCount; i++) { mbox[i] = currentMessenger.createMailbox(); } // claim this site is done sitesDone.incrementAndGet(); System.out.printf("Host/Site %d/%d has joined and created sites.\n", hostId, mySiteId); // spin until all sites are done while (sitesDone.get() < siteCount.get()) {} System.out.printf("Host/Site %d/%d thinks all threads are ready.\n", hostId, mySiteId); // begin loop while (recvCount.get() < msgCount) { // figure out which message to send int msgIndex = sentCount.getAndIncrement(); // send a message if (msgIndex < msgCount) { int siteId = rand.nextInt(siteCount.get()); long mailboxId = rand.nextLong() % mailboxCount; System.out.printf( "Host/Site %d/%d is sending message %d/%d to site/mailbox %d/%d.\n", hostId, mySiteId, msgIndex, msgCount, siteId, mailboxId); MsgTest mt = new MsgTest(); mt.setValues(); (currentMessenger).send((mailboxCount << 32) + siteId, mt); } // try to recv a message for (int i = 0; i < mailboxCount; i++) { MsgTest mt = (MsgTest) mbox[i].recv(); if (mt != null) { int recvCountTemp = recvCount.incrementAndGet(); System.out.printf( "Host/Site %d/%d is receiving message %d/%d.\n", hostId, mySiteId, recvCountTemp, msgCount); assertTrue(mt.verify()); } } } try { messenger.shutdown(); } catch (InterruptedException e1) { e1.printStackTrace(); } }