2 * Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.openflowjava.protocol.impl.core;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
15 import io.netty.channel.ChannelHandlerContext;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
25 * Unit tests for IdleHandler.
29 public class IdleHandlerTest {
31 @Mock ChannelHandlerContext mockChHndlrCtx ;
33 IdleHandler idleHandler ;
36 * Sets up test environment.
40 MockitoAnnotations.initMocks(this);
41 idleHandler = new IdleHandler(60L, TimeUnit.MINUTES);
45 * Test message passing on channel read.
48 public void testChannelRead() throws Exception {
49 idleHandler.channelRead(mockChHndlrCtx, new Object());
51 // Verify that a read was fired for the next handler ...
52 verify(mockChHndlrCtx, times(1)).fireChannelRead(Mockito.<SwitchIdleEvent>any()) ;
56 * Test channel read timeout.
59 public void testReadTimedOut() throws Exception {
60 idleHandler.readTimedOut(mockChHndlrCtx);
62 // Verify a read was fired for the next handler to process ...
63 verify(mockChHndlrCtx, times(1)).fireChannelRead(any(SwitchIdleEvent.class)) ;
67 * Test only one timeout notification.
70 public void testReadTimedOutNoOpNotFirst() throws Exception {
71 idleHandler.readTimedOut(mockChHndlrCtx);
72 idleHandler.readTimedOut(mockChHndlrCtx);
74 // Verify that only one notification was sent to the next handler ...
75 verify(mockChHndlrCtx, times(1)).fireChannelRead(any(Object.class)) ;
79 * Test two timeout notifications.
82 public void testReadTimedOutTwice() throws Exception {
83 idleHandler.readTimedOut(mockChHndlrCtx);
84 verify(mockChHndlrCtx, times(1)).fireChannelRead(any(Object.class));
86 idleHandler.channelRead(mockChHndlrCtx, new String());
87 verify(mockChHndlrCtx, times(2)).fireChannelRead(any(Object.class));
89 idleHandler.readTimedOut(mockChHndlrCtx);
90 verify(mockChHndlrCtx, times(3)).fireChannelRead(any(Object.class));