Initial commit

This commit is contained in:
Arjen Poutsma
2015-06-05 15:38:50 +02:00
commit 342299abf6
11 changed files with 975 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
/*
* 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.rx.io;
import java.io.EOFException;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.springframework.rx.util.BlockingByteBufQueue;
import org.springframework.rx.util.BlockingByteBufQueuePublisher;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
*/
public class ByteBufPublisherInputStreamTests {
private BlockingByteBufQueue queue;
private ByteBufPublisherInputStream is;
@Before
public void setUp() throws Exception {
queue = new BlockingByteBufQueue();
is = new ByteBufPublisherInputStream(queue);
}
@Test
public void readSingleByte() throws Exception {
ByteBuf abc = Unpooled.copiedBuffer(new byte[]{'a', 'b', 'c'});
ByteBuf def = Unpooled.copiedBuffer(new byte[]{'d', 'e', 'f'});
queue.putBuffer(abc);
queue.putBuffer(def);
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 {
ByteBuf abc = Unpooled.copiedBuffer(new byte[]{'a', 'b', 'c'});
ByteBuf def = Unpooled.copiedBuffer(new byte[]{'d', 'e', 'f'});
queue.putBuffer(abc);
queue.putBuffer(def);
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

@@ -0,0 +1,178 @@
/*
* 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.rx.util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
/**
* @author Arjen Poutsma
*/
public class BlockingByteBufQueuePublisherTests {
private BlockingByteBufQueue queue;
private BlockingByteBufQueuePublisher publisher;
@Before
public void setUp() throws Exception {
queue = new BlockingByteBufQueue();
publisher = new BlockingByteBufQueuePublisher(queue);
}
@Test
public void normal() throws Exception {
ByteBuf abc = Unpooled.copiedBuffer(new byte[]{'a', 'b', 'c'});
ByteBuf def = Unpooled.copiedBuffer(new byte[]{'d', 'e', 'f'});
queue.putBuffer(abc);
queue.putBuffer(def);
queue.complete();
final AtomicBoolean complete = new AtomicBoolean(false);
final List<ByteBuf> received = new ArrayList<ByteBuf>(2);
publisher.subscribe(new Subscriber<ByteBuf>() {
@Override
public void onSubscribe(Subscription s) {
s.request(2);
}
@Override
public void onNext(ByteBuf byteBuf) {
received.add(byteBuf);
}
@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 {
ByteBuf abc = Unpooled.copiedBuffer(new byte[]{'a', 'b', 'c'});
ByteBuf def = Unpooled.copiedBuffer(new byte[]{'d', 'e', 'f'});
queue.putBuffer(abc);
queue.putBuffer(def);
queue.complete();
final AtomicBoolean complete = new AtomicBoolean(false);
final List<ByteBuf> received = new ArrayList<ByteBuf>(2);
publisher.subscribe(new Subscriber<ByteBuf>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ByteBuf byteBuf) {
received.add(byteBuf);
}
@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<ByteBuf>() {
@Override
public void onSubscribe(Subscription s) {
}
@Override
public void onNext(ByteBuf byteBuf) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});
publisher.subscribe(new Subscriber<ByteBuf>() {
@Override
public void onSubscribe(Subscription s) {
fail("onSubscribe not expected");
}
@Override
public void onNext(ByteBuf byteBuf) {
fail("onNext not expected");
}
@Override
public void onError(Throwable t) {
assertTrue(t instanceof IllegalStateException);
}
@Override
public void onComplete() {
fail("onComplete not expected");
}
});
}
}

View File

@@ -0,0 +1,81 @@
/*
* 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.rx.util;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
/**
* @author Arjen Poutsma
*/
public class BlockingByteBufQueueTests {
private BlockingByteBufQueue queue;
@Before
public void setUp() throws Exception {
queue = new BlockingByteBufQueue();
}
@Test
public void normal() throws Exception {
ByteBuf abc = Unpooled.copiedBuffer(new byte[]{'a', 'b', 'c'});
ByteBuf def = Unpooled.copiedBuffer(new byte[]{'d', 'e', 'f'});
queue.putBuffer(abc);
queue.putBuffer(def);
queue.complete();
assertTrue(queue.isHeadBuffer());
assertFalse(queue.isHeadError());
assertSame(abc, queue.pollBuffer());
assertTrue(queue.isHeadBuffer());
assertFalse(queue.isHeadError());
assertSame(def, queue.pollBuffer());
assertTrue(queue.isComplete());
}
@Test
public void empty() throws Exception {
assertNull(queue.pollBuffer());
}
@Test
public void error() throws Exception {
ByteBuf abc = Unpooled.copiedBuffer(new byte[]{'a', 'b', 'c'});
Throwable error = new IllegalStateException();
queue.putBuffer(abc);
queue.putError(error);
queue.complete();
assertTrue(queue.isHeadBuffer());
assertFalse(queue.isHeadError());
assertSame(abc, queue.pollBuffer());
assertTrue(queue.isHeadError());
assertFalse(queue.isHeadBuffer());
assertSame(error, queue.pollError());
assertTrue(queue.isComplete());
}
}