2 * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.connection;
10 import io.netty.channel.ChannelHandler;
11 import io.netty.channel.ChannelHandlerContext;
12 import io.netty.channel.ChannelOutboundHandlerAdapter;
13 import io.netty.channel.ChannelPromise;
14 import io.netty.channel.embedded.EmbeddedChannel;
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.TimeUnit;
19 import junit.framework.Assert;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
31 import com.google.common.cache.Cache;
32 import com.google.common.cache.CacheBuilder;
33 import com.google.common.cache.RemovalListener;
34 import com.google.common.cache.RemovalNotification;
40 public class ChannelOutboundQueue02Test {
41 private static int counter;
42 private static final int RPC_RESPONSE_EXPIRATION = 1;
43 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
44 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
46 public void onRemoval(
47 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
48 notification.getValue().discard();
52 @Mock EchoInput echoInput;
53 @Mock BarrierInput barrierInput;
54 @Mock EchoReplyInput echoReplyInput;
55 @Mock ExperimenterInput experimenterInput;
56 private ConnectionAdapterImpl adapter;
57 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
63 MockitoAnnotations.initMocks(this);
66 * Disconnect adapter after each test
69 public void tierDown(){
70 if (adapter != null && adapter.isAlive()) {
76 * Test write to closed / opened channel
80 public void test01() throws Exception {
81 EmbeddedChannel ec = new EmbeddedChannel(new EmbededChannelHandler());
82 adapter = new ConnectionAdapterImpl(ec,InetSocketAddress.createUnresolved("localhost", 9876));
83 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
84 .removalListener(REMOVAL_LISTENER).build();
85 adapter.setResponseCache(cache);
86 ChannelOutboundQueue cq = (ChannelOutboundQueue) ec.pipeline().last();
88 adapter.barrier(barrierInput);
89 adapter.echo(echoInput);
90 cq.channelInactive(ec.pipeline().lastContext());
92 Assert.assertEquals("Wrong - ChannelOutboundHandlerAdapter.write was invoked on closed channel",0, counter);
93 cq.channelActive(ec.pipeline().lastContext());
95 adapter.barrier(barrierInput);
96 adapter.experimenter(experimenterInput);
98 Assert.assertEquals("Wrong - ChannelOutboundHandlerAdapter.write has not been invoked on opened channel",2, counter);
102 * Test write to read only / writable channel
105 public void test02(){
106 ChangeWritableEmbededChannel ec = new ChangeWritableEmbededChannel(new EmbededChannelHandler());
107 adapter = new ConnectionAdapterImpl(ec,InetSocketAddress.createUnresolved("localhost", 9876));
108 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
109 .removalListener(REMOVAL_LISTENER).build();
110 adapter.setResponseCache(cache);
113 adapter.barrier(barrierInput);
114 adapter.echo(echoInput);
115 ec.runPendingTasks();
116 Assert.assertEquals("Wrong - write to readonly channel",0, counter);
118 adapter.echoReply(echoReplyInput);
119 adapter.echo(echoInput);
120 ec.runPendingTasks();
121 Assert.assertEquals("Wrong - write to writtable channel",4, counter);
125 * Channel Handler for testing
129 private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
131 public void write(ChannelHandlerContext ctx, Object msg,
132 ChannelPromise promise) throws Exception {
133 if(msg instanceof MessageListenerWrapper){
140 * Class for testing - channel can change state to read only or writable
144 private class ChangeWritableEmbededChannel extends EmbeddedChannel {
145 private boolean isWrittable;
146 public ChangeWritableEmbededChannel(ChannelHandler channelHandler){
147 super(channelHandler);
152 public boolean isWritable() {
156 public void setWritable(){
160 public void setReadOnly(){