View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.impl.nio;
29  
30  import org.apache.hc.core5.http.impl.nio.AbstractHttp1StreamDuplexer.CapacityWindow;
31  import org.apache.hc.core5.reactor.IOSession;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.mockito.Mock;
35  
36  import java.io.IOException;
37  import java.nio.channels.SelectionKey;
38  
39  import static org.junit.Assert.assertEquals;
40  import static org.mockito.ArgumentMatchers.eq;
41  import static org.mockito.Mockito.verify;
42  import static org.mockito.Mockito.verifyNoMoreInteractions;
43  import static org.mockito.Mockito.verifyZeroInteractions;
44  import static org.mockito.MockitoAnnotations.initMocks;
45  
46  public class TestAbstractHttp1StreamDuplexerCapacityWindow {
47      @Mock private IOSession ioSession;
48  
49      @Before
50      public void before() {
51          initMocks(this);
52      }
53  
54      @Test
55      public void testWindowUpdate() throws IOException {
56          final CapacityWindow window = new CapacityWindow(0, ioSession);
57          window.update(1);
58          assertEquals(1, window.getWindow());
59          verify(ioSession).setEvent(eq(SelectionKey.OP_READ));
60          verifyNoMoreInteractions(ioSession);
61      }
62  
63      @Test
64      public void testRemoveCapacity() {
65          final CapacityWindow window = new CapacityWindow(1, ioSession);
66          window.removeCapacity(1);
67          assertEquals(0, window.getWindow());
68          verify(ioSession).clearEvent(eq(SelectionKey.OP_READ));
69          verifyNoMoreInteractions(ioSession);
70      }
71  
72      @Test
73      public void noReadsSetAfterWindowIsClosed() throws IOException {
74          final CapacityWindow window = new CapacityWindow(1, ioSession);
75          window.close();
76          window.update(1);
77          verifyZeroInteractions(ioSession);
78      }
79  
80      @Test
81      public void windowCannotUnderflow() {
82          final CapacityWindow window = new CapacityWindow(Integer.MIN_VALUE, ioSession);
83          window.removeCapacity(1);
84          assertEquals(Integer.MIN_VALUE, window.getWindow());
85      }
86  
87      @Test
88      public void windowCannotOverflow() throws IOException{
89          final CapacityWindow window = new CapacityWindow(Integer.MAX_VALUE, ioSession);
90          window.update(1);
91          assertEquals(Integer.MAX_VALUE, window.getWindow());
92      }
93  
94      @Test
95      public void noChangesIfUpdateIsNonPositive() throws IOException {
96          final CapacityWindow window = new CapacityWindow(1, ioSession);
97          window.update(0);
98          window.update(-1);
99          assertEquals(1, window.getWindow());
100         verifyZeroInteractions(ioSession);
101     }
102 }