|
| 1 | +package em.embedded.org.grpcncs; |
| 2 | + |
| 3 | +import io.grpc.*; |
| 4 | +import org.evomaster.client.java.controller.EmbeddedSutController; |
| 5 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 6 | +import org.evomaster.client.java.controller.api.dto.AuthenticationDto; |
| 7 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 8 | +import org.evomaster.client.java.controller.api.dto.problem.rpc.RPCType; |
| 9 | +import org.evomaster.client.java.controller.internal.db.DbSpecification; |
| 10 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 11 | +import org.evomaster.client.java.controller.problem.RPCProblem; |
| 12 | +import org.grpc.ncs.NcsServiceImplBaseImpl; |
| 13 | +import org.grpc.ncs.generated.NcsServiceGrpc; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.List; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +public class EmbeddedEvoMasterController extends EmbeddedSutController { |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + |
| 23 | + int port = 40100; |
| 24 | + if (args.length > 0) { |
| 25 | + port = Integer.parseInt(args[0]); |
| 26 | + } |
| 27 | + |
| 28 | + EmbeddedEvoMasterController controller = new EmbeddedEvoMasterController(port); |
| 29 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 30 | + |
| 31 | + starter.start(); |
| 32 | + } |
| 33 | + |
| 34 | + public EmbeddedEvoMasterController() { |
| 35 | + this(40100); |
| 36 | + } |
| 37 | + |
| 38 | + public EmbeddedEvoMasterController(int port) { |
| 39 | + registeredService = new NcsServiceImplBaseImpl(); |
| 40 | + setControllerPort(port); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + protected ManagedChannel channel; |
| 46 | + private Server server; |
| 47 | + |
| 48 | + private NcsServiceGrpc.NcsServiceBlockingStub stub; |
| 49 | + |
| 50 | + private final BindableService registeredService; |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean isSutRunning() { |
| 54 | + return server != null && !server.isShutdown() && !server.isTerminated(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getPackagePrefixesToCover() { |
| 59 | + return "org.grpc.ncs."; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ProblemInfo getProblemInfo() { |
| 69 | + return new RPCProblem(NcsServiceGrpc.NcsServiceBlockingStub.class, stub, RPCType.gRPC); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 74 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_4; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String startSut() { |
| 79 | + |
| 80 | + try { |
| 81 | + server = ServerBuilder.forPort(0).addService(registeredService).build(); |
| 82 | + server.start(); |
| 83 | + |
| 84 | + startClient(); |
| 85 | + return "http://localhost:"+server.getPort(); |
| 86 | + |
| 87 | + } catch (IOException e) { |
| 88 | + throw new RuntimeException(e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private String startClient() { |
| 93 | + channel = ManagedChannelBuilder.forAddress("localhost", getSutPort()).usePlaintext().build(); |
| 94 | + stub = NcsServiceGrpc.newBlockingStub(channel); |
| 95 | + |
| 96 | + |
| 97 | + return "started:"+!(channel.isShutdown() || channel.isTerminated()); |
| 98 | + } |
| 99 | + |
| 100 | + protected int getSutPort() { |
| 101 | + return server.getPort(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void stopSut() { |
| 106 | + |
| 107 | + try { |
| 108 | + if (channel != null) |
| 109 | + channel.shutdown().awaitTermination(2, TimeUnit.SECONDS); |
| 110 | + if (server != null) |
| 111 | + server.shutdown().awaitTermination(2, TimeUnit.SECONDS); |
| 112 | + |
| 113 | + server = null; |
| 114 | + } catch (InterruptedException e) { |
| 115 | + throw new RuntimeException(e); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void resetStateOfSUT() { |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public List<DbSpecification> getDbSpecifications() { |
| 127 | + return null; |
| 128 | + } |
| 129 | +} |
0 commit comments