77b9e0569c2b14c802288a554e6ba0e75bb66da1
[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
13 import java.io.IOException;
14 import java.net.ConnectException;
15 import java.net.InetSocketAddress;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
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         @Test
35         public void testConnectionEstablished() throws Exception {
36                 this.dispatcher = new DispatcherImpl(new MessageFactory());
37
38                 final InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.3", PORT);
39
40                 this.server = this.dispatcher.createServer(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));
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 new InetSocketAddress("127.0.0.3", PORT);
84                         }
85
86                         @Override
87                         public SessionListener getListener() {
88                                 return ServerTest.this.pce;
89                         }
90                 }, new SimpleSessionFactory(MAX_MSGSIZE)).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         @Test
103         public void testConnectionFailed() throws IOException, InterruptedException, ExecutionException {
104                 this.dispatcher = new DispatcherImpl(new MessageFactory());
105                 this.clientDispatcher = new DispatcherImpl(new MessageFactory());
106                 final SimpleSessionListener listener = new SimpleSessionListener();
107
108                 final Future<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 new InetSocketAddress("127.0.0.5", PORT);
122                         }
123
124                         @Override
125                         public SessionListener getListener() {
126                                 return listener;
127                         }
128                 }, new SimpleSessionFactory(MAX_MSGSIZE));
129                 try {
130                         session.get();
131                         fail("Exception should have occurred.");
132                 } catch (final ExecutionException e) {
133                         listener.failed = true;
134                         assertTrue(e.getCause() instanceof ConnectException);
135                 }
136                 final int maxAttempts = 100;
137                 int attempts = 0;
138                 synchronized (listener) {
139                         while (!listener.failed && ++attempts < maxAttempts) {
140                                 listener.wait(100);
141                         }
142                 }
143                 assertTrue(listener.failed);
144         }
145
146         @After
147         public void tearDown() throws IOException {
148                 this.dispatcher.onSessionClosed(this.session);
149                 if (this.server != null)
150                         this.server.close();
151                 // this.dispatcher.stop();
152                 // this.clientDispatcher.stop();
153                 try {
154                         Thread.sleep(100);
155                 } catch (final InterruptedException e) {
156                         throw new RuntimeException(e);
157                 }
158         }
159 }