Search in sources :

Example 11 with MockSpannerServiceImpl

use of com.google.cloud.spanner.MockSpannerServiceImpl in project java-spanner-jdbc by googleapis.

the class JdbcGrpcErrorTest method startStaticServer.

@BeforeClass
public static void startStaticServer() throws IOException {
    mockSpanner = new MockSpannerServiceImpl();
    // We don't want any unpredictable aborted transactions.
    mockSpanner.setAbortProbability(0.0D);
    mockSpanner.putStatementResult(StatementResult.query(SELECT1, SELECT1_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
    mockSpanner.putStatementResult(StatementResult.exception(INVALID_UPDATE_STATEMENT, Status.NOT_FOUND.withDescription("Unknown table name").asRuntimeException()));
    MockInstanceAdminImpl mockInstanceAdmin = new MockInstanceAdminImpl();
    MockDatabaseAdminImpl mockDatabaseAdmin = new MockDatabaseAdminImpl();
    InetSocketAddress address = new InetSocketAddress("localhost", 0);
    server = NettyServerBuilder.forAddress(address).addService(mockSpanner).addService(mockInstanceAdmin).addService(mockDatabaseAdmin).build().start();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MockDatabaseAdminImpl(com.google.cloud.spanner.admin.database.v1.MockDatabaseAdminImpl) MockInstanceAdminImpl(com.google.cloud.spanner.admin.instance.v1.MockInstanceAdminImpl) MockSpannerServiceImpl(com.google.cloud.spanner.MockSpannerServiceImpl) BeforeClass(org.junit.BeforeClass)

Example 12 with MockSpannerServiceImpl

use of com.google.cloud.spanner.MockSpannerServiceImpl in project java-spanner by googleapis.

the class AbstractMockServerTest method startStaticServer.

@BeforeClass
public static void startStaticServer() throws IOException {
    mockSpanner = new MockSpannerServiceImpl();
    // We don't want any unpredictable aborted transactions.
    mockSpanner.setAbortProbability(0.0D);
    mockInstanceAdmin = new MockInstanceAdminImpl();
    mockDatabaseAdmin = new MockDatabaseAdminImpl();
    mockOperations = new OperationsImplBase() {

        @Override
        public void getOperation(GetOperationRequest request, StreamObserver<Operation> responseObserver) {
            responseObserver.onNext(Operation.newBuilder().setDone(false).setName(request.getName()).setMetadata(Any.pack(Empty.getDefaultInstance())).build());
            responseObserver.onCompleted();
        }
    };
    address = new InetSocketAddress("localhost", 0);
    server = NettyServerBuilder.forAddress(address).addService(mockSpanner).addService(mockInstanceAdmin).addService(mockDatabaseAdmin).addService(mockOperations).build().start();
    mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_BEFORE_INSERT));
    mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, UPDATE_COUNT));
    mockSpanner.putStatementResult(StatementResult.query(SELECT_RANDOM_STATEMENT, RANDOM_RESULT_SET));
    futureParentHandlers = Logger.getLogger(AbstractFuture.class.getName()).getUseParentHandlers();
    exceptionRunnableParentHandlers = Logger.getLogger(LogExceptionRunnable.class.getName()).getUseParentHandlers();
    nettyServerParentHandlers = Logger.getLogger("io.grpc.netty.shaded.io.grpc.netty.NettyServerHandler").getUseParentHandlers();
    clientStreamParentHandlers = Logger.getLogger("io.grpc.netty.shaded.io.grpc.netty.NettyServerHandler").getUseParentHandlers();
    Logger.getLogger(AbstractFuture.class.getName()).setUseParentHandlers(false);
    Logger.getLogger(LogExceptionRunnable.class.getName()).setUseParentHandlers(false);
    Logger.getLogger("io.grpc.netty.shaded.io.grpc.netty.NettyServerHandler").setUseParentHandlers(false);
    Logger.getLogger("io.grpc.internal.AbstractClientStream").setUseParentHandlers(false);
}
Also used : LogExceptionRunnable(io.grpc.internal.LogExceptionRunnable) GetOperationRequest(com.google.longrunning.GetOperationRequest) InetSocketAddress(java.net.InetSocketAddress) MockDatabaseAdminImpl(com.google.cloud.spanner.admin.database.v1.MockDatabaseAdminImpl) MockInstanceAdminImpl(com.google.cloud.spanner.admin.instance.v1.MockInstanceAdminImpl) Operation(com.google.longrunning.Operation) OperationsImplBase(com.google.longrunning.OperationsGrpc.OperationsImplBase) MockSpannerServiceImpl(com.google.cloud.spanner.MockSpannerServiceImpl) AbstractFuture(com.google.common.util.concurrent.AbstractFuture) BeforeClass(org.junit.BeforeClass)

