Esempio n. 1
0
  @Test
  public void testAsyncHttpServer() {
    Log.debugging();

    On.req(
        req -> {
          U.must(!req.isAsync());
          req.async();
          U.must(req.isAsync());

          Jobs.after(10, TimeUnit.MILLISECONDS)
              .run(
                  () -> {
                    IO.write(req.out(), "O");

                    Jobs.after(10, TimeUnit.MILLISECONDS)
                        .run(
                            () -> {
                              IO.write(req.out(), "K");
                              req.done();
                            });
                  });

          return req;
        });

    Self.get("/").expect("OK").benchmark(1, 100, 10000);
    Self.post("/").expect("OK").benchmark(1, 100, 10000);
  }
  @Test
  public void testWithInvalidEncoding() {
    On.get("/").json(req -> U.map("uri", req.uri(), "query", req.query(), "data", req.data()));

    String resp =
        Msc.connect(
            "localhost",
            8888,
            (in, reader, out) -> {
              out.writeBytes("GET /?a=[%A%]&b=bb!&c=%&d=%% HTTP/1.0\n\n");
              return Flow.of(reader.lines()).findLast().get();
            });

    verify(resp);
  }
  @Test
  public void testSyncAsyncMix() {

    On.get("/")
        .plain(
            (Resp resp, Integer n) -> {
              if (n % 2 == 0) return n;

              return Jobs.after(3, TimeUnit.MILLISECONDS).run(() -> resp.result(n * 10).done());
            });

    for (int i = 0; i < ROUNDS; i++) {
      int expected = i % 2 == 0 ? i : i * 10;
      Self.get("/?n=" + i).expect("" + expected);
    }
  }
Esempio n. 4
0
  @Test
  public void testAsyncHttpServer2() {
    On.req(
        req ->
            Jobs.after(10, TimeUnit.MILLISECONDS)
                .run(
                    () -> {
                      IO.write(req.out(), "A");

                      Jobs.after(10, TimeUnit.MILLISECONDS)
                          .run(
                              () -> {
                                IO.write(req.out(), "SYNC");
                                req.done();
                              });
                    }));

    Self.get("/").expect("ASYNC").benchmark(1, 100, 10000);
    Self.post("/").expect("ASYNC").benchmark(1, 100, 10000);
  }