BUG-54 : switched channel pipeline to be protocol specific.
[bgpcep.git] / 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.util.concurrent.DefaultPromise;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import io.netty.util.concurrent.Promise;
18
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;
24
25 import org.junit.After;
26 import org.junit.Test;
27
28 public class ServerTest {
29         public static final int PORT = 18080;
30
31         AbstractDispatcher<?, SimpleSessionListener> clientDispatcher, dispatcher;
32
33         final SimpleSessionListener pce = new SimpleSessionListener();
34
35         SimpleSession session = null;
36
37         ChannelFuture server = null;
38
39         public final InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.5", PORT);
40
41         @Test
42         public void testConnectionEstablished() throws Exception {
43                 final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
44
45                 this.dispatcher = new SimpleDispatcher<>(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
46
47                         @Override
48                         public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
49                                         final Channel channel, final Promise<SimpleSession> promise) {
50                                 p.setSuccess(true);
51                                 return new SimpleSessionNegotiator(promise, channel);
52                         }
53                 }, new ProtocolHandlerFactory<>(new MessageFactory()), new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE));
54
55                 this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
56                         @Override
57                         public SimpleSessionListener getSessionListener() {
58                                 return new SimpleSessionListener();
59                         }
60                 });
61
62                 this.server.get();
63
64                 this.clientDispatcher = new SimpleDispatcher<>(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
65                         @Override
66                         public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
67                                         final Channel channel, final Promise<SimpleSession> promise) {
68                                 return new SimpleSessionNegotiator(promise, channel);
69                         }
70                 }, new ProtocolHandlerFactory<>(new MessageFactory()), new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE));
71
72                 this.session = (SimpleSession) this.clientDispatcher.createClient(this.serverAddress,
73                                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
74                                         @Override
75                                         public SimpleSessionListener getSessionListener() {
76                                                 return new SimpleSessionListener();
77                                         }
78                                 }).get(6, TimeUnit.SECONDS);
79
80                 assertEquals(true, p.get(3, TimeUnit.SECONDS));
81         }
82
83         @Test
84         public void testConnectionFailed() throws IOException, InterruptedException, ExecutionException, TimeoutException {
85                 final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
86
87                 this.dispatcher = new SimpleDispatcher<>(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
88
89                         @Override
90                         public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
91                                         final Channel channel, final Promise<SimpleSession> promise) {
92                                 p.setSuccess(true);
93                                 return new SimpleSessionNegotiator(promise, channel);
94                         }
95                 }, new ProtocolHandlerFactory<>(new MessageFactory()), new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE));
96
97                 this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
98                         @Override
99                         public SimpleSessionListener getSessionListener() {
100                                 return new SimpleSessionListener();
101                         }
102                 });
103
104                 this.server.get();
105
106                 this.clientDispatcher = new SimpleDispatcher<>(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
107                         @Override
108                         public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
109                                         final Channel channel, final Promise<SimpleSession> promise) {
110                                 return new SimpleSessionNegotiator(promise, channel);
111                         }
112                 }, new ProtocolHandlerFactory<>(new MessageFactory()), new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE));
113
114                 this.session = (SimpleSession) this.clientDispatcher.createClient(this.serverAddress,
115                                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
116                                         @Override
117                                         public SimpleSessionListener getSessionListener() {
118                                                 return new SimpleSessionListener();
119                                         }
120                                 }).get(6, TimeUnit.SECONDS);
121
122                 final Future<?> session = this.clientDispatcher.createClient(this.serverAddress,
123                                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000), new SessionListenerFactory<SimpleSessionListener>() {
124                                         @Override
125                                         public SimpleSessionListener getSessionListener() {
126                                                 return new SimpleSessionListener();
127                                         }
128                                 });
129                 assertFalse(session.isSuccess());
130         }
131
132         @After
133         public void tearDown() throws IOException {
134                 this.server.channel().close();
135                 this.dispatcher.close();
136                 this.clientDispatcher.close();
137                 try {
138                         Thread.sleep(100);
139                 } catch (final InterruptedException e) {
140                         throw new RuntimeException(e);
141                 }
142         }
143 }