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;
12 import java.io.IOException;
13 import java.net.InetSocketAddress;
14 import java.util.concurrent.ExecutionException;
16 import org.junit.After;
17 import org.junit.Test;
19 public class ServerTest {
20 private static final int MAX_MSGSIZE = 500;
21 public static final int PORT = 18080;
23 DispatcherImpl clientDispatcher, dispatcher;
25 final SimpleSessionListener pce = new SimpleSessionListener();
27 ProtocolSession session = null;
29 ProtocolServer server = null;
31 public final InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.5", PORT);
34 public void testConnectionEstablished() throws Exception {
35 this.dispatcher = new DispatcherImpl(new MessageFactory());
37 this.server = this.dispatcher.createServer(this.serverAddress, new ProtocolConnectionFactory() {
39 public ProtocolConnection createProtocolConnection(final InetSocketAddress address) {
41 return new ProtocolConnection() {
43 public SessionPreferencesChecker getProposalChecker() {
44 return new SimpleSessionProposalChecker();
48 public SessionPreferences getProposal() {
49 return new SimpleSessionPreferences();
53 public InetSocketAddress getPeerAddress() {
58 public SessionListener getListener() {
59 return new SimpleSessionListener();
63 }, new SimpleSessionFactory(MAX_MSGSIZE));
65 this.clientDispatcher = new DispatcherImpl(new MessageFactory());
67 this.session = this.clientDispatcher.createClient(new ProtocolConnection() {
69 public SessionPreferencesChecker getProposalChecker() {
70 return new SimpleSessionProposalChecker();
74 public SessionPreferences getProposal() {
75 return new SimpleSessionPreferences();
79 public InetSocketAddress getPeerAddress() {
80 return ServerTest.this.serverAddress;
84 public SessionListener getListener() {
85 return ServerTest.this.pce;
87 }, new SimpleSessionFactory(MAX_MSGSIZE));
89 final int maxAttempts = 1000;
91 synchronized (this.pce) {
92 while (!this.pce.up && ++attempts < maxAttempts) {
96 assertTrue(this.pce.up);
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();
105 final ProtocolSession session = this.clientDispatcher.createClient(new ProtocolConnection() {
107 public SessionPreferencesChecker getProposalChecker() {
108 return new SimpleSessionProposalChecker();
112 public SessionPreferences getProposal() {
113 return new SimpleSessionPreferences();
117 public InetSocketAddress getPeerAddress() {
118 return ServerTest.this.serverAddress;
122 public SessionListener getListener() {
125 }, new SimpleSessionFactory(MAX_MSGSIZE));
127 listener.failed = true;
128 final int maxAttempts = 100;
130 synchronized (listener) {
131 while (!listener.failed && ++attempts < maxAttempts) {
135 assertTrue(listener.failed);
139 public void tearDown() throws IOException {
140 if (this.server != null)
142 this.dispatcher.close();
143 this.clientDispatcher.close();
146 } catch (final InterruptedException e) {
147 throw new RuntimeException(e);