WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
155
156
157
158
159
package tech.powerjob.client.test;
 
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import tech.powerjob.client.PowerJobClient;
import tech.powerjob.common.enums.ExecuteType;
import tech.powerjob.common.enums.ProcessorType;
import tech.powerjob.common.enums.TimeExpressionType;
import tech.powerjob.common.request.http.SaveJobInfoRequest;
import tech.powerjob.common.response.InstanceInfoDTO;
import tech.powerjob.common.response.JobInfoDTO;
import tech.powerjob.common.response.ResultDTO;
 
/**
 * Test cases for {@link PowerJobClient}
 *
 * @author tjq
 * @author Echo009
 * @since 2020/4/15
 */
@Slf4j
class TestClient extends ClientInitializer {
 
    public static final long JOB_ID = 1L;
 
    @Test
    void testSaveJob() {
 
        SaveJobInfoRequest newJobInfo = new SaveJobInfoRequest();
        newJobInfo.setId(JOB_ID);
        newJobInfo.setJobName("omsOpenAPIJobccccc" + System.currentTimeMillis());
        newJobInfo.setJobDescription("test OpenAPI" + System.currentTimeMillis());
        newJobInfo.setJobParams("{'aa':'bb'}");
        newJobInfo.setTimeExpressionType(TimeExpressionType.CRON);
        newJobInfo.setTimeExpression("0 0 * * * ? ");
        newJobInfo.setExecuteType(ExecuteType.STANDALONE);
        newJobInfo.setProcessorType(ProcessorType.BUILT_IN);
        newJobInfo.setProcessorInfo("tech.powerjob.samples.processors.StandaloneProcessorDemo");
        newJobInfo.setDesignatedWorkers("");
 
        newJobInfo.setMinCpuCores(1.1);
        newJobInfo.setMinMemorySpace(1.2);
        newJobInfo.setMinDiskSpace(1.3);
 
        log.info("[TestClient] [testSaveJob] SaveJobInfoRequest: {}", JSONObject.toJSONString(newJobInfo));
 
        ResultDTO<Long> resultDTO = powerJobClient.saveJob(newJobInfo);
        log.info("[TestClient] [testSaveJob] result: {}", JSONObject.toJSONString(resultDTO));
        Assertions.assertNotNull(resultDTO);
    }
 
    @Test
    void testCopyJob() {
        ResultDTO<Long> copyJobRes = powerJobClient.copyJob(JOB_ID);
        System.out.println(JSONObject.toJSONString(copyJobRes));
        Assertions.assertNotNull(copyJobRes);
    }
 
    @Test
    void testExportJob() {
        ResultDTO<SaveJobInfoRequest> exportJobRes = powerJobClient.exportJob(JOB_ID);
        System.out.println(JSONObject.toJSONString(exportJobRes));
    }
 
    @Test
    void testFetchJob() {
        ResultDTO<JobInfoDTO> fetchJob = powerJobClient.fetchJob(JOB_ID);
        System.out.println(JSONObject.toJSONString(fetchJob));
        Assertions.assertNotNull(fetchJob);
    }
 
    @Test
    void testDisableJob() {
        ResultDTO<Void> res = powerJobClient.disableJob(JOB_ID);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
 
    @Test
    void testEnableJob() {
        ResultDTO<Void> res = powerJobClient.enableJob(JOB_ID);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
 
    @Test
    void testDeleteJob() {
        ResultDTO<Void> res = powerJobClient.deleteJob(JOB_ID);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
 
    @Test
    void testRun() {
        ResultDTO<Long> res = powerJobClient.runJob(JOB_ID, null, 0);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
 
    @Test
    void testRunJobDelay() {
        ResultDTO<Long> res = powerJobClient.runJob(JOB_ID, "this is instanceParams", 60000);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
 
    @Test
    void testFetchInstanceInfo() {
        ResultDTO<InstanceInfoDTO> res = powerJobClient.fetchInstanceInfo(702482902331424832L);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
 
    @Test
    void testStopInstance() {
        ResultDTO<Void> res = powerJobClient.stopInstance(702482902331424832L);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
 
    @Test
    void testFetchInstanceStatus() {
        ResultDTO<Integer> res = powerJobClient.fetchInstanceStatus(702482902331424832L);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
 
    @Test
    void testCancelInstanceInTimeWheel() {
        ResultDTO<Long> startRes = powerJobClient.runJob(JOB_ID, "start by OhMyClient", 20000);
        System.out.println("runJob result: " + JSONObject.toJSONString(startRes));
        ResultDTO<Void> cancelRes = powerJobClient.cancelInstance(startRes.getData());
        System.out.println("cancelJob result: " + JSONObject.toJSONString(cancelRes));
        Assertions.assertTrue(cancelRes.isSuccess());
    }
 
//    @Test
//    @SneakyThrows
//    void testCancelInstanceInDatabase() {
//        ResultDTO<Long> startRes = powerJobClient.runJob(15L, "start by OhMyClient", 2000000);
//        System.out.println("runJob result: " + JSONObject.toJSONString(startRes));
//
//        // Restart server manually and clear all the data in time wheeler.
//        TimeUnit.MINUTES.sleep(1);
//
//        ResultDTO<Void> cancelRes = powerJobClient.cancelInstance(startRes.getData());
//        System.out.println("cancelJob result: " + JSONObject.toJSONString(cancelRes));
//        Assertions.assertTrue(cancelRes.isSuccess());
//    }
 
    @Test
    void testRetryInstance() {
        ResultDTO<Void> res = powerJobClient.retryInstance(169557545206153344L);
        System.out.println(res);
        Assertions.assertNotNull(res);
    }
}