Removed BlockingSignalQueue in favor of PublisherSignal.

This commit is contained in:
Arjen Poutsma
2015-09-10 11:05:30 +02:00
parent 0ec29d1c67
commit 5bbeb9c204
11 changed files with 394 additions and 885 deletions

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2002-2015 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.
* 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.springframework.reactive.io;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.rx.Stream;
import reactor.rx.Streams;
import org.springframework.util.FileCopyUtils;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
*/
public class ByteArrayPublisherInputStreamTests {
private ByteArrayPublisherInputStream is;
@Before
public void createStream() {
Stream<byte[]> stream =
Streams.just(new byte[]{'a', 'b', 'c'}, new byte[]{'d', 'e'});
is = new ByteArrayPublisherInputStream(stream);
}
@Test
public void reactor() throws Exception {
assertEquals(3, is.available());
int ch = is.read();
assertEquals('a', ch);
ch = is.read();
assertEquals('b', ch);
ch = is.read();
assertEquals('c', ch);
assertEquals(2, is.available());
ch = is.read();
assertEquals('d', ch);
ch = is.read();
assertEquals('e', ch);
ch = is.read();
assertEquals(-1, ch);
assertEquals(0, is.available());
}
@Test
public void copy() throws Exception {
ByteArrayPublisherOutputStream os = new ByteArrayPublisherOutputStream();
FileCopyUtils.copy(is, os);
Publisher<byte[]> publisher = os.toByteArrayPublisher();
publisher.subscribe(new Subscriber<byte[]>() {
List<byte[]> result = new ArrayList<>();
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(byte[] bytes) {
result.add(bytes);
}
@Override
public void onError(Throwable t) {
fail(t.getMessage());
}
@Override
public void onComplete() {
assertArrayEquals(result.get(0), new byte[]{'a', 'b', 'c'});
assertArrayEquals(result.get(0), new byte[]{'d', 'e'});
}
});
}
}

View File

@@ -1,96 +0,0 @@
/*
* Copyright 2002-2015 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.
* 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.springframework.reactive.io;
import org.junit.Before;
import org.junit.Test;
import org.springframework.reactive.util.BlockingSignalQueue;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
/**
* @author Arjen Poutsma
*/
public class ByteBufPublisherInputStreamTests {
private BlockingSignalQueue<byte[]> queue;
private ByteArrayPublisherInputStream is;
@Before
public void setUp() throws Exception {
queue = new BlockingSignalQueue<byte[]>();
is = new ByteArrayPublisherInputStream(queue);
}
@Test
public void readSingleByte() throws Exception {
queue.putSignal(new byte[]{'a', 'b', 'c'});
queue.putSignal(new byte[]{'d', 'e', 'f'});
queue.complete();
int ch = is.read();
assertEquals('a', ch);
ch = is.read();
assertEquals('b', ch);
ch = is.read();
assertEquals('c', ch);
ch = is.read();
assertEquals('d', ch);
ch = is.read();
assertEquals('e', ch);
ch = is.read();
assertEquals('f', ch);
ch = is.read();
assertEquals(-1, ch);
}
@Test
public void readBytes() throws Exception {
queue.putSignal(new byte[]{'a', 'b', 'c'});
queue.putSignal(new byte[]{'d', 'e', 'f'});
queue.complete();
byte[] buf = new byte[2];
int read = this.is.read(buf);
assertEquals(2, read);
assertArrayEquals(new byte[] { 'a', 'b'}, buf);
read = this.is.read(buf);
assertEquals(1, read);
assertEquals('c', buf[0]);
read = this.is.read(buf);
assertEquals(2, read);
assertArrayEquals(new byte[] { 'd', 'e'}, buf);
read = this.is.read(buf);
assertEquals(1, read);
assertEquals('f', buf[0]);
read = this.is.read(buf);
assertEquals(-1, read);
}
}

View File

