TaskQueueImplTest.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. package cn.escheduler.common.queue;
  18. import cn.escheduler.common.Constants;
  19. import cn.escheduler.common.utils.IpUtils;
  20. import cn.escheduler.common.utils.OSUtils;
  21. import org.junit.After;
  22. import org.junit.Assert;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import java.util.Arrays;
  28. import java.util.List;
  29. import java.util.Random;
  30. import static org.junit.Assert.assertEquals;
  31. /**
  32. * task queue test
  33. */
  34. public class TaskQueueImplTest {
  35. private static final Logger logger = LoggerFactory.getLogger(TaskQueueImplTest.class);
  36. ITaskQueue tasksQueue = null;
  37. @Before
  38. public void before(){
  39. tasksQueue = TaskQueueFactory.getTaskQueueInstance();
  40. //clear all data
  41. tasksQueue.delete();
  42. }
  43. @After
  44. public void after(){
  45. //clear all data
  46. tasksQueue.delete();
  47. }
  48. @Test
  49. public void testAdd(){
  50. //add
  51. tasksQueue.add(Constants.SCHEDULER_TASKS_QUEUE,"1_0_1_1_-1");
  52. tasksQueue.add(Constants.SCHEDULER_TASKS_QUEUE,"0_1_1_1_2130706433,3232236775");
  53. tasksQueue.add(Constants.SCHEDULER_TASKS_QUEUE,"1_1_0_1_2130706433,3232236775,"+IpUtils.ipToLong(OSUtils.getHost()));
  54. tasksQueue.add(Constants.SCHEDULER_TASKS_QUEUE,"1_2_1_1_2130706433,3232236775");
  55. List<String> tasks = tasksQueue.poll(Constants.SCHEDULER_TASKS_QUEUE, 1);
  56. if(tasks.size() <= 0){
  57. return;
  58. }
  59. //pop
  60. String node1 = tasks.get(0);
  61. assertEquals(node1,"1_0_1_1_-1");
  62. tasks = tasksQueue.poll(Constants.SCHEDULER_TASKS_QUEUE, 1);
  63. if(tasks.size() <= 0){
  64. return;
  65. }
  66. String node2 = tasks.get(0);
  67. }
  68. /**
  69. * test one million data from zookeeper queue
  70. */
  71. @Test
  72. public void extremeTest(){
  73. int total = 30 * 10000;
  74. for(int i = 0; i < total; i++)
  75. {
  76. for(int j = 0; j < total; j++) {
  77. //${processInstancePriority}_${processInstanceId}_${taskInstancePriority}_${taskId}
  78. //format ${processInstancePriority}_${processInstanceId}_${taskInstancePriority}_${taskId}
  79. String formatTask = String.format("%s_%d_%s_%d", i, i + 1, j, j == 0 ? 0 : j + new Random().nextInt(100));
  80. tasksQueue.add(Constants.SCHEDULER_TASKS_QUEUE, formatTask);
  81. }
  82. }
  83. String node1 = tasksQueue.poll(Constants.SCHEDULER_TASKS_QUEUE, 1).get(0);
  84. assertEquals(node1,"0");
  85. }
  86. }