Add missing license headers
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / OutboundQueueEntryTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
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;
28
29 /**
30  * {@link OutboundQueueEntry} class test
31  */
32 @RunWith(MockitoJUnitRunner.class)
33 public class OutboundQueueEntryTest {
34
35     private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueEntryTest.class);
36
37     private static final short VERSION = (short) 13;
38     private static final long VALUE = 1L;
39
40     private Integer failCounter = 0;
41
42     @Mock
43     private OfHeader ofHeader;
44     @Mock
45     private FutureCallback<OfHeader> futureCallback;
46
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();
54
55     @Test
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());
61     }
62
63     @Test
64     public void reset() throws Exception {
65         outboundQueueEntry.commit(ofHeader, futureCallback);
66         Assert.assertTrue(outboundQueueEntry.isCommitted());
67
68         outboundQueueEntry.reset();
69         Assert.assertFalse(outboundQueueEntry.isCommitted());
70     }
71
72     @Test
73     public void isBarrier() throws Exception {
74         outboundQueueEntry.commit(barrierInput, futureCallback);
75         Assert.assertTrue(outboundQueueEntry.isBarrier());
76     }
77
78     @Test
79     public void takeMessage() throws Exception {
80         outboundQueueEntry.commit(packetOutInput, futureCallback);
81         outboundQueueEntry.takeMessage();
82         Mockito.verify(futureCallback).onSuccess(Mockito.<OfHeader>any());
83     }
84
85     @Test
86     public void complete() throws Exception {
87         final boolean result = outboundQueueEntry.complete(multipartReplyMessage);
88         Assert.assertTrue(result);
89         Assert.assertTrue(outboundQueueEntry.isCompleted());
90     }
91
92     @Test(expected = IllegalStateException.class)
93     public void completeTwice() throws Exception {
94         outboundQueueEntry.complete(multipartReplyMessage);
95         outboundQueueEntry.complete(multipartReplyMessage);
96     }
97
98     @Test
99     public void fail() throws Exception {
100         outboundQueueEntry.commit(ofHeader, futureCallback);
101         outboundQueueEntry.fail(null);
102         Mockito.verify(futureCallback).onFailure(Mockito.<OutboundQueueException>any());
103     }
104
105     private Integer increaseFailCounter() {
106         return ++this.failCounter;
107     }
108
109     @Test
110     public void test() throws Exception {
111
112         final FutureCallback<OfHeader> result =
113                 new FutureCallback<OfHeader>() {
114
115                     @Override
116                     public void onSuccess(@Nullable OfHeader ofHeader) {
117                         LOG.info("onSuccess: xid: {}", ofHeader.getXid());
118                     }
119
120                     @Override
121                     public void onFailure(Throwable throwable) {
122                         LOG.info("onFailure! Error: {}", throwable);
123                         LOG.info("Failure called {} time", increaseFailCounter());
124                     }
125                 };
126
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 */
130
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);
137
138         Assert.assertTrue(outboundQueueEntry.isCompleted());
139         Assert.assertTrue(outboundQueueEntry.isCommitted());
140
141         /** This is check that no callback is in entry stuck */
142         Assert.assertFalse(outboundQueueEntry.hasCallback());
143
144         Assert.assertTrue(this.failCounter == 1);
145     }
146
147 }