]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.thrift.server; | |
20 | ||
21 | ||
22 | import org.apache.thrift.TProcessor; | |
23 | import org.apache.thrift.protocol.TProtocol; | |
24 | import org.apache.thrift.protocol.TProtocolFactory; | |
25 | import org.apache.thrift.server.TNonblockingServer.Args; | |
26 | import org.apache.thrift.transport.TFramedTransport; | |
27 | import org.apache.thrift.transport.TNonblockingServerSocket; | |
28 | import org.apache.thrift.transport.TSocket; | |
29 | import org.apache.thrift.transport.TTransport; | |
30 | import org.apache.thrift.transport.TTransportException; | |
31 | import org.apache.thrift.transport.TTransportFactory; | |
32 | ||
33 | import thrift.test.ThriftTest; | |
34 | ||
35 | public class TestNonblockingServer extends ServerTestBase { | |
36 | ||
37 | private Thread serverThread; | |
38 | private TServer server; | |
39 | private static final int NUM_QUERIES = 1000; | |
40 | ||
41 | protected TServer getServer(TProcessor processor, TNonblockingServerSocket socket, TProtocolFactory protoFactory, TTransportFactory factory) { | |
42 | final Args args = new Args(socket).processor(processor).protocolFactory(protoFactory); | |
43 | if (factory != null) { | |
44 | args.transportFactory(factory); | |
45 | } | |
46 | return new TNonblockingServer(args); | |
47 | } | |
48 | ||
49 | @Override | |
50 | public void startServer(final TProcessor processor, final TProtocolFactory protoFactory, final TTransportFactory factory) throws Exception { | |
51 | serverThread = new Thread() { | |
52 | public void run() { | |
53 | try { | |
54 | // Transport | |
55 | TNonblockingServerSocket tServerSocket = | |
56 | new TNonblockingServerSocket(new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs().port(PORT)); | |
57 | ||
58 | server = getServer(processor, tServerSocket, protoFactory, factory); | |
59 | ||
60 | // Run it | |
61 | System.out.println("Starting the server on port " + PORT + "..."); | |
62 | server.serve(); | |
63 | } catch (Exception e) { | |
64 | e.printStackTrace(); | |
65 | fail(); | |
66 | } | |
67 | } | |
68 | }; | |
69 | serverThread.start(); | |
70 | Thread.sleep(1000); | |
71 | } | |
72 | ||
73 | @Override | |
74 | public void stopServer() throws Exception { | |
75 | server.stop(); | |
76 | try { | |
77 | serverThread.join(); | |
78 | } catch (InterruptedException e) {} | |
79 | } | |
80 | ||
81 | @Override | |
82 | public TTransport getClientTransport(TTransport underlyingTransport) throws Exception { | |
83 | return new TFramedTransport(underlyingTransport); | |
84 | } | |
85 | ||
86 | ||
87 | public void testCleanupAllSelectionKeys() throws Exception { | |
88 | for (TProtocolFactory protoFactory : getProtocols()) { | |
89 | TestHandler handler = new TestHandler(); | |
90 | ThriftTest.Processor processor = new ThriftTest.Processor(handler); | |
91 | ||
92 | startServer(processor, protoFactory); | |
93 | ||
94 | TSocket socket = new TSocket(HOST, PORT); | |
95 | socket.setTimeout(SOCKET_TIMEOUT); | |
96 | TTransport transport = getClientTransport(socket); | |
97 | ||
98 | TProtocol protocol = protoFactory.getProtocol(transport); | |
99 | ThriftTest.Client testClient = new ThriftTest.Client(protocol); | |
100 | ||
101 | open(transport); | |
102 | ||
103 | for (int i = 0; i < NUM_QUERIES; ++i) { | |
104 | testClient.testI32(1); | |
105 | } | |
106 | server.stop(); | |
107 | for (int i = 0; i < NUM_QUERIES; ++i) { | |
108 | try { | |
109 | testClient.testI32(1); | |
110 | } catch(TTransportException e) { | |
111 | System.err.println(e); | |
112 | e.printStackTrace(); | |
113 | if (e.getCause() instanceof java.net.SocketTimeoutException) { | |
114 | fail("timed out when it should have thrown another kind of error!"); | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | transport.close(); | |
120 | stopServer(); | |
121 | } | |
122 | } | |
123 | } |