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.assertTrue;
12 import static org.junit.Assert.fail;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.util.concurrent.DefaultPromise;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import io.netty.util.concurrent.Promise;
19 import java.io.IOException;
20 import java.net.ConnectException;
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
25 import org.junit.After;
26 import org.junit.Test;
28 public class ServerTest {
29 public static final int PORT = 18080;
31 DispatcherImpl 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 this.dispatcher = new DispatcherImpl();
45 final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
47 this.server = this.dispatcher.createServer(this.serverAddress,
48 new SessionListenerFactory<SimpleSessionListener>() {
50 public SimpleSessionListener getSessionListener() {
51 return new SimpleSessionListener();
53 }, new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
56 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
57 final Channel channel, final Promise<SimpleSession> promise) {
59 return new SimpleSessionNegotiator(promise, channel);
61 }, new MessageFactory());
65 this.clientDispatcher = new DispatcherImpl();
67 this.session = this.clientDispatcher.createClient(serverAddress,
68 new SimpleSessionListener(), new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
70 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
71 final Channel channel, final Promise<SimpleSession> promise) {
72 return new SimpleSessionNegotiator(promise, channel);
74 }, new MessageFactory(), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000)).get();
76 assertEquals(true, p.get(3, TimeUnit.SECONDS));
79 public void testConnectionFailed() throws IOException, InterruptedException {
80 this.dispatcher = new DispatcherImpl();
81 this.clientDispatcher = new DispatcherImpl();
82 final SimpleSessionListener listener = new SimpleSessionListener();
85 this.clientDispatcher.createClient(serverAddress, listener,
86 new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
88 public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
89 final Channel channel, final Promise<SimpleSession> promise) {
90 // TODO Auto-generated method stub
93 }, new MessageFactory(), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000)).get();
95 fail("Connection succeeded unexpectedly");
96 } catch (ExecutionException e) {
97 assertTrue(listener.failed);
98 assertTrue(e.getCause() instanceof ConnectException);
103 public void tearDown() throws IOException {
104 server.channel().close();
105 this.dispatcher.close();
106 this.clientDispatcher.close();
109 } catch (final InterruptedException e) {
110 throw new RuntimeException(e);