Added tests for errors in the source stream

This commit adds decoder/message-reader tests for errors in
the source data buffer publisher. Because the tests extend
AbstractDataBufferAllocatingTestCase, they also check whether
the buffers that precede the error in the stream are properly
released.

Issue: SPR-17025
This commit is contained in:
Arjen Poutsma
2018-09-05 11:03:55 +02:00
parent 196c0adf47
commit 259b2ca5f4
6 changed files with 126 additions and 18 deletions

View File

@@ -29,9 +29,7 @@ import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.util.MimeTypeUtils;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
@@ -67,6 +65,21 @@ public class ByteArrayDecoderTests extends AbstractDataBufferAllocatingTestCase
.verify();
}
@Test
public void decodeError() {
DataBuffer fooBuffer = stringBuffer("foo");
Flux<DataBuffer> source =
Flux.just(fooBuffer).mergeWith(Flux.error(new RuntimeException()));
Flux<byte[]> output = this.decoder.decode(source,
ResolvableType.forClassWithGenerics(Publisher.class, byte[].class),
null, Collections.emptyMap());
StepVerifier.create(output)
.consumeNextWith(bytes -> assertArrayEquals("foo".getBytes(), bytes))
.expectError()
.verify();
}
@Test
public void decodeToMono() {
DataBuffer fooBuffer = stringBuffer("foo");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,8 +30,7 @@ import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.util.MimeTypeUtils;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* @author Sebastien Deleuze
@@ -65,6 +64,21 @@ public class ByteBufferDecoderTests extends AbstractDataBufferAllocatingTestCase
.verify();
}
@Test
public void decodeError() {
DataBuffer fooBuffer = stringBuffer("foo");
Flux<DataBuffer> source =
Flux.just(fooBuffer).mergeWith(Flux.error(new RuntimeException()));
Flux<ByteBuffer> output = this.decoder.decode(source,
ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class),
null, Collections.emptyMap());
StepVerifier.create(output)
.expectNext(ByteBuffer.wrap("foo".getBytes()))
.expectError()
.verify();
}
@Test
public void decodeToMono() {
DataBuffer fooBuffer = stringBuffer("foo");

View File

@@ -32,7 +32,7 @@ import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StreamUtils;
import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.*;
import static org.springframework.core.ResolvableType.forClass;
/**
* @author Arjen Poutsma
@@ -73,4 +73,19 @@ public class ResourceDecoderTests extends AbstractDataBufferAllocatingTestCase {
.verify();
}
@Test
public void decodeError() {
DataBuffer fooBuffer = stringBuffer("foo");
Flux<DataBuffer> source =
Flux.just(fooBuffer).mergeWith(Flux.error(new RuntimeException()));
Flux<Resource> result = this.decoder
.decode(source, forClass(Resource.class), null, Collections.emptyMap());
StepVerifier.create(result)
.expectError()
.verify();
}
}

View File

@@ -173,6 +173,22 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
}
@Test
public void decodeError() {
DataBuffer fooBuffer = stringBuffer("foo\n");
Flux<DataBuffer> source =
Flux.just(fooBuffer).mergeWith(Flux.error(new RuntimeException()));
Flux<String> output = this.decoder.decode(source,
ResolvableType.forClass(String.class), null, Collections.emptyMap());
StepVerifier.create(output)
.expectNext("foo")
.expectError()
.verify();
}
@Test
public void decodeToMono() {
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));