RoleChanger { public synchronized void submitRequest(Collection<OFSwitchImpl> switches, Role role) { long deadline = System.nanoTime(); if (deadline - lastSubmitTime < 10 * 1000*1000) deadline = lastSubmitTime + 10 * 1000*1000; ArrayList<OFSwitchImpl> switches_copy = new ArrayList<OFSwitchImpl>(switches); RoleChangeTask req = new RoleChangeTask(switches_copy, role, deadline); pendingTasks.put(req); lastSubmitTime = deadline; } RoleChanger(); synchronized void submitRequest(Collection<OFSwitchImpl> switches, Role role);  }
@Test public void testSubmitRequest() throws Exception { LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); roleChanger.timeout = 500*1000*1000; OFSwitchImpl sw1 = EasyMock.createStrictMock(OFSwitchImpl.class); expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(true); expect(sw1.sendNxRoleRequest(EasyMock.same(Role.MASTER), EasyMock.anyLong())) .andReturn(1); expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(true); expect(sw1.sendNxRoleRequest(EasyMock.same(Role.SLAVE), EasyMock.anyLong())) .andReturn(1); expect(sw1.checkFirstPendingRoleRequestCookie(EasyMock.anyLong())) .andReturn(false); expect(sw1.checkFirstPendingRoleRequestCookie(EasyMock.anyLong())) .andReturn(false); switches.add(sw1); replay(sw1); roleChanger.submitRequest(switches, Role.MASTER); roleChanger.submitRequest(switches, Role.SLAVE); Thread.sleep(100); assertEquals(2, roleChanger.pendingTasks.size()); assertSame(RoleChanger.RoleChangeTask.Type.TIMEOUT, roleChanger.pendingTasks.peek().type); assertNotSame(switches, roleChanger.pendingTasks.peek().switches); Thread.sleep(500); assertEquals(0, roleChanger.pendingTasks.size()); verify(sw1); }