blob: 961568bca8e7d2deee801be84ca8259b05c8cffd [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Madan Jampanif1d425a2014-10-07 09:52:36 -070016package org.onlab.netty;
17
Madan Jampani24f9efb2014-10-24 18:56:23 -070018import java.util.concurrent.Future;
Madan Jampanif1d425a2014-10-07 09:52:36 -070019import java.util.concurrent.TimeUnit;
20
Madan Jampani53e44e62014-10-07 12:39:51 -070021import org.apache.commons.lang3.RandomUtils;
Madan Jampani24f9efb2014-10-24 18:56:23 -070022
Madan Jampani53e44e62014-10-07 12:39:51 -070023import static org.junit.Assert.*;
Madan Jampani24f9efb2014-10-24 18:56:23 -070024
Madan Jampanif1d425a2014-10-07 09:52:36 -070025import org.junit.Test;
26
27/**
28 * Simple ping-pong test that exercises NettyMessagingService.
29 */
30public class PingPongTest {
31
32 @Test
33 public void testPingPong() throws Exception {
34 NettyMessagingService pinger = new NettyMessagingService(8085);
35 NettyMessagingService ponger = new NettyMessagingService(9086);
36 try {
37 pinger.activate();
38 ponger.activate();
Madan Jampanif1d425a2014-10-07 09:52:36 -070039 ponger.registerHandler("echo", new EchoHandler());
Madan Jampani53e44e62014-10-07 12:39:51 -070040 byte[] payload = RandomUtils.nextBytes(100);
Madan Jampani24f9efb2014-10-24 18:56:23 -070041 Future<byte[]> responseFuture = pinger.sendAndReceive(new Endpoint("localhost", 9086), "echo", payload);
42 assertArrayEquals(payload, responseFuture.get(10000, TimeUnit.MILLISECONDS));
Madan Jampanif1d425a2014-10-07 09:52:36 -070043 } finally {
44 pinger.deactivate();
45 ponger.deactivate();
46 }
47 }
48}