Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Jampani | f1d425a | 2014-10-07 09:52:36 -0700 | [diff] [blame] | 19 | package org.onlab.netty; |
| 20 | |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 21 | import java.util.concurrent.Future; |
Madan Jampani | f1d425a | 2014-10-07 09:52:36 -0700 | [diff] [blame] | 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 24 | import org.apache.commons.lang3.RandomUtils; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 25 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.*; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 27 | |
Madan Jampani | f1d425a | 2014-10-07 09:52:36 -0700 | [diff] [blame] | 28 | import org.junit.Test; |
| 29 | |
| 30 | /** |
| 31 | * Simple ping-pong test that exercises NettyMessagingService. |
| 32 | */ |
| 33 | public 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 Jampani | f1d425a | 2014-10-07 09:52:36 -0700 | [diff] [blame] | 42 | ponger.registerHandler("echo", new EchoHandler()); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 43 | byte[] payload = RandomUtils.nextBytes(100); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 44 | Future<byte[]> responseFuture = pinger.sendAndReceive(new Endpoint("localhost", 9086), "echo", payload); |
| 45 | assertArrayEquals(payload, responseFuture.get(10000, TimeUnit.MILLISECONDS)); |
Madan Jampani | f1d425a | 2014-10-07 09:52:36 -0700 | [diff] [blame] | 46 | } finally { |
| 47 | pinger.deactivate(); |
| 48 | ponger.deactivate(); |
| 49 | } |
| 50 | } |
| 51 | } |