public Application build() { Map<String, XioServer> servers = new HashMap<>(); serverBootstraps.forEach((k, v) -> servers.put(k, v.build())); ZkClient zkClient = state.getZkClient(); zkClient.registerUpdater( new IpFilterConfig.Updater(config.getIpFilterPath(), state::setIpFilterConfig)); zkClient.registerUpdater( new Http1FilterConfig.Updater(config.getHttp1FilterPath(), state::setHttp1FilterConfig)); state.getZkClient().start(); Configurator configurator = Configurator.build(config.settings()); configurator.start(); Application application = new Application(config, servers, state, configurator); new ApplicationRunner(application).start(); return application; } }
@Test public void testEchoServerLargePayload() { XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testEchoServer").addToPipeline(new XioEchoPipeline()); try (XioServer server = bootstrap.build(); EchoClient client = new EchoClient()) { client.connect(server.getInstrumentation().addressBound()); int n = 800; String payload = "Netty rocks!"; StringBuilder builder = new StringBuilder(n * payload.length()); for (int i = 0; i < n; i += 1) { builder.append(payload); } payload = builder.toString(); client.send(payload); String response = client.recv(); assertEquals(payload, response); } } }
try (XioServer server = bootstrap.build()) { InetSocketAddress address = server.getInstrumentation().addressBound(); Response response = ClientHelper.http(address);
@Test public void buildHttp11Server() { XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testHttpsServer").addToPipeline(new SmartHttpPipeline()); XioServer server = bootstrap.build(); assertEquals("ssl-http/1.1", server.getInstrumentation().applicationProtocol()); } }
@Test public void testEchoServerLargePayload() { try (EchoClient client = new EchoClient(); EchoServer server = new EchoServer()) { server.bind(new InetSocketAddress("127.0.0.1", 0)); XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testTcpProxyServer") .addToPipeline(new XioTcpProxyPipeline(server.addressBound())); try (XioServer proxy = bootstrap.build()) { client.connect(proxy.getInstrumentation().addressBound()); server.accept(); int n = 800; String payload = "Netty rocks!"; StringBuilder builder = new StringBuilder(n * payload.length()); for (int i = 0; i < n; i += 1) { builder.append(payload); } payload = builder.toString(); client.send(payload); String line = server.process(); String response = client.recv(); assertEquals(payload, line); assertEquals(payload, response); } } } }
@Test public void testServe404() { XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testHttpServer") .addToPipeline( new SmartHttpPipeline( new XioPipelineFragment() { public String applicationProtocol() { return ""; } public void buildHandlers( ApplicationState appState, XioServerConfig config, XioServerState state, ChannelPipeline pipeline) { pipeline.addLast(new XioHttp404Handler()); } })); try (XioServer server = bootstrap.build()) { Response response = ClientHelper.http(server.getInstrumentation().addressBound()); assertEquals(404, response.code()); assertEquals(server.getInstrumentation() != null, true); } } }
@Test public void testEchoServer() { XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testEchoServer").addToPipeline(new XioEchoPipeline()); try (XioServer server = bootstrap.build(); EchoClient client = new EchoClient()) { client.connect(server.getInstrumentation().addressBound()); String payload = "test message"; client.send(payload); String response = client.recv(); assertEquals(payload, response); } }
XioServer xioServer = bootstrap.build(); GrpcClient client = GrpcClient.run(xioServer.getPort());
@Test public void testProxyServer() { try (EchoClient client = new EchoClient(); EchoServer server = new EchoServer()) { server.bind(new InetSocketAddress("127.0.0.1", 0)); XioServerBootstrap bootstrap = XioServerBootstrap.fromConfig("xio.testTcpProxyServer") .addToPipeline(new XioTcpProxyPipeline(server.addressBound())); try (XioServer proxy = bootstrap.build()) { client.connect(proxy.getInstrumentation().addressBound()); server.accept(); String payload = "test message"; client.send(payload); String line = server.process(); String response = client.recv(); assertEquals(payload, line); assertEquals(payload, response); } } }
XioServer xioServer = bootstrap.build(); GrpcClient client = GrpcClient.run(xioServer.getPort());
XioServer xioServer = bootstrap.build(); GrpcClient client = GrpcClient.run(xioServer.getPort());