Merge "Add XSQL into default compilation"
[controller.git] / opendaylight / commons / protocol-framework / src / test / java / org / opendaylight / protocol / framework / ServerTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.framework;
9
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;
19
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;
25
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29
30 public class ServerTest {
31     SimpleDispatcher clientDispatcher, dispatcher;
32
33     final SimpleSessionListener pce = new SimpleSessionListener();
34
35     SimpleSession session = null;
36
37     ChannelFuture server = null;
38
39     InetSocketAddress serverAddress;
40     private NioEventLoopGroup eventLoopGroup;
41
42
43     @Before
44     public void setUp() {
45         final int port = 10000 + (int)(10000 * Math.random());
46         serverAddress = new InetSocketAddress("127.0.0.1", port);
47         eventLoopGroup = new NioEventLoopGroup();
48     }
49
50     @Test
51     public void testConnectionEstablished() throws Exception {
52         final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
53
54         this.dispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
55
56             @Override
57             public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
58                     final Channel channel, final Promise<SimpleSession> promise) {
59                 p.setSuccess(true);
60                 return new SimpleSessionNegotiator(promise, channel);
61             }
62         }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
63
64         this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
65             @Override
66             public SimpleSessionListener getSessionListener() {
67                 return new SimpleSessionListener();
68             }
69         });
70
71         this.server.get();
72
73         this.clientDispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
74             @Override
75             public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
76                     final Channel channel, final Promise<SimpleSession> promise) {
77                 return new SimpleSessionNegotiator(promise, channel);
78             }
79         }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
80
81         this.session = this.clientDispatcher.createClient(this.serverAddress,
82                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
83             @Override
84             public SimpleSessionListener getSessionListener() {
85                 return new SimpleSessionListener();
86             }
87         }).get(6, TimeUnit.SECONDS);
88
89         assertEquals(true, p.get(3, TimeUnit.SECONDS));
90     }
91
92     @Test
93     public void testConnectionFailed() throws IOException, InterruptedException, ExecutionException, TimeoutException {
94         final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
95
96         this.dispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
97
98             @Override
99             public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
100                     final Channel channel, final Promise<SimpleSession> promise) {
101                 p.setSuccess(true);
102                 return new SimpleSessionNegotiator(promise, channel);
103             }
104         }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
105
106         this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
107             @Override
108             public SimpleSessionListener getSessionListener() {
109                 return new SimpleSessionListener();
110             }
111         });
112
113         this.server.get();
114
115         this.clientDispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
116             @Override
117             public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
118                     final Channel channel, final Promise<SimpleSession> promise) {
119                 return new SimpleSessionNegotiator(promise, channel);
120             }
121         }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
122
123         this.session = this.clientDispatcher.createClient(this.serverAddress,
124                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
125             @Override
126             public SimpleSessionListener getSessionListener() {
127                 return new SimpleSessionListener();
128             }
129         }).get(6, TimeUnit.SECONDS);
130
131         final Future<?> session = this.clientDispatcher.createClient(this.serverAddress,
132                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
133             @Override
134             public SimpleSessionListener getSessionListener() {
135                 return new SimpleSessionListener();
136             }
137         });
138         assertFalse(session.isSuccess());
139     }
140
141     @After
142     public void tearDown() throws IOException, InterruptedException {
143         this.server.channel().close();
144         this.eventLoopGroup.shutdownGracefully();
145         try {
146             Thread.sleep(500);
147         } catch (final InterruptedException e) {
148             throw new RuntimeException(e);
149         }
150     }
151 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.