blob: 2fa3b5079fcd2b532411e558aa299e623537aa77 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Madan Jampanif1d425a2014-10-07 09:52:36 -070019package org.onlab.netty;
20
Madan Jampani24f9efb2014-10-24 18:56:23 -070021import java.util.concurrent.Future;
Madan Jampanif1d425a2014-10-07 09:52:36 -070022import java.util.concurrent.TimeUnit;
23
Madan Jampani53e44e62014-10-07 12:39:51 -070024import org.apache.commons.lang3.RandomUtils;
Madan Jampani24f9efb2014-10-24 18:56:23 -070025
Madan Jampani53e44e62014-10-07 12:39:51 -070026import static org.junit.Assert.*;
Madan Jampani24f9efb2014-10-24 18:56:23 -070027
Madan Jampanif1d425a2014-10-07 09:52:36 -070028import org.junit.Test;
29
30/**
31 * Simple ping-pong test that exercises NettyMessagingService.
32 */
33public class PingPongTest {
34
35 @Test
36 public void testPingPong() throws Exception {
37 NettyMessagingService pinger = new NettyMessagingService(8085);
38 NettyMessagingService ponger = new NettyMessagingService(9086);
39 try {
40 pinger.activate();
41 ponger.activate();
Madan Jampanif1d425a2014-10-07 09:52:36 -070042 ponger.registerHandler("echo", new EchoHandler());
Madan Jampani53e44e62014-10-07 12:39:51 -070043 byte[] payload = RandomUtils.nextBytes(100);
Madan Jampani24f9efb2014-10-24 18:56:23 -070044 Future<byte[]> responseFuture = pinger.sendAndReceive(new Endpoint("localhost", 9086), "echo", payload);
45 assertArrayEquals(payload, responseFuture.get(10000, TimeUnit.MILLISECONDS));
Madan Jampanif1d425a2014-10-07 09:52:36 -070046 } finally {
47 pinger.deactivate();
48 ponger.deactivate();
49 }
50 }
51}