@Test public void testRoleChangeTask() { @SuppressWarnings("unchecked") Collection<OFSwitchImpl> switches = EasyMock.createMock(Collection.class); long now = System.nanoTime(); long dt1 = 10 * 1000*1000*1000L; long dt2 = 20 * 1000*1000*1000L; long dt3 = 15 * 1000*1000*1000L; RoleChangeTask t1 = new RoleChangeTask(switches, null, now+dt1); RoleChangeTask t2 = new RoleChangeTask(switches, null, now+dt2); RoleChangeTask t3 = new RoleChangeTask(switches, null, now+dt3); // FIXME: cannot test comparison against self. grrr //assertTrue( t1.compareTo(t1) <= 0 ); assertTrue( t1.compareTo(t2) < 0 ); assertTrue( t1.compareTo(t3) < 0 ); assertTrue( t2.compareTo(t1) > 0 ); //assertTrue( t2.compareTo(t2) <= 0 ); assertTrue( t2.compareTo(t3) > 0 ); }
@Test public void testRoleChangeTask() { @SuppressWarnings("unchecked") Collection<IOFSwitch> switches = EasyMock.createMock(Collection.class); long now = System.nanoTime(); long dt1 = 10 * 1000*1000*1000L; long dt2 = 20 * 1000*1000*1000L; long dt3 = 15 * 1000*1000*1000L; RoleChangeTask t1 = new RoleChangeTask(switches, null, now+dt1); RoleChangeTask t2 = new RoleChangeTask(switches, null, now+dt2); RoleChangeTask t3 = new RoleChangeTask(switches, null, now+dt3); // FIXME: cannot test comparison against self. grrr //assertTrue( t1.compareTo(t1) <= 0 ); assertTrue( t1.compareTo(t2) < 0 ); assertTrue( t1.compareTo(t3) < 0 ); assertTrue( t2.compareTo(t1) > 0 ); //assertTrue( t2.compareTo(t2) <= 0 ); assertTrue( t2.compareTo(t3) > 0 ); }
@Test public void testSubmitRequest() throws Exception { LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); roleChanger.timeout = 500*1000*1000; // 500 ms // a switch that supports role requests OFSwitchImpl sw1 = EasyMock.createStrictMock(OFSwitchImpl.class); // No support for NX_ROLE 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); // The following calls happen for timeout handling: 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); // Wait until role request has been sent. // TODO: need to get rid of this sleep somehow Thread.sleep(100); // Now there should be exactly one timeout task pending assertEquals(2, roleChanger.pendingTasks.size()); // Make sure it's indeed a timeout task assertSame(RoleChanger.RoleChangeTask.Type.TIMEOUT, roleChanger.pendingTasks.peek().type); // Check that RoleChanger indeed made a copy of switches collection assertNotSame(switches, roleChanger.pendingTasks.peek().switches); // Wait until the timeout triggers // TODO: get rid of this sleep too. Thread.sleep(500); assertEquals(0, roleChanger.pendingTasks.size()); verify(sw1); }
@Test public void testErrorEPERM() throws Exception { // Check behavior with a BAD_REQUEST/EPERM error // Ensure controller attempts to reset switch role. OFChannelState state = new OFChannelState(); state.hsState = HandshakeState.READY; Controller.OFChannelHandler chdlr = controller.new OFChannelHandler(state); OFError error = new OFError(); error.setErrorType(OFErrorType.OFPET_BAD_REQUEST); error.setErrorCode(OFBadRequestCode.OFPBRC_EPERM); IOFSwitch sw = createMock(IOFSwitch.class); chdlr.sw = sw; controller.activeSwitches.put(1L, sw); // prepare the switch and lock expectations Lock lock = createNiceMock(Lock.class); expect(sw.getListenerReadLock()).andReturn(lock).anyTimes(); expect(sw.isConnected()).andReturn(true).anyTimes(); expect(sw.getHARole()).andReturn(Role.MASTER).anyTimes(); expect(sw.getId()).andReturn(1L).anyTimes(); // Make sure controller attempts to reset switch master expect(sw.getAttribute("supportsNxRole")).andReturn(true).anyTimes(); expect(sw.getNextTransactionId()).andReturn(0).anyTimes(); sw.write(EasyMock.<List<OFMessage>> anyObject(), (FloodlightContext)anyObject()); // test replay(sw, lock); chdlr.processOFMessage(error); DelayQueue<RoleChangeTask> pendingTasks = controller.roleChanger.pendingTasks; synchronized (pendingTasks) { RoleChangeTask t; while ((t = pendingTasks.peek()) == null || RoleChanger.RoleChangeTask.Type.TIMEOUT != t.type) { pendingTasks.wait(); } } // Now there should be exactly one timeout task pending assertEquals(1, pendingTasks.size()); }
@SuppressWarnings("unchecked") @Test public void testSubmitRequest() throws Exception { LinkedList<IOFSwitch> switches = new LinkedList<IOFSwitch>(); roleChanger.timeout = 100*1000*1000; // 100 ms // a switch that supports role requests IOFSwitch sw1 = EasyMock.createStrictMock(IOFSwitch.class); // Support for NX_ROLE expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(true); expect(sw1.getNextTransactionId()).andReturn(1); sw1.write((List<OFMessage>)EasyMock.anyObject(), (FloodlightContext)EasyMock.anyObject()); // Second request expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(true); expect(sw1.getNextTransactionId()).andReturn(2); sw1.write((List<OFMessage>)EasyMock.anyObject(), (FloodlightContext)EasyMock.anyObject()); expect(sw1.getAttribute(IOFSwitch.SWITCH_DESCRIPTION_DATA)) .andReturn(null); expect(sw1.getHARole()).andReturn(null); sw1.setHARole(Role.MASTER, false); expect(sw1.getAttribute(IOFSwitch.SWITCH_DESCRIPTION_DATA)) .andReturn(null); expect(sw1.getHARole()).andReturn(Role.MASTER); sw1.setHARole(Role.SLAVE, false); // Disconnect on timing out SLAVE request sw1.disconnectOutputStream(); switches.add(sw1); // Add to switch when timing out the MASTER request controller.addSwitch(sw1, true); replay(sw1, controller); roleChanger.submitRequest(switches, Role.MASTER); roleChanger.submitRequest(switches, Role.SLAVE); // Wait until role request has been sent. synchronized (roleChanger.pendingTasks) { while (RoleChanger.RoleChangeTask.Type.TIMEOUT != roleChanger.pendingTasks.peek().type) { roleChanger.pendingTasks.wait(); } } // Now there should be exactly one timeout task pending for each request assertEquals(2, roleChanger.pendingTasks.size()); // Check that RoleChanger indeed made a copy of switches collection assertNotSame(switches, roleChanger.pendingTasks.peek().switches); // Wait until the timeout triggers synchronized (roleChanger.pendingTasks) { while (roleChanger.pendingTasks.size() != 0) { roleChanger.pendingTasks.wait(); } } verify(sw1, controller); }