Skip to content

Instantly share code, notes, and snippets.

@cevaris
Created July 31, 2015 13:17
Show Gist options
  • Save cevaris/eb9bd434237256432fae to your computer and use it in GitHub Desktop.
Save cevaris/eb9bd434237256432fae to your computer and use it in GitHub Desktop.
package com.nam.thrift.integration;
import junit.framework.TestCase;
import org.apache.thrift.TProcessor;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import java.net.ServerSocket;
/**
* For more on Thrift server config
* https://github.com/apache/thrift/blob/master/lib/java/test/org/apache/thrift/test/TestServer.java
* http://www.programcreek.com/java-api-examples/index.php?api=org.apache.thrift.server.TThreadPoolServer
*/
public abstract class ThriftServerTestCase<S extends TProcessor> extends TestCase {
private TServer thriftServer;
/**
* Port number which the test thrift server is running. This would change for every test.
* Need to update client to read from this port for every test.
*/
protected int testPort;
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
if (thriftServer != null) {
thriftServer.stop();
Thread.sleep(100);
}
}
protected void initThriftServer(S thriftProcessor) throws Exception {
ServerSocket t = new ServerSocket(0);
testPort = t.getLocalPort();
thriftServer = new TSimpleServer(thriftProcessor, new TServerSocket(t));
new Thread(new Runnable() {
@Override
public void run() {
thriftServer.serve();
}
}).start();
Thread.sleep(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment