|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal.netty; |
| 7 | + |
| 8 | +import static org.mockito.Mockito.*; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import io.netty.buffer.ByteBuf; |
| 15 | +import io.netty.buffer.ByteBufAllocator; |
| 16 | +import io.netty.channel.ChannelFuture; |
| 17 | +import io.netty.channel.ChannelHandlerContext; |
| 18 | +import io.netty.handler.codec.http.HttpResponse; |
| 19 | +import io.netty.handler.codec.http.LastHttpContent; |
| 20 | + |
| 21 | +public class Issue3554 { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void shouldCloseOutputStreamOnce() throws IOException { |
| 25 | + var ctx = mock(NettyContext.class); |
| 26 | + var headers = mock(HttpResponse.class); |
| 27 | + |
| 28 | + var buffer = mock(ByteBuf.class); |
| 29 | + when(buffer.readableBytes()).thenReturn(0); |
| 30 | + |
| 31 | + var bufferAllocator = mock(ByteBufAllocator.class); |
| 32 | + when(bufferAllocator.buffer(0, 1024)).thenReturn(buffer); |
| 33 | + |
| 34 | + var future = mock(ChannelFuture.class); |
| 35 | + var channelContext = mock(ChannelHandlerContext.class); |
| 36 | + when(channelContext.alloc()).thenReturn(bufferAllocator); |
| 37 | + when(channelContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT)).thenReturn(future); |
| 38 | + |
| 39 | + var httpOutputStream = new NettyOutputStream(ctx, channelContext, 1024, headers); |
| 40 | + httpOutputStream.close(); |
| 41 | + httpOutputStream.close(); |
| 42 | + |
| 43 | + verify(channelContext, times(1)).writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); |
| 44 | + } |
| 45 | +} |
0 commit comments