2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.framework;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.util.concurrent.DefaultPromise;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import io.netty.util.concurrent.Promise;
19 import java.io.IOException;
20 import java.net.InetSocketAddress;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
25 import org.junit.After;
26 import org.junit.Test;
28 public class ServerTest {
29 public static final int PORT = 18080;
31 AbstractDispatcher<?, SimpleSessionListener> clientDispatcher, dispatcher;
33 final SimpleSessionListener pce = new SimpleSessionListener();
35 SimpleSession session = null;
37 ChannelFuture server = null;
39 public final InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.5", PORT);
42 public void testConnectionEstablished() throws Exception {
43 final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
45 this.dispatcher = new SimpleDispatcher<>(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
48 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
49 final Channel channel, final Promise<SimpleSession> promise) {
51 return new SimpleSessionNegotiator(promise, channel);
53 }, new ProtocolHandlerFactory<>(new MessageFactory()), new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE));
55 this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
57 public SimpleSessionListener getSessionListener() {
58 return new SimpleSessionListener();
64 this.clientDispatcher = new SimpleDispatcher<>(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
66 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
67 final Channel channel, final Promise<SimpleSession> promise) {
68 return new SimpleSessionNegotiator(promise, channel);
70 }, new ProtocolHandlerFactory<>(new MessageFactory()), new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE));
72 this.session = (SimpleSession) this.clientDispatcher.createClient(this.serverAddress,
73 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
75 public SimpleSessionListener getSessionListener() {
76 return new SimpleSessionListener();
78 }).get(6, TimeUnit.SECONDS);
80 assertEquals(true, p.get(3, TimeUnit.SECONDS));
84 public void testConnectionFailed() throws IOException, InterruptedException, ExecutionException, TimeoutException {
85 final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
87 this.dispatcher = new SimpleDispatcher<>(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
90 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
91 final Channel channel, final Promise<SimpleSession> promise) {
93 return new SimpleSessionNegotiator(promise, channel);
95 }, new ProtocolHandlerFactory<>(new MessageFactory()), new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE));
97 this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
99 public SimpleSessionListener getSessionListener() {
100 return new SimpleSessionListener();
106 this.clientDispatcher = new SimpleDispatcher<>(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
108 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
109 final Channel channel, final Promise<SimpleSession> promise) {
110 return new SimpleSessionNegotiator(promise, channel);
112 }, new ProtocolHandlerFactory<>(new MessageFactory()), new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE));
114 this.session = (SimpleSession) this.clientDispatcher.createClient(this.serverAddress,
115 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
117 public SimpleSessionListener getSessionListener() {
118 return new SimpleSessionListener();
120 }).get(6, TimeUnit.SECONDS);
122 final Future<?> session = this.clientDispatcher.createClient(this.serverAddress,
123 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
125 public SimpleSessionListener getSessionListener() {
126 return new SimpleSessionListener();
129 assertFalse(session.isSuccess());
133 public void tearDown() throws IOException {
134 this.server.channel().close();
135 this.dispatcher.close();
136 this.clientDispatcher.close();
139 } catch (final InterruptedException e) {
140 throw new RuntimeException(e);