e8587cfd3d449fd542236b3a6d0270eddfa6ec96
[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.assertTrue;
11 import static org.junit.Assert.fail;
12 import io.netty.util.concurrent.GlobalEventExecutor;
13
14 import java.io.IOException;
15 import java.net.ConnectException;
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.ExecutionException;
18
19 import org.junit.After;
20 import org.junit.Test;
21
22 public class ServerTest {
23         private static final int MAX_MSGSIZE = 500;
24         public static final int PORT = 18080;
25
26         DispatcherImpl clientDispatcher, dispatcher;
27
28         final SimpleSessionListener pce = new SimpleSessionListener();
29
30         ProtocolSession session = null;
31
32         ProtocolServer server = null;
33
34         public final InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.5", PORT);
35
36         @Test
37         public void testConnectionEstablished() throws Exception {
38                 this.dispatcher = new DispatcherImpl(new MessageFactory());
39
40                 this.server = this.dispatcher.createServer(this.serverAddress, new ProtocolConnectionFactory() {
41                         @Override
42                         public ProtocolConnection createProtocolConnection(final InetSocketAddress address) {
43
44                                 return new ProtocolConnection() {
45                                         @Override
46                                         public SessionPreferencesChecker getProposalChecker() {
47                                                 return new SimpleSessionProposalChecker();
48                                         }
49
50                                         @Override
51                                         public SessionPreferences getProposal() {
52                                                 return new SimpleSessionPreferences();
53                                         }
54
55                                         @Override
56                                         public InetSocketAddress getPeerAddress() {
57                                                 return address;
58                                         }
59
60                                         @Override
61                                         public SessionListener getListener() {
62                                                 return new SimpleSessionListener();
63                                         }
64                                 };
65                         }
66                 }, new SimpleSessionFactory(MAX_MSGSIZE)).get();
67
68                 this.clientDispatcher = new DispatcherImpl(new MessageFactory());
69
70                 this.session = this.clientDispatcher.createClient(new ProtocolConnection() {
71                         @Override
72                         public SessionPreferencesChecker getProposalChecker() {
73                                 return new SimpleSessionProposalChecker();
74                         }
75
76                         @Override
77                         public SessionPreferences getProposal() {
78                                 return new SimpleSessionPreferences();
79                         }
80
81                         @Override
82                         public InetSocketAddress getPeerAddress() {
83                                 return ServerTest.this.serverAddress;
84                         }
85
86                         @Override
87                         public SessionListener getListener() {
88                                 return ServerTest.this.pce;
89                         }
90                 }, new SimpleSessionFactory(MAX_MSGSIZE), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000)).get();
91
92                 final int maxAttempts = 1000;
93                 int attempts = 0;
94                 synchronized (this.pce) {
95                         while (!this.pce.up && ++attempts < maxAttempts) {
96                                 this.pce.wait(100);
97                         }
98                 }
99                 assertTrue(this.pce.up);
100         }
101
102         public void testConnectionFailed() throws IOException, InterruptedException {
103                 this.dispatcher = new DispatcherImpl(new MessageFactory());
104                 this.clientDispatcher = new DispatcherImpl(new MessageFactory());
105                 final SimpleSessionListener listener = new SimpleSessionListener();
106
107                 try {
108                         final ProtocolSession session = this.clientDispatcher.createClient(new ProtocolConnection() {
109                                 @Override
110                                 public SessionPreferencesChecker getProposalChecker() {
111                                         return new SimpleSessionProposalChecker();
112                                 }
113
114                                 @Override
115                                 public SessionPreferences getProposal() {
116                                         return new SimpleSessionPreferences();
117                                 }
118
119                                 @Override
120                                 public InetSocketAddress getPeerAddress() {
121                                         return ServerTest.this.serverAddress;
122                                 }
123
124                                 @Override
125                                 public SessionListener getListener() {
126                                         return listener;
127                                 }
128                         }, new SimpleSessionFactory(MAX_MSGSIZE), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000)).get();
129
130                         fail("Connection succeeded unexpectedly");
131                 } catch (ExecutionException e) {
132                         assertTrue(listener.failed);
133                         assertTrue(e.getCause() instanceof ConnectException);
134                 }
135         }
136
137         @After
138         public void tearDown() throws IOException {
139                 if (this.server != null)
140                         this.server.close();
141                 this.dispatcher.close();
142                 this.clientDispatcher.close();
143                 try {
144                         Thread.sleep(100);
145                 } catch (final InterruptedException e) {
146                         throw new RuntimeException(e);
147                 }
148         }
149 }