Ejemplo 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);
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {

    Map<String, String> cfg = U.map("title", "App", "user", "u1", "theme", "x");

    On.get("/cfg").json(cfg);

    PageMenu menu = PageMenu.from(U.map("Home", "/", "Groups", "/group"));

    On.defaultWrap((data, next) -> next.invoke(x -> page(x).menu(menu).title(cfg.get("title"))));

    List<Btn> letters = Flow.chars('a', 'z').map(c -> btn(c).linkTo("/find?p=" + c)).toList();

    On.page("/")
        .gui("p", p -> U.list(letters, grid(cfg).keyView(k -> a(k).href("/edit?key=" + k))));

    On.page("/group")
        .gui(
            "x",
            x -> {
              return Do.map(Do.group(cfg).by((k, v) -> k.charAt(0)))
                  .toList((k, v) -> U.list(h3(k), grid(v)));
            });

    On.page("/find")
        .gui("p", p -> U.list(letters, grid(Find.allOf(cfg).where((k, v) -> k.startsWith(p)))));

    Btn ok = btn("Update").onClick(() -> Log.info("Updated", "cfg", cfg));

    On.page("/edit").gui("key", key -> edit(cfg, key).buttons(ok, btn("Back").linkTo("/")));

    Runnable replicate = () -> REST.get("http://localhost:8888/cfg", Object.class, U::print);

    Jobs.scheduleAtFixedRate(replicate, 3, 3, TimeUnit.SECONDS);
  }
Ejemplo n.º 3
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);
  }
Ejemplo n.º 4
0
  @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);
    }
  }
Ejemplo n.º 5
0
 public static <T> void call(Callback<T> callback, T result, Throwable error) {
   Jobs.execute(new CallbackExecutorJob<T>(callback, result, error));
 }