@Override protected void configure(ServerBuilder sb) throws Exception { sb.service("/hello", THttpService.of(helloService)); } };
@Override protected void configure(ServerBuilder sb) throws Exception { sb.service("/hello", THttpService.of(HELLO_SERVICE)) .service("/hellobinaryonly", THttpService.ofFormats(HELLO_SERVICE, BINARY)) .service("/hellotextonly", THttpService.ofFormats(HELLO_SERVICE, TEXT)); } };
@Override protected void configure(ServerBuilder sb) throws Exception { sb.service("/serverIp", THttpService.of(handler)); } }
@Test public void serviceNames() { assertThat(ThriftServiceUtils.serviceNames(THttpService.of(asyncService))) .isEqualTo(ImmutableSet.of(SERVICE_NAME)); assertThat(ThriftServiceUtils.serviceNames(THttpService.of(ImmutableMap.of("async", asyncService, "sync", syncService)))) .isEqualTo(ImmutableSet.of(SERVICE_NAME)); } }
@Override protected void configure(ServerBuilder sb) throws Exception { loggingService = new MockedStructuredLoggingService<>( THttpService.of((HelloService.Iface) name -> "Hello " + name)); sb.service("/hello", loggingService); } };
@Test public void testIdentity_HelloService_hello() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService syncService = THttpService.of( (HelloService.Iface) name -> "Hello, " + name + '!', defaultSerializationFormat); final THttpService asyncService = THttpService.of( (HelloService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete("Hello, " + name + '!'), defaultSerializationFormat); invokeTwice(syncService, asyncService); assertThat(promise.get()).isEqualTo(promise2.get()); }
@Test public void testSync_OnewayHelloService_hello() throws Exception { final AtomicReference<String> actualName = new AtomicReference<>(); final OnewayHelloService.Client client = new OnewayHelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (OnewayHelloService.Iface) actualName::set, defaultSerializationFormat); invoke(service); assertThat(promise.get().isEmpty()).isTrue(); assertThat(actualName.get()).isEqualTo(FOO); }
@Test public void testSync_NameSortService_sort() throws Exception { final NameSortService.Client client = new NameSortService.Client.Factory().getClient(inProto, outProto); client.send_sort(Arrays.asList(NAME_C, NAME_B, NAME_A)); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of((NameSortService.Iface) names -> { final ArrayList<Name> sorted = new ArrayList<>(names); Collections.sort(sorted); return sorted; }, defaultSerializationFormat); invoke(service); assertThat(client.recv_sort()).containsExactly(NAME_A, NAME_B, NAME_C); }
@Test public void testAsync_NameSortService_sort() throws Exception { final NameSortService.Client client = new NameSortService.Client.Factory().getClient(inProto, outProto); client.send_sort(Arrays.asList(NAME_C, NAME_B, NAME_A)); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of((NameSortService.AsyncIface) (names, resultHandler) -> { final ArrayList<Name> sorted = new ArrayList<>(names); Collections.sort(sorted); resultHandler.onComplete(sorted); }, defaultSerializationFormat); invoke(service); assertThat(client.recv_sort()).containsExactly(NAME_A, NAME_B, NAME_C); }
@Override protected void configure(ServerBuilder sb) throws Exception { sb.meterRegistry(registry); sb.service("/helloservice", THttpService.of((Iface) name -> { if ("world".equals(name)) { return "success"; } throw new IllegalArgumentException("bad argument"); }).decorate(MetricCollectingService.newDecorator( MeterIdPrefixFunction.ofDefault("armeria.server.HelloService")))); } };
@Test public void testSync_DevNullService_consume() throws Exception { final AtomicReference<String> consumed = new AtomicReference<>(); final DevNullService.Client client = new DevNullService.Client.Factory().getClient(inProto, outProto); client.send_consume(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (DevNullService.Iface) consumed::set, defaultSerializationFormat); invoke(service); assertThat(consumed.get()).isEqualTo(FOO); client.recv_consume(); }
@Test public void testSync_HelloService_hello_with_null() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(null); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (HelloService.Iface) name -> String.valueOf(name != null), defaultSerializationFormat); invoke(service); assertThat(client.recv_hello()).isEqualTo("false"); }
@Test public void testAsync_HelloService_hello_with_null() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(null); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (HelloService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete(String.valueOf(name != null)), defaultSerializationFormat); invoke(service); assertThat(client.recv_hello()).isEqualTo("false"); }
@Test public void testSync_HelloService_hello() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (HelloService.Iface) name -> "Hello, " + name + '!', defaultSerializationFormat); invoke(service); assertThat(client.recv_hello()).isEqualTo("Hello, foo!"); }
@Test public void testAsync_HelloService_hello() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (HelloService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete("Hello, " + name + '!'), defaultSerializationFormat); invoke(service); assertThat(client.recv_hello()).isEqualTo("Hello, foo!"); }
@Bean public ThriftServiceRegistrationBean helloThriftService() { return new ThriftServiceRegistrationBean() .setServiceName("helloService") .setService(THttpService.of((HelloService.Iface) name -> "hello " + name)) .setPath("/thrift") .setDecorators(ImmutableList.of(LoggingService.newDecorator())) .setExampleRequests(Collections.singleton(new hello_args("nameVal"))) .setExampleHeaders(Collections.singleton(HttpHeaders.of( HttpHeaderNames.of("x-additional-header"), "headerVal"))); } }
@Test public void testSync_NameService_removeMiddle() throws Exception { final NameService.Client client = new NameService.Client.Factory().getClient(inProto, outProto); client.send_removeMiddle(new Name(BAZ, BAR, FOO)); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (NameService.Iface) name -> new Name(name.first, null, name.last), defaultSerializationFormat); invoke(service); assertThat(client.recv_removeMiddle()).isEqualTo(new Name(BAZ, null, FOO)); }
@Test public void testAsync_NameService_removeMiddle() throws Exception { final NameService.Client client = new NameService.Client.Factory().getClient(inProto, outProto); client.send_removeMiddle(new Name(BAZ, BAR, FOO)); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (NameService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete(new Name(name.first, null, name.last)), defaultSerializationFormat); invoke(service); assertThat(client.recv_removeMiddle()).isEqualTo(new Name(BAZ, null, FOO)); }
@Override protected void configure(ServerBuilder sb) throws Exception { sb.gracefulShutdownTimeout(1000L, 2000L); sb.defaultRequestTimeoutMillis(0); // Disable RequestTimeoutException. sb.service("/sleep", THttpService.of( (AsyncIface) (milliseconds, resultHandler) -> RequestContext.current().eventLoop().schedule( () -> resultHandler.onComplete(milliseconds), milliseconds, MILLISECONDS))); } };