perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Multiple Spark instances within single JVM use single 404 route instead of supplied to Service.notFound #1103

Open azazar opened 5 years ago

azazar commented 5 years ago

Test:

import org.junit.Test;
import spark.Service;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import static org.junit.Assert.assertEquals;

public class ErrorHandlerTest {

    private int spark1Port = 52335;
    private int spark2Port = spark1Port + 1;

    Service spark1, spark2;

    int spark1NotFound = 0;
    int spark2NotFound = 0;

    public Service init(int port, Runnable counter404) {
        Service spark = Service.ignite();

        spark.port(port);
        spark.init();

        spark.get("/", (req, resp) -> "OK");

        spark.notFound((req, resp) -> {
            counter404.run();
            return "";
        });

        return spark;
    }

    public void init() {
        spark1 = init(spark1Port, () -> spark1NotFound++);
        spark2 = init(spark2Port, () -> spark2NotFound++);

        spark1.awaitInitialization();
        spark2.awaitInitialization();
    }

    public void done() {
        spark1.stop();
        spark2.stop();

        spark1.awaitStop();
        spark2.awaitStop();
    }

    private void query(int port, String path) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:" + port + path).openConnection();

        try {
            try (InputStream in = conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST ? conn.getInputStream() : conn.getErrorStream()) {
            }
        }
        finally {
            conn.disconnect();
        }
    }

    @Test
    public void errorHandlerTest() throws Exception {
        init();

        assertEquals(0, spark1NotFound);
        assertEquals(0, spark2NotFound);

        query(spark1Port, "/404");

        assertEquals(1, spark1NotFound);
        assertEquals(0, spark2NotFound);

        query(spark2Port, "/404");

        assertEquals(1, spark1NotFound);
        assertEquals(1, spark2NotFound);

        done();
    }

}

Output:

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.316 sec <<< FAILURE!
errorHandlerTest(ErrorHandlerTest)  Time elapsed: 0.287 sec  <<< FAILURE!
java.lang.AssertionError: expected:<1> but was:<0>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:631)
    at ErrorHandlerTest.errorHandlerTest(ErrorHandlerTest.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)