|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.streampark.flink.core; |
| 19 | + |
| 20 | +import org.apache.flink.api.java.utils.ParameterTool; |
| 21 | +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +class FlinkStreamingJobTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void lifecycleExecutesInOrder() throws Exception { |
| 34 | + List<String> trace = new ArrayList<>(); |
| 35 | + |
| 36 | + FlinkStreamingJob job = |
| 37 | + new FlinkStreamingJob() { |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void configure(StreamExecutionEnvironment env, ParameterTool parameter) { |
| 41 | + trace.add("configure"); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void ready() { |
| 46 | + trace.add("ready"); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void handle() { |
| 51 | + trace.add("handle"); |
| 52 | + env.fromElements(1, 2, 3).map(i -> i * 2).print(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void destroy() { |
| 57 | + trace.add("destroy"); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + job.run(new String[0]); |
| 62 | + |
| 63 | + assertThat(trace).containsExactly("configure", "ready", "handle", "destroy"); |
| 64 | + assertThat(job.jobExecutionResult).isNotNull(); |
| 65 | + } |
| 66 | + |
| 67 | + static class NoOpJob extends FlinkStreamingJob { |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void handle() throws FlinkJobException { |
| 71 | + env.fromElements(1).print(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void appNameDefaultsToClassName() throws Exception { |
| 77 | + NoOpJob job = new NoOpJob(); |
| 78 | + job.run(new String[0]); |
| 79 | + assertThat(job.getClass().getSimpleName()).isEqualTo("NoOpJob"); |
| 80 | + assertThat(job.jobExecutionResult).isNotNull(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void parameterToolPropagatedToEnv() throws Exception { |
| 85 | + FlinkStreamingJob job = |
| 86 | + new FlinkStreamingJob() { |
| 87 | + |
| 88 | + @Override |
| 89 | + protected void handle() { |
| 90 | + env.fromElements(1).print(); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + job.run(new String[]{"--app.name", "test-job"}); |
| 95 | + |
| 96 | + assertThat(job.parameter.get("app.name")).isEqualTo("test-job"); |
| 97 | + } |
| 98 | +} |
0 commit comments