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 java.io.IOException;
31  import java.nio.channels.SelectionKey;
32  
33  import org.apache.hc.core5.http.impl.nio.AbstractHttp1StreamDuplexer.CapacityWindow;
34  import org.apache.hc.core5.reactor.IOSession;
35  
36  import org.junit.jupiter.api.AfterEach;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.BeforeEach;
39  import org.junit.jupiter.api.Test;
40  import org.mockito.Mock;
41  import org.mockito.Mockito;
42  import org.mockito.MockitoAnnotations;
43  
44  public class TestAbstractHttp1StreamDuplexerCapacityWindow {
45  
46      @Mock
47      private IOSession ioSession;
48  
49      private AutoCloseable closeable;
50  
51      @BeforeEach
52      public void prepareMocks() {
53          closeable = MockitoAnnotations.openMocks(this);
54      }
55  
56      @AfterEach
57      public void releaseMocks() throws Exception {
58          closeable.close();
59      }
60  
61      @Test
62      public void testWindowUpdate() throws IOException {
63          final CapacityWindow window = new CapacityWindow(0, ioSession);
64          window.update(1);
65          Assertions.assertEquals(1, window.getWindow());
66          Mockito.verify(ioSession).setEvent(Mockito.eq(SelectionKey.OP_READ));
67          Mockito.verifyNoMoreInteractions(ioSession);
68      }
69  
70      @Test
71      public void testRemoveCapacity() {
72          final CapacityWindow window = new CapacityWindow(1, ioSession);
73          window.removeCapacity(1);
74          Assertions.assertEquals(0, window.getWindow());
75          Mockito.verify(ioSession).clearEvent(Mockito.eq(SelectionKey.OP_READ));
76          Mockito.verifyNoMoreInteractions(ioSession);
77      }
78  
79      @Test
80      public void noReadsSetAfterWindowIsClosed() throws IOException {
81          final CapacityWindow window = new CapacityWindow(1, ioSession);
82          window.close();
83          window.update(1);
84          Mockito.verifyNoInteractions(ioSession);
85      }
86  
87      @Test
88      public void windowCannotUnderflow() {
89          final CapacityWindow window = new CapacityWindow(Integer.MIN_VALUE, ioSession);
90          window.removeCapacity(1);
91          Assertions.assertEquals(Integer.MIN_VALUE, window.getWindow());
92      }
93  
94      @Test
95      public void windowCannotOverflow() throws IOException{
96          final CapacityWindow window = new CapacityWindow(Integer.MAX_VALUE, ioSession);
97          window.update(1);
98          Assertions.assertEquals(Integer.MAX_VALUE, window.getWindow());
99      }
100 
101     @Test
102     public void noChangesIfUpdateIsNonPositive() throws IOException {
103         final CapacityWindow window = new CapacityWindow(1, ioSession);
104         window.update(0);
105         window.update(-1);
106         Assertions.assertEquals(1, window.getWindow());
107         Mockito.verifyNoInteractions(ioSession);
108     }
109 }