TaskCallbackServiceTest.java 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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.dolphinscheduler.server.worker.processor;

import io.netty.channel.Channel;
20
import org.apache.dolphinscheduler.common.thread.Stopper;
21
import org.apache.dolphinscheduler.common.utils.JSONUtils;
22 23 24 25
import org.apache.dolphinscheduler.remote.NettyRemotingClient;
import org.apache.dolphinscheduler.remote.NettyRemotingServer;
import org.apache.dolphinscheduler.remote.command.CommandType;
import org.apache.dolphinscheduler.remote.command.TaskExecuteAckCommand;
26
import org.apache.dolphinscheduler.remote.command.TaskExecuteRequestCommand;
27
import org.apache.dolphinscheduler.remote.command.TaskExecuteResponseCommand;
28 29 30
import org.apache.dolphinscheduler.remote.config.NettyClientConfig;
import org.apache.dolphinscheduler.remote.config.NettyServerConfig;
import org.apache.dolphinscheduler.remote.utils.Host;
31
import org.apache.dolphinscheduler.server.entity.TaskExecutionContext;
32 33
import org.apache.dolphinscheduler.server.master.config.MasterConfig;
import org.apache.dolphinscheduler.server.master.processor.TaskAckProcessor;
34 35
import org.apache.dolphinscheduler.server.master.processor.TaskResponseProcessor;
import org.apache.dolphinscheduler.server.master.processor.queue.TaskResponseService;
36 37 38 39 40 41
import org.apache.dolphinscheduler.server.master.registry.MasterRegistry;
import org.apache.dolphinscheduler.server.registry.ZookeeperNodeManager;
import org.apache.dolphinscheduler.server.registry.ZookeeperRegistryCenter;
import org.apache.dolphinscheduler.server.worker.config.WorkerConfig;
import org.apache.dolphinscheduler.server.worker.registry.WorkerRegistry;
import org.apache.dolphinscheduler.server.zk.SpringZKServer;
42
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import org.apache.dolphinscheduler.service.zk.ZookeeperCachedOperator;
import org.apache.dolphinscheduler.service.zk.ZookeeperConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Date;

/**
 * test task call back service
 */
@RunWith(SpringJUnit4ClassRunner.class)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
@ContextConfiguration(classes={
        TaskCallbackServiceTestConfig.class,
        SpringZKServer.class,
        SpringApplicationContext.class,
        MasterRegistry.class,
        WorkerRegistry.class,
        ZookeeperRegistryCenter.class,
        MasterConfig.class,
        WorkerConfig.class,
        ZookeeperCachedOperator.class,
        ZookeeperConfig.class,
        ZookeeperNodeManager.class,
        TaskCallbackService.class,
        TaskResponseService.class,
        TaskAckProcessor.class,
        TaskResponseProcessor.class,
        TaskExecuteProcessor.class})
75 76 77 78 79 80 81 82
public class TaskCallbackServiceTest {

    @Autowired
    private TaskCallbackService taskCallbackService;

    @Autowired
    private MasterRegistry masterRegistry;

83 84 85 86 87 88
    @Autowired
    private TaskAckProcessor taskAckProcessor;

