2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.framework;
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
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;
19 import org.junit.After;
20 import org.junit.Test;
22 public class ServerTest {
23 private static final int MAX_MSGSIZE = 500;
24 public static final int PORT = 18080;
26 DispatcherImpl clientDispatcher, dispatcher;
28 final SimpleSessionListener pce = new SimpleSessionListener();
30 ProtocolSession session = null;
32 ProtocolServer server = null;
35 public void testConnectionEstablished() throws Exception {
36 this.dispatcher = new DispatcherImpl(new MessageFactory());
38 final InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.3", PORT);
40 this.server = this.dispatcher.createServer(serverAddress, new ProtocolConnectionFactory() {
42 public ProtocolConnection createProtocolConnection(final InetSocketAddress address) {
44 return new ProtocolConnection() {
46 public SessionPreferencesChecker getProposalChecker() {
47 return new SimpleSessionProposalChecker();
51 public SessionPreferences getProposal() {
52 return new SimpleSessionPreferences();
56 public InetSocketAddress getPeerAddress() {
61 public SessionListener getListener() {
62 return new SimpleSessionListener();
66 }, new SimpleSessionFactory(MAX_MSGSIZE));
68 this.clientDispatcher = new DispatcherImpl(new MessageFactory());
70 this.session = this.clientDispatcher.createClient(new ProtocolConnection() {
72 public SessionPreferencesChecker getProposalChecker() {
73 return new SimpleSessionProposalChecker();
77 public SessionPreferences getProposal() {
78 return new SimpleSessionPreferences();
82 public InetSocketAddress getPeerAddress() {
83 return new InetSocketAddress("127.0.0.3", PORT);
87 public SessionListener getListener() {
88 return ServerTest.this.pce;
90 }, new SimpleSessionFactory(MAX_MSGSIZE)).get();
92 final int maxAttempts = 1000;
94 synchronized (this.pce) {
95 while (!this.pce.up && ++attempts < maxAttempts) {
99 assertTrue(this.pce.up);
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();
108 final Future<ProtocolSession> session = this.clientDispatcher.createClient(new ProtocolConnection() {
110 public SessionPreferencesChecker getProposalChecker() {
111 return new SimpleSessionProposalChecker();
115 public SessionPreferences getProposal() {
116 return new SimpleSessionPreferences();
120 public InetSocketAddress getPeerAddress() {
121 return new InetSocketAddress("127.0.0.5", PORT);
125 public SessionListener getListener() {
128 }, new SimpleSessionFactory(MAX_MSGSIZE));
131 fail("Exception should have occurred.");
132 } catch (final ExecutionException e) {
133 listener.failed = true;
134 assertTrue(e.getCause() instanceof ConnectException);
136 final int maxAttempts = 100;
138 synchronized (listener) {
139 while (!listener.failed && ++attempts < maxAttempts) {
143 assertTrue(listener.failed);
147 public void tearDown() throws IOException {
148 this.dispatcher.onSessionClosed(this.session);
149 if (this.server != null)
151 // this.dispatcher.stop();
152 // this.clientDispatcher.stop();
155 } catch (final InterruptedException e) {
156 throw new RuntimeException(e);