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.core.connection;
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.RemovalListener;
13 import com.google.common.cache.RemovalNotification;
14 import io.netty.channel.ChannelHandler;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.ChannelOutboundHandlerAdapter;
17 import io.netty.channel.ChannelPromise;
18 import io.netty.channel.embedded.EmbeddedChannel;
19 import java.net.InetSocketAddress;
20 import java.util.concurrent.TimeUnit;
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
36 public class ChannelOutboundQueue02Test {
37 private static int counter;
38 private static final int RPC_RESPONSE_EXPIRATION = 1;
39 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
40 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
42 public void onRemoval(
43 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
44 notification.getValue().discard();
48 @Mock EchoInput echoInput;
49 @Mock BarrierInput barrierInput;
50 @Mock EchoReplyInput echoReplyInput;
51 @Mock ExperimenterInput experimenterInput;
52 private ConnectionAdapterImpl adapter;
53 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
59 MockitoAnnotations.initMocks(this);
62 * Disconnect adapter after each test
65 public void tierDown(){
66 if (adapter != null && adapter.isAlive()) {
72 * Test write to closed / opened channel
76 public void test01() throws Exception {
77 final EmbeddedChannel ec = new EmbeddedChannel(new EmbededChannelHandler());
78 adapter = new ConnectionAdapterImpl(ec, InetSocketAddress.createUnresolved("localhost", 9876), true);
79 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
80 .removalListener(REMOVAL_LISTENER).build();
81 adapter.setResponseCache(cache);
82 final ChannelOutboundQueue cq = (ChannelOutboundQueue) ec.pipeline().last();
84 adapter.barrier(barrierInput);
85 adapter.echo(echoInput);
86 cq.channelInactive(ec.pipeline().lastContext());
88 Assert.assertEquals("Wrong - ChannelOutboundHandlerAdapter.write was invoked on closed channel",0, counter);
89 cq.channelActive(ec.pipeline().lastContext());
91 adapter.barrier(barrierInput);
92 adapter.experimenter(experimenterInput);
94 Assert.assertEquals("Wrong - ChannelOutboundHandlerAdapter.write has not been invoked on opened channel",2, counter);
98 * Test write to read only / writable channel
101 public void test02(){
102 final ChangeWritableEmbededChannel ec = new ChangeWritableEmbededChannel(new EmbededChannelHandler());
103 adapter = new ConnectionAdapterImpl(ec, InetSocketAddress.createUnresolved("localhost", 9876), true);
104 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
105 .removalListener(REMOVAL_LISTENER).build();
106 adapter.setResponseCache(cache);
109 adapter.barrier(barrierInput);
110 adapter.echo(echoInput);
111 ec.runPendingTasks();
112 Assert.assertEquals("Wrong - write to readonly channel",0, counter);
114 adapter.echoReply(echoReplyInput);
115 adapter.echo(echoInput);
116 ec.runPendingTasks();
117 Assert.assertEquals("Wrong - write to writtable channel",4, counter);
121 * Channel Handler for testing
125 private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
127 public void write(final ChannelHandlerContext ctx, final Object msg,
128 final ChannelPromise promise) throws Exception {
129 if(msg instanceof MessageListenerWrapper){
136 * Class for testing - channel can change state to read only or writable
140 private class ChangeWritableEmbededChannel extends EmbeddedChannel {
141 private boolean isWrittable;
142 public ChangeWritableEmbededChannel(final ChannelHandler channelHandler){
143 super(channelHandler);
148 public boolean isWritable() {
152 public void setWritable(){
156 public void setReadOnly(){