diff --git a/HttpComponent/src/test/java/com/arialyy/aria/http/download/HttpRangeValidatorTest.java b/HttpComponent/src/test/java/com/arialyy/aria/http/download/HttpRangeValidatorTest.java index 7f762e96..fd92204a 100644 --- a/HttpComponent/src/test/java/com/arialyy/aria/http/download/HttpRangeValidatorTest.java +++ b/HttpComponent/src/test/java/com/arialyy/aria/http/download/HttpRangeValidatorTest.java @@ -22,4 +22,46 @@ public class HttpRangeValidatorTest { @Test(expected = IOException.class) public void rejectsOverflow() throws Exception { HttpRangeValidator.validate(206, "bytes 10-9999999999999999999999/100", null, 10, 20); } + + @Test public void acceptsUnknownTotal() throws Exception { + HttpRangeValidator.validate(206, "bytes 10-19/*", null, 10, 20); + } + @Test public void acceptsSingleByteAtEnd() throws Exception { + HttpRangeValidator.validate(206, "bytes 99-99/100", "IDENTITY", 99, 100); + } + @Test public void acceptsLargeFileOffsets() throws Exception { + HttpRangeValidator.validate(206, "bytes 4294967296-4294967395/8589934592", + "identity", 4294967296L, 4294967396L); + } + @Test public void acceptsWhitespaceAroundHeader() throws Exception { + HttpRangeValidator.validate(206, " bytes 10-19/100 ", null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsMissingHeaderForPartialResponse() + throws Exception { + HttpRangeValidator.validate(206, null, null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsUnsatisfiableResponse() throws Exception { + HttpRangeValidator.validate(416, "bytes */100", null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsRedirect() throws Exception { + HttpRangeValidator.validate(302, "bytes 10-19/100", null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsTotalSmallerThanRange() throws Exception { + HttpRangeValidator.validate(206, "bytes 10-19/19", null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsTotalOverflow() throws Exception { + HttpRangeValidator.validate(206, "bytes 10-19/9999999999999999999999", null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsExtraBytes() throws Exception { + HttpRangeValidator.validate(206, "bytes 10-20/100", null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsNegativeOffset() throws Exception { + HttpRangeValidator.validate(206, "bytes -1-19/100", null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsMalformedHeader() throws Exception { + HttpRangeValidator.validate(206, "bytes ten-nineteen/100", null, 10, 20); + } + @Test(expected = IOException.class) public void rejectsMultipartHeader() throws Exception { + HttpRangeValidator.validate(206, "bytes 10-19/100, 30-39/100", null, 10, 20); + } } diff --git a/PublicComponent/src/test/java/com/arialyy/aria/core/event/EventMsgUtilTest.java b/PublicComponent/src/test/java/com/arialyy/aria/core/event/EventMsgUtilTest.java index 1c685339..2e8ebf86 100644 --- a/PublicComponent/src/test/java/com/arialyy/aria/core/event/EventMsgUtilTest.java +++ b/PublicComponent/src/test/java/com/arialyy/aria/core/event/EventMsgUtilTest.java @@ -2,6 +2,11 @@ package com.arialyy.aria.core.event; import java.util.ArrayList; import java.util.List; +import java.util.HashSet; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.Test; @@ -28,4 +33,125 @@ public class EventMsgUtilTest { bus.unRegister(receiver); } } + + static class GateEvent {} + + // A latch, rather than sleep, keeps the queue deterministically full during assertions. + static class Gate { + final CountDownLatch entered = new CountDownLatch(1); + final CountDownLatch release = new CountDownLatch(1); + @Event public void receive(GateEvent event) throws InterruptedException { + entered.countDown(); + if (!release.await(5, TimeUnit.SECONDS)) throw new AssertionError("Gate timed out"); + } + } + + static class Collector { + final List values = new ArrayList<>(); + final CountDownLatch drained = new CountDownLatch(1); + @Event public void receive(Integer value) { values.add(value); } + @Event public void barrier(String marker) { drained.countDown(); } + } + + @Test(expected = IllegalArgumentException.class) public void rejectsNullEvent() { + EventMsgUtil.getDefault().post(null); + } + + @Test public void rejectsFullQueueAndRecoversWithoutLosingAcceptedEvents() throws Exception { + EventMsgUtil bus = EventMsgUtil.getDefault(); + Gate gate = new Gate(); + Collector collector = new Collector(); + bus.register(gate); + bus.register(collector); + try { + bus.post(new GateEvent()); + assertTrue(gate.entered.await(5, TimeUnit.SECONDS)); + for (int i = 0; i < 9; i++) bus.post(i); + bus.post("barrier"); + try { + bus.post(999); + fail("A full queue must reject the event explicitly"); + } catch (RejectedExecutionException expected) { + // The rejected event must never reach the subscriber. + } + gate.release.countDown(); + assertTrue(collector.drained.await(5, TimeUnit.SECONDS)); + assertEquals(9, collector.values.size()); + for (int i = 0; i < 9; i++) assertEquals(Integer.valueOf(i), collector.values.get(i)); + Collector next = new Collector(); + bus.register(next); + try { + bus.post(42); + bus.post("recovered"); + assertTrue(next.drained.await(5, TimeUnit.SECONDS)); + assertEquals(java.util.Collections.singletonList(42), next.values); + } finally { + bus.unRegister(next); + } + } finally { + gate.release.countDown(); + bus.unRegister(gate); + bus.unRegister(collector); + } + } + + @Test public void queuedEventsDoNotReachUnregisteredSubscriber() throws Exception { + EventMsgUtil bus = EventMsgUtil.getDefault(); + Gate gate = new Gate(); + Collector removed = new Collector(); + Collector observer = new Collector(); + bus.register(gate); + bus.register(removed); + bus.register(observer); + try { + bus.post(new GateEvent()); + assertTrue(gate.entered.await(5, TimeUnit.SECONDS)); + bus.post(1); + bus.unRegister(removed); + bus.post("barrier"); + gate.release.countDown(); + assertTrue(observer.drained.await(5, TimeUnit.SECONDS)); + assertTrue(removed.values.isEmpty()); + assertEquals(java.util.Collections.singletonList(1), observer.values); + } finally { + gate.release.countDown(); + bus.unRegister(gate); + bus.unRegister(removed); + bus.unRegister(observer); + } + } + + @Test public void concurrentProducersDeliverEveryAcceptedEventExactlyOnce() throws Exception { + EventMsgUtil bus = EventMsgUtil.getDefault(); + Collector collector = new Collector(); + ExecutorService producers = Executors.newFixedThreadPool(4); + CountDownLatch start = new CountDownLatch(1); + List> futures = new ArrayList<>(); + bus.register(collector); + try { + for (int i = 0; i < 4; i++) { + final int producer = i; + futures.add(producers.submit(() -> { + start.await(); + bus.post(producer * 2); + bus.post(producer * 2 + 1); + return null; + })); + } + start.countDown(); + for (Future future : futures) future.get(5, TimeUnit.SECONDS); + bus.post("barrier"); + assertTrue(collector.drained.await(5, TimeUnit.SECONDS)); + assertEquals(8, collector.values.size()); + assertEquals(8, new HashSet<>(collector.values).size()); + for (int i = 0; i < 8; i++) assertTrue(collector.values.contains(i)); + for (int i = 0; i < 4; i++) { + assertTrue(collector.values.indexOf(i * 2) < collector.values.indexOf(i * 2 + 1)); + } + } finally { + start.countDown(); + producers.shutdownNow(); + bus.unRegister(collector); + } + } }