blob: 7d72780a93348c843a56aa8dfb3b2289792b77c7 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
18package net.floodlightcontroller.core.util;
19
20import static org.easymock.EasyMock.createNiceMock;
21import static org.easymock.EasyMock.expect;
22import static org.easymock.EasyMock.replay;
23import static org.easymock.EasyMock.verify;
24
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070025import java.io.PrintWriter;
26import java.io.StringWriter;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027import java.util.ArrayList;
28import java.util.List;
29import java.util.Random;
30
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031import net.floodlightcontroller.core.IOFMessageListener;
32import net.floodlightcontroller.test.FloodlightTestCase;
33
Jonathan Harta88fd242014-04-03 11:24:54 -070034import org.junit.Test;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070035import org.projectfloodlight.openflow.protocol.OFType;
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
Jonathan Harta88fd242014-04-03 11:24:54 -070038
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039public class MessageDispatcherTest extends FloodlightTestCase {
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070040 private static final Logger log = LoggerFactory.getLogger(MessageDispatcherTest.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041
42 IOFMessageListener createLMock(String name) {
43 IOFMessageListener mock = createNiceMock(IOFMessageListener.class);
44 expect(mock.getName()).andReturn(name).anyTimes();
45 return mock;
46 }
Ray Milkey269ffb92014-04-03 14:43:30 -070047
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048 void addPrereqs(IOFMessageListener mock, String... deps) {
49 for (String dep : deps) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070050 expect(mock.isCallbackOrderingPrereq(OFType.PACKET_IN, dep)).andReturn(true)
51 .anyTimes();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052 }
53 }
Ray Milkey269ffb92014-04-03 14:43:30 -070054
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080055 void testOrdering(ArrayList<IOFMessageListener> inputListeners) {
Ray Milkey269ffb92014-04-03 14:43:30 -070056 ListenerDispatcher<OFType, IOFMessageListener> ld =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080057 new ListenerDispatcher<OFType, IOFMessageListener>();
Ray Milkey269ffb92014-04-03 14:43:30 -070058
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080059 for (IOFMessageListener l : inputListeners) {
60 ld.addListener(OFType.PACKET_IN, l);
61 }
62 for (IOFMessageListener l : inputListeners) {
63 verify(l);
64 }
Ray Milkey269ffb92014-04-03 14:43:30 -070065
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080066 List<IOFMessageListener> result = ld.getOrderedListeners();
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070067 StringWriter sw = new StringWriter();
68 PrintWriter out = new PrintWriter(sw);
69
70 out.print("Ordering: ");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080071 for (IOFMessageListener l : result) {
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070072 out.print(l.getName());
73 out.print(",");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080074 }
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070075 out.print("\n");
76 log.debug(sw.toString());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080077
78 for (int ind_i = 0; ind_i < result.size(); ind_i++) {
79 IOFMessageListener i = result.get(ind_i);
Ray Milkey269ffb92014-04-03 14:43:30 -070080 for (int ind_j = ind_i + 1; ind_j < result.size(); ind_j++) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080081 IOFMessageListener j = result.get(ind_j);
Ray Milkey269ffb92014-04-03 14:43:30 -070082
83 boolean orderwrong =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080084 (i.isCallbackOrderingPrereq(OFType.PACKET_IN, j.getName()) ||
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070085 j.isCallbackOrderingPostreq(OFType.PACKET_IN, i.getName()));
Ray Milkey269ffb92014-04-03 14:43:30 -070086 assertFalse("Invalid order: " +
87 ind_i + " (" + i.getName() + ") " +
88 ind_j + " (" + j.getName() + ") ", orderwrong);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080089 }
90 }
91 }
Ray Milkey269ffb92014-04-03 14:43:30 -070092
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080093 void randomTestOrdering(ArrayList<IOFMessageListener> mocks) {
94 Random rand = new Random(0);
Ray Milkey269ffb92014-04-03 14:43:30 -070095 ArrayList<IOFMessageListener> random =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080096 new ArrayList<IOFMessageListener>();
97 random.addAll(mocks);
98 for (int i = 0; i < 20; i++) {
99 for (int j = 0; j < random.size(); j++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 int ind = rand.nextInt(mocks.size() - 1);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800101 IOFMessageListener tmp = random.get(j);
102 random.set(j, random.get(ind));
103 random.set(ind, tmp);
104 }
105 testOrdering(random);
106 }
107 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700108
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800109 @Test
110 public void testCallbackOrderingSimple() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800112 new ArrayList<IOFMessageListener>();
113 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800115 }
116 for (int i = 1; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800118 }
119 for (IOFMessageListener l : mocks) {
120 replay(l);
121 }
122 randomTestOrdering(mocks);
123 }
124
125 @Test
126 public void testCallbackOrderingPartial() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700127 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800128 new ArrayList<IOFMessageListener>();
129 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800131 }
132 for (int i = 1; i < 5; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800134 }
135 for (int i = 6; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700136 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800137 }
138 for (IOFMessageListener l : mocks) {
139 replay(l);
140 }
141 randomTestOrdering(mocks);
142 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700143
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800144 @Test
145 public void testCallbackOrderingPartial2() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800147 new ArrayList<IOFMessageListener>();
148 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700149 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800150 }
151 for (int i = 2; i < 5; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800153 }
154 for (int i = 6; i < 9; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800156 }
157 for (IOFMessageListener l : mocks) {
158 replay(l);
159 }
160 randomTestOrdering(mocks);
161 }
162}