@@ -1,231 +0,0 @@
/*
* Copyright 2002-2015 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.
* 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.springframework.reactive.util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Before;
import org.junit.Test;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Arjen Poutsma
*/
public class BlockingByteBufQueuePublisherTests {
private BlockingSignalQueue<byte[]> queue;
private Publisher<byte[]> publisher;
@Before
public void setUp() throws Exception {
queue = new BlockingSignalQueue<byte[]>();
publisher = queue.publisher();
}
@Test
public void normal() throws Exception {
byte[] abc = new byte[]{'a', 'b', 'c'};
byte[] def = new byte[]{'d', 'e', 'f'};
queue.putSignal(abc);
queue.putSignal(def);
queue.complete();
final AtomicBoolean complete = new AtomicBoolean(false);
final List<byte[]> received = new ArrayList<byte[]>(2);
publisher.subscribe(new Subscriber<byte[]>() {
private Subscription subscription;
@Override
public void onSubscribe(Subscription s) {
s.request(1);
this.subscription = s;
}
@Override
public void onNext(byte[] bytes) {
received.add(bytes);
this.subscription.request(1);
}
@Override
public void onError(Throwable t) {
fail("onError not expected");
}
@Override
public void onComplete() {
complete.set(true);
}
});
while (!complete.get()) {
}
assertEquals(2, received.size());
assertSame(abc, received.get(0));
assertSame(def, received.get(1));
}
@Test
public void unbounded() throws Exception {
byte[] abc = new byte[]{'a', 'b', 'c'};
byte[] def = new byte[]{'d', 'e', 'f'};
queue.putSignal(abc);
queue.putSignal(def);
queue.complete();
final AtomicBoolean complete = new AtomicBoolean(false);
final List<byte[]> received = new ArrayList<byte[]>(2);
publisher.subscribe(new Subscriber<byte[]>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(byte[] bytes) {
received.add(bytes);
}
@Override
public void onError(Throwable t) {
fail("onError not expected");
}
@Override
public void onComplete() {
complete.set(true);
}
});
while (!complete.get()) {
}
assertEquals(2, received.size());
assertSame(abc, received.get(0));
assertSame(def, received.get(1));
}
@Test
public void multipleSubscribe() throws Exception {
publisher.subscribe(new Subscriber<byte[]>() {
@Override
public void onSubscribe(Subscription s) {
}
@Override
public void onNext(byte[] bytes) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});
publisher.subscribe(new Subscriber<byte[]>() {
@Override
public void onSubscribe(Subscription s) {
fail("onSubscribe not expected");
}
@Override
public void onNext(byte[] bytes) {
fail("onNext not expected");
}
@Override
public void onError(Throwable t) {
assertTrue(t instanceof IllegalStateException);
}
@Override
public void onComplete() {
fail("onComplete not expected");
}
});
}
@Test
public void cancel() throws Exception {
byte[] abc = new byte[]{'a', 'b', 'c'};
byte[] def = new byte[]{'d', 'e', 'f'};
queue.putSignal(abc);
queue.putSignal(def);
queue.complete();
final AtomicBoolean complete = new AtomicBoolean(false);
final List<byte[]> received = new ArrayList<byte[]>(1);
publisher.subscribe(new Subscriber<byte[]>() {
private Subscription subscription;
@Override
public void onSubscribe(Subscription s) {
s.request(1);
this.subscription = s;
}
@Override
public void onNext(byte[] bytes) {
received.add(bytes);
this.subscription.cancel();
complete.set(true);
}
@Override
public void onError(Throwable t) {
fail("onError not expected");
}
@Override
public void onComplete() {
}
});
while (!complete.get()) {
}
assertEquals(1, received.size());
assertSame(abc, received.get(0));
}
}

View File

@@ -1,78 +0,0 @@
/*
* Copyright 2002-2015 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.
* 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.springframework.reactive.util;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Arjen Poutsma
*/
public class BlockingByteBufQueueTests {
private BlockingSignalQueue<byte[]> queue;
@Before
public void setUp() throws Exception {
queue = new BlockingSignalQueue<byte[]>();
}
@Test
public void normal() throws Exception {
byte[] abc = new byte[]{'a', 'b', 'c'};
byte[] def = new byte[]{'d', 'e', 'f'};
queue.putSignal(abc);
queue.putSignal(def);
queue.complete();
assertTrue(queue.isHeadSignal());
assertFalse(queue.isHeadError());
assertSame(abc, queue.pollSignal());
assertTrue(queue.isHeadSignal());
assertFalse(queue.isHeadError());
assertSame(def, queue.pollSignal());
assertTrue(queue.isComplete());
}
@Test
public void error() throws Exception {
byte[] abc = new byte[]{'a', 'b', 'c'};
Throwable error = new IllegalStateException();
queue.putSignal(abc);
queue.putError(error);
queue.complete();
assertTrue(queue.isHeadSignal());
assertFalse(queue.isHeadError());
assertSame(abc, queue.pollSignal());
assertTrue(queue.isHeadError());
assertFalse(queue.isHeadSignal());
assertSame(error, queue.pollError());
assertTrue(queue.isComplete());
}
}