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.channel.nio.NioEventLoopGroup;
15 import io.netty.util.concurrent.DefaultPromise;
16 import io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.GlobalEventExecutor;
18 import io.netty.util.concurrent.Promise;
20 import java.io.IOException;
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
30 public class ServerTest {
31 SimpleDispatcher clientDispatcher, dispatcher;
33 final SimpleSessionListener pce = new SimpleSessionListener();
35 SimpleSession session = null;
37 ChannelFuture server = null;
39 InetSocketAddress serverAddress;
40 private NioEventLoopGroup eventLoopGroup;
45 final int port = 10000 + (int)(10000 * Math.random());
46 serverAddress = new InetSocketAddress("127.0.0.1", port);
47 eventLoopGroup = new NioEventLoopGroup();
51 public void testConnectionEstablished() throws Exception {
52 final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
54 this.dispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
57 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
58 final Channel channel, final Promise<SimpleSession> promise) {
60 return new SimpleSessionNegotiator(promise, channel);
62 }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
64 this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
66 public SimpleSessionListener getSessionListener() {
67 return new SimpleSessionListener();
73 this.clientDispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
75 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
76 final Channel channel, final Promise<SimpleSession> promise) {
77 return new SimpleSessionNegotiator(promise, channel);
79 }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
81 this.session = this.clientDispatcher.createClient(this.serverAddress,
82 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
84 public SimpleSessionListener getSessionListener() {
85 return new SimpleSessionListener();
87 }).get(6, TimeUnit.SECONDS);
89 assertEquals(true, p.get(3, TimeUnit.SECONDS));
93 public void testConnectionFailed() throws IOException, InterruptedException, ExecutionException, TimeoutException {
94 final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
96 this.dispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
99 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
100 final Channel channel, final Promise<SimpleSession> promise) {
102 return new SimpleSessionNegotiator(promise, channel);
104 }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
106 this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
108 public SimpleSessionListener getSessionListener() {
109 return new SimpleSessionListener();
115 this.clientDispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
117 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
118 final Channel channel, final Promise<SimpleSession> promise) {
119 return new SimpleSessionNegotiator(promise, channel);
121 }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
123 this.session = this.clientDispatcher.createClient(this.serverAddress,
124 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
126 public SimpleSessionListener getSessionListener() {
127 return new SimpleSessionListener();
129 }).get(6, TimeUnit.SECONDS);
131 final Future<?> session = this.clientDispatcher.createClient(this.serverAddress,
132 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
134 public SimpleSessionListener getSessionListener() {
135 return new SimpleSessionListener();
138 assertFalse(session.isSuccess());
142 public void tearDown() throws IOException, InterruptedException {
143 this.server.channel().close();
144 this.eventLoopGroup.shutdownGracefully();
147 } catch (final InterruptedException e) {
148 throw new RuntimeException(e);