1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.openflow.lib;
4 import io.netty.channel.Channel;
5 import io.netty.util.concurrent.Future;
7 import java.util.ArrayList;
8 import java.util.Iterator;
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.TimeUnit;
13 import org.junit.After;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.openflow.lib.clients.SimpleClient;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
23 * @author michal.polkorab
25 public class TCPHandlerTest {
27 /** Name of file in which OpenFLow protocol messages are stored in binary format */
28 private static final String OF_BINARY_MESSAGE_INPUT_TXT = "OFBinaryMessageInput.txt";
30 protected static final Logger LOGGER = LoggerFactory
31 .getLogger(TCPHandlerTest.class);
33 private static final long CONNECTION_TIMEOUT = 2000;
36 protected String address;
37 protected TcpHandler tcphandler;
40 * @throws InterruptedException
41 * @throws ExecutionException
44 public void setUp() throws InterruptedException, ExecutionException {
45 tcphandler = new TcpHandler(0);
47 tcphandler.getIsOnlineFuture().get();
48 port = tcphandler.getPort();
49 address = tcphandler.getAddress();
53 * stop {@link TcpHandler}
56 public void tearDown() {
57 tcphandler.shutdown();
61 * Test of connections in {@link TcpHandler} - accepting connection of 1
63 * @throws InterruptedException
64 * @throws ExecutionException
67 public void testConnectOneClient() throws InterruptedException, ExecutionException {
68 int amountOfCLients = 1;
69 createAndStartClient(amountOfCLients);
70 int actualConnections = tcphandler.getNumberOfConnections();
71 Assert.assertEquals(amountOfCLients, actualConnections);
72 PublishingChannelInitializer channelInitializer = tcphandler.getChannelInitializer();
73 for (Iterator<Channel> iterator = channelInitializer.getConnectionIterator(); iterator.hasNext();) {
74 Channel channel = iterator.next();
76 channel.close().sync();
77 } catch (InterruptedException e) {
78 LOGGER.error(e.getMessage(), e);
81 actualConnections = tcphandler.getNumberOfConnections();
82 Assert.assertEquals(0, actualConnections);
86 * Test of connections in {@link TcpHandler} - accepting connection of 10
88 * @throws InterruptedException
89 * @throws ExecutionException
92 public void testConnectTenClients() throws InterruptedException, ExecutionException {
93 int amountOfCLients = 10;
94 createAndStartClient(amountOfCLients);
95 int actualConnections = tcphandler.getNumberOfConnections();
96 Assert.assertEquals(amountOfCLients, actualConnections);
97 PublishingChannelInitializer channelInitializer = tcphandler.getChannelInitializer();
98 for (Iterator<Channel> iterator = channelInitializer.getConnectionIterator(); iterator.hasNext();) {
99 Channel channel = iterator.next();
101 channel.close().sync();
102 } catch (InterruptedException e) {
103 LOGGER.error(e.getMessage(), e);
106 actualConnections = tcphandler.getNumberOfConnections();
107 Assert.assertEquals(0, actualConnections);
111 * Test of disconnecting in {@link TcpHandler} - shutting down connection of 10
113 * @throws InterruptedException
114 * @throws ExecutionException
117 public void testDisconnectTenClients() throws InterruptedException, ExecutionException {
118 int amountOfCLients = 10;
119 List<SimpleClient> clients = createAndStartClient(amountOfCLients);
120 int actualConnections = tcphandler.getNumberOfConnections();
121 Assert.assertEquals(amountOfCLients, actualConnections);
123 disconnectClients(clients);
125 actualConnections = tcphandler.getNumberOfConnections();
126 Assert.assertEquals(0, actualConnections);
130 * @param amountOfCLients
131 * @return new clients up and running
132 * @throws InterruptedException
133 * @throws ExecutionException
135 private List<SimpleClient> createAndStartClient(int amountOfCLients)
136 throws InterruptedException, ExecutionException {
137 List<SimpleClient> clientsHorde = new ArrayList<>();
138 for (int i = 0; i < amountOfCLients; i++) {
139 SimpleClient sc = new SimpleClient(address, port, getClass().getResourceAsStream(
140 OF_BINARY_MESSAGE_INPUT_TXT));
141 sc.setSecuredClient(true);
142 clientsHorde.add(sc);
145 for (SimpleClient sc : clientsHorde) {
147 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
148 } catch (Exception e) {
149 LOGGER.error(e.getMessage(), e);
150 throw new ExecutionException(e);
157 * Test of disconnecting in {@link TcpHandler} - shutting down connection of 1
159 * @throws InterruptedException
160 * @throws ExecutionException
163 public void testDisconnectOneClient() throws InterruptedException, ExecutionException {
164 int amountOfCLients = 1;
165 List<SimpleClient> clients = createAndStartClient(amountOfCLients);
166 int actualConnections = tcphandler.getNumberOfConnections();
167 Assert.assertEquals(amountOfCLients, actualConnections);
168 disconnectClients(clients);
169 actualConnections = tcphandler.getNumberOfConnections();
170 Assert.assertEquals(0, actualConnections);
175 * @throws InterruptedException
177 private static void disconnectClients(List<SimpleClient> clients) throws InterruptedException {
178 List<Future<?>> disconnectFutureBag = new ArrayList<>();
179 for (SimpleClient simpleClient : clients) {
180 disconnectFutureBag.add(simpleClient.disconnect());
182 for (Future<?> toBeDisconnected : disconnectFutureBag) {
183 toBeDisconnected.sync();