Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public void request(int numMessages) {
@Override
public void decode(InputStream inputStream) throws DecodeException {
if (closed) {
// ignored
try {
inputStream.close();
} catch (IOException e) {
throw new DecodeException(e);
}
return;
}
accumulate.addInputStream(inputStream);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting.http12.message;

import org.apache.dubbo.remoting.http12.exception.DecodeException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DefaultStreamingDecoderTest {

@Test
void closesInputReceivedAfterStreamClosed() {
DefaultStreamingDecoder decoder = new DefaultStreamingDecoder();
decoder.onStreamClosed();
CloseTrackingInputStream inputStream = new CloseTrackingInputStream(new byte[] {0});

decoder.decode(inputStream);

assertTrue(inputStream.closed);
}

@Test
void closesInputReceivedAfterDecoderClosed() {
DefaultStreamingDecoder decoder = new DefaultStreamingDecoder();
decoder.close();
CloseTrackingInputStream inputStream = new CloseTrackingInputStream(new byte[] {0});

decoder.decode(inputStream);

assertTrue(inputStream.closed);
}

@Test
void propagatesCloseFailureAsDecodeException() {
DefaultStreamingDecoder decoder = new DefaultStreamingDecoder();
decoder.onStreamClosed();
InputStream failingStream = new InputStream() {
@Override
public int read() throws IOException {
return -1;
}

@Override
public void close() throws IOException {
throw new IOException("close failed");
}
};

assertThrows(DecodeException.class, () -> decoder.decode(failingStream));
}

private static final class CloseTrackingInputStream extends ByteArrayInputStream {

private boolean closed;

private CloseTrackingInputStream(byte[] buf) {
super(buf);
}

@Override
public void close() throws IOException {
this.closed = true;
super.close();
}
}
}
Loading