    @Autowired
    private TaskResponseProcessor taskResponseProcessor;

89 90 91
    @Autowired
    private TaskExecuteProcessor taskExecuteProcessor;

92 93 94 95
    /**
     * send ack test
     * @throws Exception
     */
96
    @Test
97
    public void testSendAck() throws Exception{
98 99 100
        final NettyServerConfig serverConfig = new NettyServerConfig();
        serverConfig.setListenPort(30000);
        NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(serverConfig);
101
        nettyRemotingServer.registerProcessor(CommandType.TASK_EXECUTE_ACK, taskAckProcessor);
102 103 104 105 106 107 108 109 110 111 112
        nettyRemotingServer.start();

        final NettyClientConfig clientConfig = new NettyClientConfig();
        NettyRemotingClient nettyRemotingClient = new NettyRemotingClient(clientConfig);
        Channel channel = nettyRemotingClient.getChannel(Host.of("localhost:30000"));
        taskCallbackService.addRemoteChannel(1, new NettyRemoteChannel(channel, 1));
        TaskExecuteAckCommand ackCommand = new TaskExecuteAckCommand();
        ackCommand.setTaskInstanceId(1);
        ackCommand.setStartTime(new Date());
        taskCallbackService.sendAck(1, ackCommand.convert2Command());

113 114
        Thread.sleep(5000);

115 116
        Stopper.stop();

117 118
        Thread.sleep(5000);

119 120 121 122
        nettyRemotingServer.close();
        nettyRemotingClient.close();
    }

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    /**
     * send result test
     * @throws Exception
     */
    @Test
    public void testSendResult() throws Exception{
        final NettyServerConfig serverConfig = new NettyServerConfig();
        serverConfig.setListenPort(30000);
        NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(serverConfig);
        nettyRemotingServer.registerProcessor(CommandType.TASK_EXECUTE_RESPONSE, taskResponseProcessor);
        nettyRemotingServer.start();

        final NettyClientConfig clientConfig = new NettyClientConfig();
        NettyRemotingClient nettyRemotingClient = new NettyRemotingClient(clientConfig);
        Channel channel = nettyRemotingClient.getChannel(Host.of("localhost:30000"));
        taskCallbackService.addRemoteChannel(1, new NettyRemoteChannel(channel, 1));
        TaskExecuteResponseCommand responseCommand  = new TaskExecuteResponseCommand();
        responseCommand.setTaskInstanceId(1);
        responseCommand.setEndTime(new Date());

        taskCallbackService.sendResult(1, responseCommand.convert2Command());

        Thread.sleep(5000);

        Stopper.stop();

        Thread.sleep(5000);

        nettyRemotingServer.close();
        nettyRemotingClient.close();
    }

155 156 157 158
    @Test(expected = IllegalArgumentException.class)
    public void testSendAckWithIllegalArgumentException(){
        TaskExecuteAckCommand ackCommand = Mockito.mock(TaskExecuteAckCommand.class);
        taskCallbackService.sendAck(1, ackCommand.convert2Command());
159
        Stopper.stop();
160 161
    }

162 163
    @Test(expected = IllegalStateException.class)
    public void testSendAckWithIllegalStateException1(){
164
        masterRegistry.registry();
165 166 167
        final NettyServerConfig serverConfig = new NettyServerConfig();
        serverConfig.setListenPort(30000);
        NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(serverConfig);
168
        nettyRemotingServer.registerProcessor(CommandType.TASK_EXECUTE_ACK, taskAckProcessor);
169 170 171 172 173 174
        nettyRemotingServer.start();

        final NettyClientConfig clientConfig = new NettyClientConfig();
        NettyRemotingClient nettyRemotingClient = new NettyRemotingClient(clientConfig);
        Channel channel = nettyRemotingClient.getChannel(Host.of("localhost:30000"));
        taskCallbackService.addRemoteChannel(1, new NettyRemoteChannel(channel, 1));
175
        channel.close();
176 177 178 179
        TaskExecuteAckCommand ackCommand = new TaskExecuteAckCommand();
        ackCommand.setTaskInstanceId(1);
        ackCommand.setStartTime(new Date());

180
        nettyRemotingServer.close();
181

182 183 184 185 186 187
        taskCallbackService.sendAck(1, ackCommand.convert2Command());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
188 189 190

        Stopper.stop();

191 192 193 194 195
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
196 197
    }

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    @Test
    public void testTaskExecuteProcessor() throws Exception{
        final NettyServerConfig serverConfig = new NettyServerConfig();
        serverConfig.setListenPort(30000);
        NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(serverConfig);
        nettyRemotingServer.registerProcessor(CommandType.TASK_EXECUTE_REQUEST, taskExecuteProcessor);
        nettyRemotingServer.start();

        final NettyClientConfig clientConfig = new NettyClientConfig();
        NettyRemotingClient nettyRemotingClient = new NettyRemotingClient(clientConfig);

        TaskExecuteRequestCommand taskExecuteRequestCommand = new TaskExecuteRequestCommand();

        nettyRemotingClient.send(new Host("localhost",30000),taskExecuteRequestCommand.convert2Command());

        taskExecuteRequestCommand.setTaskExecutionContext(JSONUtils.toJsonString(new TaskExecutionContext()));

        nettyRemotingClient.send(new Host("localhost",30000),taskExecuteRequestCommand.convert2Command());

        Thread.sleep(5000);

        Stopper.stop();

        Thread.sleep(5000);

        nettyRemotingServer.close();
        nettyRemotingClient.close();
    }

_和's avatar
_和 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
//    @Test(expected = IllegalStateException.class)
//    public void testSendAckWithIllegalStateException2(){
//        masterRegistry.registry();
//        final NettyServerConfig serverConfig = new NettyServerConfig();
//        serverConfig.setListenPort(30000);
//        NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(serverConfig);
//        nettyRemotingServer.registerProcessor(CommandType.TASK_EXECUTE_ACK, taskAckProcessor);
//        nettyRemotingServer.start();
//
//        final NettyClientConfig clientConfig = new NettyClientConfig();
//        NettyRemotingClient nettyRemotingClient = new NettyRemotingClient(clientConfig);
//        Channel channel = nettyRemotingClient.getChannel(Host.of("localhost:30000"));
//        taskCallbackService.addRemoteChannel(1, new NettyRemoteChannel(channel, 1));
//        channel.close();
//        TaskExecuteAckCommand ackCommand = new TaskExecuteAckCommand();
//        ackCommand.setTaskInstanceId(1);
//        ackCommand.setStartTime(new Date());
//
//        nettyRemotingServer.close();
//
//        taskCallbackService.sendAck(1, ackCommand.convert2Command());
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        Stopper.stop();
//
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }
262 263

}