2 * Copyright (c) 2015 Cisco Systems, Inc. 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.util.concurrent.FutureCallback;
11 import javax.annotation.Nullable;
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.runners.MockitoJUnitRunner;
18 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueException;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * {@link OutboundQueueEntry} class test
32 @RunWith(MockitoJUnitRunner.class)
33 public class OutboundQueueEntryTest {
35 private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueEntryTest.class);
37 private static final short VERSION = (short) 13;
38 private static final long VALUE = 1L;
40 private Integer failCounter = 0;
43 private OfHeader ofHeader;
45 private FutureCallback<OfHeader> futureCallback;
47 private final OutboundQueueEntry outboundQueueEntry = new OutboundQueueEntry();
48 private final OfHeader barrierInput = new BarrierInputBuilder().setVersion(VERSION).setXid(VALUE).build();
49 private final OfHeader packetOutInput = new PacketOutInputBuilder().setVersion(VERSION).setXid(VALUE).build();
50 private final OfHeader multipartReplyMessage =
51 new MultipartReplyMessageBuilder().setVersion(VERSION).setXid(VALUE).setFlags(new MultipartRequestFlags(false)).build();
52 private final OfHeader flowModInput = new FlowModInputBuilder().setVersion(VERSION).setXid(VALUE).build();
53 private final OfHeader flowRemoved = new FlowRemovedMessageBuilder().setVersion(VERSION).setXid(VALUE).build();
56 public void commit() throws Exception {
57 outboundQueueEntry.commit(ofHeader, futureCallback);
58 Assert.assertTrue(outboundQueueEntry.isCommitted());
59 Assert.assertFalse(outboundQueueEntry.isCompleted());
60 Assert.assertFalse(outboundQueueEntry.isBarrier());
64 public void reset() throws Exception {
65 outboundQueueEntry.commit(ofHeader, futureCallback);
66 Assert.assertTrue(outboundQueueEntry.isCommitted());
68 outboundQueueEntry.reset();
69 Assert.assertFalse(outboundQueueEntry.isCommitted());
73 public void isBarrier() throws Exception {
74 outboundQueueEntry.commit(barrierInput, futureCallback);
75 Assert.assertTrue(outboundQueueEntry.isBarrier());
79 public void takeMessage() throws Exception {
80 outboundQueueEntry.commit(packetOutInput, futureCallback);
81 outboundQueueEntry.takeMessage();
82 Mockito.verify(futureCallback).onSuccess(Mockito.<OfHeader>any());
86 public void complete() throws Exception {
87 final boolean result = outboundQueueEntry.complete(multipartReplyMessage);
88 Assert.assertTrue(result);
89 Assert.assertTrue(outboundQueueEntry.isCompleted());
92 @Test(expected = IllegalStateException.class)
93 public void completeTwice() throws Exception {
94 outboundQueueEntry.complete(multipartReplyMessage);
95 outboundQueueEntry.complete(multipartReplyMessage);
99 public void fail() throws Exception {
100 outboundQueueEntry.commit(ofHeader, futureCallback);
101 outboundQueueEntry.fail(null);
102 Mockito.verify(futureCallback).onFailure(Mockito.<OutboundQueueException>any());
105 private Integer increaseFailCounter() {
106 return ++this.failCounter;
110 public void test() throws Exception {
112 final FutureCallback<OfHeader> result =
113 new FutureCallback<OfHeader>() {
116 public void onSuccess(@Nullable OfHeader ofHeader) {
117 LOG.info("onSuccess: xid: {}", ofHeader.getXid());
121 public void onFailure(Throwable throwable) {
122 LOG.info("onFailure! Error: {}", throwable);
123 LOG.info("Failure called {} time", increaseFailCounter());
127 /** This scenario creates entry with XID 1 then commit it, fail it and again commit it */
128 /** Simulates behavior when entry is committed after fail */
129 /** It shouldn't be in state completed and still have callback, it can consume all threads in thread pool */
131 /** Entry but no callback */
132 outboundQueueEntry.commit(flowModInput, null);
133 /** Failed entry for whatever reason */
134 outboundQueueEntry.fail(null);
135 /** Commit the same entry adding callback */
136 outboundQueueEntry.commit(flowModInput, result);
138 Assert.assertTrue(outboundQueueEntry.isCompleted());
139 Assert.assertTrue(outboundQueueEntry.isCommitted());
141 /** This is check that no callback is in entry stuck */
142 Assert.assertFalse(outboundQueueEntry.hasCallback());
144 Assert.assertTrue(this.failCounter == 1);