blob: 21e5c4a758cb86f747ba9a5978433efe6bd5663d [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
25import java.util.ArrayList;
26import java.util.List;
27import java.util.Random;
28
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080029import net.floodlightcontroller.core.IOFMessageListener;
30import net.floodlightcontroller.test.FloodlightTestCase;
31
Jonathan Harta88fd242014-04-03 11:24:54 -070032import org.junit.Test;
33import org.openflow.protocol.OFType;
34
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080035public class MessageDispatcherTest extends FloodlightTestCase {
36
37 IOFMessageListener createLMock(String name) {
38 IOFMessageListener mock = createNiceMock(IOFMessageListener.class);
39 expect(mock.getName()).andReturn(name).anyTimes();
40 return mock;
41 }
Ray Milkey269ffb92014-04-03 14:43:30 -070042
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080043 void addPrereqs(IOFMessageListener mock, String... deps) {
44 for (String dep : deps) {
45 expect(mock.isCallbackOrderingPrereq(OFType.PACKET_IN, dep)).andReturn(true).anyTimes();
46 }
47 }
Ray Milkey269ffb92014-04-03 14:43:30 -070048
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080049 void testOrdering(ArrayList<IOFMessageListener> inputListeners) {
Ray Milkey269ffb92014-04-03 14:43:30 -070050 ListenerDispatcher<OFType, IOFMessageListener> ld =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051 new ListenerDispatcher<OFType, IOFMessageListener>();
Ray Milkey269ffb92014-04-03 14:43:30 -070052
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080053 for (IOFMessageListener l : inputListeners) {
54 ld.addListener(OFType.PACKET_IN, l);
55 }
56 for (IOFMessageListener l : inputListeners) {
57 verify(l);
58 }
Ray Milkey269ffb92014-04-03 14:43:30 -070059
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080060 List<IOFMessageListener> result = ld.getOrderedListeners();
61 System.out.print("Ordering: ");
62 for (IOFMessageListener l : result) {
63 System.out.print(l.getName());
64 System.out.print(",");
65 }
66 System.out.print("\n");
67
68 for (int ind_i = 0; ind_i < result.size(); ind_i++) {
69 IOFMessageListener i = result.get(ind_i);
Ray Milkey269ffb92014-04-03 14:43:30 -070070 for (int ind_j = ind_i + 1; ind_j < result.size(); ind_j++) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080071 IOFMessageListener j = result.get(ind_j);
Ray Milkey269ffb92014-04-03 14:43:30 -070072
73 boolean orderwrong =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080074 (i.isCallbackOrderingPrereq(OFType.PACKET_IN, j.getName()) ||
Ray Milkey269ffb92014-04-03 14:43:30 -070075 j.isCallbackOrderingPostreq(OFType.PACKET_IN, i.getName()));
76 assertFalse("Invalid order: " +
77 ind_i + " (" + i.getName() + ") " +
78 ind_j + " (" + j.getName() + ") ", orderwrong);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080079 }
80 }
81 }
Ray Milkey269ffb92014-04-03 14:43:30 -070082
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080083 void randomTestOrdering(ArrayList<IOFMessageListener> mocks) {
84 Random rand = new Random(0);
Ray Milkey269ffb92014-04-03 14:43:30 -070085 ArrayList<IOFMessageListener> random =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080086 new ArrayList<IOFMessageListener>();
87 random.addAll(mocks);
88 for (int i = 0; i < 20; i++) {
89 for (int j = 0; j < random.size(); j++) {
Ray Milkey269ffb92014-04-03 14:43:30 -070090 int ind = rand.nextInt(mocks.size() - 1);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080091 IOFMessageListener tmp = random.get(j);
92 random.set(j, random.get(ind));
93 random.set(ind, tmp);
94 }
95 testOrdering(random);
96 }
97 }
Ray Milkey269ffb92014-04-03 14:43:30 -070098
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080099 @Test
100 public void testCallbackOrderingSimple() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800102 new ArrayList<IOFMessageListener>();
103 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 }
106 for (int i = 1; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800108 }
109 for (IOFMessageListener l : mocks) {
110 replay(l);
111 }
112 randomTestOrdering(mocks);
113 }
114
115 @Test
116 public void testCallbackOrderingPartial() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800118 new ArrayList<IOFMessageListener>();
119 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800121 }
122 for (int i = 1; i < 5; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800124 }
125 for (int i = 6; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800127 }
128 for (IOFMessageListener l : mocks) {
129 replay(l);
130 }
131 randomTestOrdering(mocks);
132 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700133
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800134
135 @Test
136 public void testCallbackOrderingPartial2() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700137 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800138 new ArrayList<IOFMessageListener>();
139 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800141 }
142 for (int i = 2; i < 5; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700143 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800144 }
145 for (int i = 6; i < 9; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800147 }
148 for (IOFMessageListener l : mocks) {
149 replay(l);
150 }
151 randomTestOrdering(mocks);
152 }
153}