Example 13 with MockSpannerServiceImpl

use of com.google.cloud.spanner.MockSpannerServiceImpl in project java-spanner by googleapis.

the class GapicSpannerRpcTest method startServer.

@Before
public void startServer() throws IOException {
    assumeTrue("Skip tests when emulator is enabled as this test interferes with the check whether the emulator is running", System.getenv("SPANNER_EMULATOR_HOST") == null);
    defaultUserAgent = "spanner-java/" + GaxProperties.getLibraryVersion(GapicSpannerRpc.class);
    mockSpanner = new MockSpannerServiceImpl();
    // We don't want any unpredictable aborted transactions.
    mockSpanner.setAbortProbability(0.0D);
    mockSpanner.putStatementResult(StatementResult.query(SELECT1AND2, SELECT1_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.update(UPDATE_FOO_STATEMENT, 1L));
    address = new InetSocketAddress("localhost", 0);
    server = NettyServerBuilder.forAddress(address).addService(mockSpanner).intercept(new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            lastSeenHeaders = headers;
            String auth = headers.get(Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER));
            assertThat(auth).isEqualTo("Bearer " + VARIABLE_OAUTH_TOKEN);
            return Contexts.interceptCall(Context.current(), call, headers, next);
        }
    }).build().start();
    optionsMap.put(Option.CHANNEL_HINT, 1L);
    spanner = createSpannerOptions().getService();
}
Also used : ServerCallHandler(io.grpc.ServerCallHandler) ServerCall(io.grpc.ServerCall) InetSocketAddress(java.net.InetSocketAddress) ServerInterceptor(io.grpc.ServerInterceptor) Metadata(io.grpc.Metadata) ResultSetMetadata(com.google.spanner.v1.ResultSetMetadata) MockSpannerServiceImpl(com.google.cloud.spanner.MockSpannerServiceImpl) Before(org.junit.Before)

Example 14 with MockSpannerServiceImpl

use of com.google.cloud.spanner.MockSpannerServiceImpl in project liquibase-spanner by cloudspannerecosystem.

the class AbstractMockServerTest method startStaticServer.

@BeforeAll
static void startStaticServer() throws IOException {
    mockSpanner = new MockSpannerServiceImpl();
    mockSpanner.setAbortProbability(0.0D);
    mockAdmin = new MockDatabaseAdminImpl();
    address = new InetSocketAddress("localhost", 0);
    server = NettyServerBuilder.forAddress(address).addService(mockSpanner).addService(mockAdmin).intercept(new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            // backend is the 'right' database, instead of at least checking the driver first...
            if (!(call.getMethodDescriptor().getFullMethodName().equals("google.spanner.v1.Spanner/BatchCreateSessions") || call.getMethodDescriptor().getFullMethodName().equals("google.spanner.v1.Spanner/ExecuteStreamingSql") || call.getMethodDescriptor().getFullMethodName().equals("google.spanner.v1.Spanner/DeleteSession"))) {
                String clientLibToken = headers.get(Key.of("x-goog-api-client", Metadata.ASCII_STRING_MARSHALLER));
                if (clientLibToken == null || !clientLibToken.contains("sp-liq")) {
                    if (!receivedRequestWithNonLiquibaseToken.getAndSet(true)) {
                        invalidClientLibToken = clientLibToken;
                    }
                }
            }
            return Contexts.interceptCall(Context.current(), call, headers, next);
        }
    }).build().start();
    registerDefaultResults();
}
Also used : ServerCallHandler(io.grpc.ServerCallHandler) ServerCall(io.grpc.ServerCall) InetSocketAddress(java.net.InetSocketAddress) MockDatabaseAdminImpl(com.google.cloud.spanner.admin.database.v1.MockDatabaseAdminImpl) ServerInterceptor(io.grpc.ServerInterceptor) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) Metadata(io.grpc.Metadata) ResultSetMetadata(com.google.spanner.v1.ResultSetMetadata) MockSpannerServiceImpl(com.google.cloud.spanner.MockSpannerServiceImpl) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 15 with MockSpannerServiceImpl

