WencongLiu commented on code in PR #20418: URL: https://github.com/apache/flink/pull/20418#discussion_r936266416
########## flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/rest/SessionCaseITTest.java: ########## @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.gateway.rest; + +import org.apache.flink.runtime.rest.messages.EmptyMessageParameters; +import org.apache.flink.runtime.rest.messages.EmptyRequestBody; +import org.apache.flink.runtime.rest.messages.EmptyResponseBody; +import org.apache.flink.table.gateway.api.session.SessionHandle; +import org.apache.flink.table.gateway.api.utils.SqlGatewayException; +import org.apache.flink.table.gateway.rest.handler.AbstractSqlGatewayRestHandler; +import org.apache.flink.table.gateway.rest.header.session.CloseSessionHeaders; +import org.apache.flink.table.gateway.rest.header.session.GetSessionConfigHeaders; +import org.apache.flink.table.gateway.rest.header.session.OpenSessionHeaders; +import org.apache.flink.table.gateway.rest.header.session.TriggerSessionHeartbeatHeaders; +import org.apache.flink.table.gateway.rest.message.session.CloseSessionResponseBody; +import org.apache.flink.table.gateway.rest.message.session.GetSessionConfigResponseBody; +import org.apache.flink.table.gateway.rest.message.session.OpenSessionRequestBody; +import org.apache.flink.table.gateway.rest.message.session.OpenSessionResponseBody; +import org.apache.flink.table.gateway.rest.message.session.SessionMessageParameters; +import org.apache.flink.table.gateway.service.session.Session; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static org.apache.flink.table.gateway.rest.handler.session.CloseSessionHandler.CLOSE_MESSAGE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * Test basic logic of handlers inherited from {@link AbstractSqlGatewayRestHandler} in session + * related cases. + */ +class SessionCaseITTest extends RestAPIITTestBase { + + private static final String SESSION_NAME = "test"; + private static final Map<String, String> properties = new HashMap<>(); + private static final int SESSION_NUMBER = 10; + + static { + properties.put("k1", "v1"); + properties.put("k2", "v2"); + } + + OpenSessionHeaders openSessionHeaders = OpenSessionHeaders.getInstance(); + OpenSessionRequestBody openSessionRequestBody = + new OpenSessionRequestBody(SESSION_NAME, properties); + EmptyMessageParameters emptyParameters = EmptyMessageParameters.getInstance(); + + CloseSessionHeaders closeSessionHeaders = CloseSessionHeaders.getInstance(); + EmptyRequestBody emptyRequestBody = EmptyRequestBody.getInstance(); + SessionMessageParameters sessionMessageParameters; + + @Test + void testWhenCreateMultiSessions() throws Exception { + Set<String> sessionHandleIds = new HashSet<>(); + for (int num = 0; num < SESSION_NUMBER; ++num) { + CompletableFuture<OpenSessionResponseBody> response = + restClientUtils.sendRequest( + openSessionHeaders, emptyParameters, openSessionRequestBody); + String sessionHandleId = response.get().getSessionHandle(); + sessionHandleIds.add(sessionHandleId); + assertThat(sessionHandleId).isNotNull(); + assertThat( + SQL_GATEWAY_SERVICE_EXTENSION + .getSessionManager() + .getSession( + new SessionHandle(UUID.fromString(sessionHandleId)))) + .isNotNull(); + } + assertThat(sessionHandleIds).hasSize(SESSION_NUMBER); + } + + @Test + void testWhenCreateAndCloseSessions() throws Exception { + List<SessionHandle> sessionHandles = new ArrayList<>(); + for (int num = 0; num < SESSION_NUMBER; ++num) { + CompletableFuture<OpenSessionResponseBody> response = + restClientUtils.sendRequest( + openSessionHeaders, emptyParameters, openSessionRequestBody); + String sessionHandleId = response.get().getSessionHandle(); + assertThat(sessionHandleId).isNotNull(); + SessionHandle sessionHandle = new SessionHandle(UUID.fromString(sessionHandleId)); + assertThat(SQL_GATEWAY_SERVICE_EXTENSION.getSessionManager().getSession(sessionHandle)) + .isNotNull(); + sessionHandles.add(sessionHandle); + } + + for (int num = 0; num < SESSION_NUMBER; ++num) { + SessionHandle sessionHandle = sessionHandles.get(num); + sessionMessageParameters = new SessionMessageParameters(sessionHandle); + CompletableFuture<CloseSessionResponseBody> response = + restClientUtils.sendRequest( + closeSessionHeaders, sessionMessageParameters, emptyRequestBody); + String status = response.get().getStatus(); + assertThat(status).isEqualTo(CLOSE_MESSAGE); + assertThatThrownBy( + () -> + SQL_GATEWAY_SERVICE_EXTENSION + .getSessionManager() + .getSession(sessionHandle)) + .isInstanceOf(SqlGatewayException.class); + // Test closing the closed session + CompletableFuture<CloseSessionResponseBody> response2 = + restClientUtils.sendRequest( + closeSessionHeaders, sessionMessageParameters, emptyRequestBody); + assertThatThrownBy(() -> response2.get()).isInstanceOf(ExecutionException.class); Review Comment: Done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org