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
package tech.powerjob.worker.test;
 
import tech.powerjob.worker.common.constants.StoreStrategy;
import tech.powerjob.worker.common.constants.TaskStatus;
import tech.powerjob.common.utils.NetUtils;
import tech.powerjob.worker.persistence.DbTaskPersistenceService;
import tech.powerjob.worker.persistence.TaskDO;
import tech.powerjob.worker.persistence.TaskPersistenceService;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.*;
 
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
 
import static tech.powerjob.worker.core.tracker.task.heavy.CommonTaskTracker.ROOT_TASK_ID;
 
/**
 * H2 数据库持久化测试
 *
 * @author tjq
 * @since 2020/3/23
 */
public class PersistenceServiceTest {
 
    private static final TaskPersistenceService taskPersistenceService = new DbTaskPersistenceService(StoreStrategy.DISK);
 
    @BeforeAll
    public static void initTable() throws Exception {
        taskPersistenceService.init();
 
        System.out.println("=============== init data ===============");
        List<TaskDO> taskList = Lists.newLinkedList();
        for (int i = 0; i < 10; i++) {
            TaskDO task = new TaskDO();
            taskList.add(task);
 
            long instanceId = 10086L + ThreadLocalRandom.current().nextInt(2);
            task.setSubInstanceId(instanceId);
            task.setInstanceId(instanceId);
            task.setTaskId(i + "");
            task.setFailedCnt(0);
            task.setStatus(TaskStatus.WORKER_RECEIVED.getValue());
            task.setTaskName("ROOT_TASK");
            task.setAddress(NetUtils.getLocalHost4Test());
            task.setLastModifiedTime(System.currentTimeMillis());
            task.setCreatedTime(System.currentTimeMillis());
            task.setLastReportTime(System.currentTimeMillis());
            task.setResult("");
        }
 
        taskPersistenceService.batchSave(taskList);
        taskList.forEach(System.out::println);
    }
 
    @AfterAll
    public static void stop() throws Exception {
        Thread.sleep(60000);
    }
 
 
    @Test
    public void testBatchSave(){
        List<TaskDO> taskList = Lists.newLinkedList();
        long instanceId = 10086L + ThreadLocalRandom.current().nextInt(2);
        for (int i = 0; i < 100; i++) {
            TaskDO task = new TaskDO();
            taskList.add(task);
            task.setSubInstanceId(instanceId);
            task.setInstanceId(instanceId);
            task.setTaskId(ROOT_TASK_ID + "." + i);
            task.setFailedCnt(0);
            task.setStatus(TaskStatus.WORKER_RECEIVED.getValue());
            task.setTaskName("ROOT_TASK");
            task.setAddress(NetUtils.getLocalHost4Test());
            task.setLastModifiedTime(System.currentTimeMillis());
            task.setCreatedTime(System.currentTimeMillis());
            task.setLastReportTime(System.currentTimeMillis());
            task.setResult("");
        }
        TaskDO firstTask = taskList.get(0);
        taskList.add(firstTask);
        taskPersistenceService.batchSave(taskList);
 
    }
 
    @Test
    public void testDeleteAllTasks() {
 
        System.out.println("=============== testBatchDelete ===============");
        boolean delete = taskPersistenceService.deleteAllTasks(100860L);
        System.out.println("delete result:" + delete);
    }
 
    @Test
    public void testUpdateLostTasks() throws Exception {
        Thread.sleep(1000);
        boolean success = taskPersistenceService.updateLostTasks(10086L, Lists.newArrayList(NetUtils.getLocalHost4Test()), true);
        System.out.println("updateLostTasks: " + success);
    }
 
    @Test
    public void testGetAllUnFinishedTaskByAddress() throws Exception {
        System.out.println("=============== testGetAllUnFinishedTaskByAddress ===============");
        List<TaskDO> res = taskPersistenceService.getAllUnFinishedTaskByAddress(10086L, NetUtils.getLocalHost4Test());
        System.out.println(res);
    }
 
}