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