use of com.google.cloud.spanner.MockSpannerServiceImpl in project pgadapter by GoogleCloudPlatform.

the class AbstractMockServerTest method startMockSpannerAndPgAdapterServers.

@BeforeClass
public static void startMockSpannerAndPgAdapterServers() throws Exception {
    mockSpanner = new MockSpannerServiceImpl();
    // We don't want any unpredictable aborted transactions.
    mockSpanner.setAbortProbability(0.0D);
    mockSpanner.putStatementResult(StatementResult.query(SELECT1, SELECT1_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.query(SELECT2, SELECT2_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
    mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, INSERT_COUNT));
    mockSpanner.putStatementResult(MockSpannerServiceImpl.StatementResult.detectDialectResult(Dialect.POSTGRESQL));
    mockOperationsService = new MockOperationsServiceImpl();
    mockDatabaseAdmin = new MockDatabaseAdminServiceImpl(mockOperationsService);
    InetSocketAddress address = new InetSocketAddress("localhost", 0);
    spannerServer = NettyServerBuilder.forAddress(address).addService(mockSpanner).addService(mockDatabaseAdmin).addService(mockOperationsService).build().start();
    // Create the test database on the mock server. This should be replaced by a simple feature in
    // the mock server to just add a database instead of having to simulate the creation of it.
    createDatabase();
    ImmutableList.Builder<String> argsListBuilder = ImmutableList.<String>builder().add("-p", "p", "-i", "i", "-d", "d", "-c", // empty credentials file, as we are using a plain text connection.
    "", "-s", // port 0 to let the OS pick an available port
    "0", "-e", String.format("localhost:%d", spannerServer.getPort()), "-r", "usePlainText=true;");
    String[] args = argsListBuilder.build().toArray(new String[0]);
    pgServer = new ProxyServer(new OptionsMetadata(args));
    pgServer.startServer();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ImmutableList(com.google.common.collect.ImmutableList) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) MockOperationsServiceImpl(com.google.cloud.spanner.MockOperationsServiceImpl) MockDatabaseAdminServiceImpl(com.google.cloud.spanner.MockDatabaseAdminServiceImpl) MockSpannerServiceImpl(com.google.cloud.spanner.MockSpannerServiceImpl) BeforeClass(org.junit.BeforeClass)

Aggregations

MockSpannerServiceImpl (com.google.cloud.spanner.MockSpannerServiceImpl)15 InetSocketAddress (java.net.InetSocketAddress)14 BeforeClass (org.junit.BeforeClass)11 MockDatabaseAdminImpl (com.google.cloud.spanner.admin.database.v1.MockDatabaseAdminImpl)5 ResultSetMetadata (com.google.spanner.v1.ResultSetMetadata)4 Metadata (io.grpc.Metadata)4 ServerCall (io.grpc.ServerCall)4 ServerCallHandler (io.grpc.ServerCallHandler)4 ServerInterceptor (io.grpc.ServerInterceptor)4 MockInstanceAdminImpl (com.google.cloud.spanner.admin.instance.v1.MockInstanceAdminImpl)3 OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)2 ImmutableList (com.google.common.collect.ImmutableList)2 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)2 Before (org.junit.Before)2 MockServiceHelper (com.google.api.gax.grpc.testing.MockServiceHelper)1 MockDatabaseAdminServiceImpl (com.google.cloud.spanner.MockDatabaseAdminServiceImpl)1 MockOperationsServiceImpl (com.google.cloud.spanner.MockOperationsServiceImpl)1 AbstractFuture (com.google.common.util.concurrent.AbstractFuture)1 GetOperationRequest (com.google.longrunning.GetOperationRequest)1 Operation (com.google.longrunning.Operation)1