Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 6 | * ================================================================================
|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 | * you may not use this file except in compliance with the License.
|
| 9 | * You may obtain a copy of the License at
|
| 10 | *
|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 12 | *
|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 | * See the License for the specific language governing permissions and
|
| 17 | * limitations under the License.
|
| 18 | * ============LICENSE_END=========================================================
|
| 19 | */
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 | package org.onap.osam.services;
|
| 24 |
|
| 25 | //
|
| 26 | //import com.google.common.collect.ImmutableList;
|
| 27 | //import com.google.common.collect.ImmutableMap;
|
| 28 | //import org.apache.commons.lang.RandomStringUtils;
|
| 29 | //import org.apache.commons.lang3.RandomUtils;
|
| 30 | //import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
| 31 | //import org.apache.commons.lang3.builder.ToStringStyle;
|
| 32 | //import org.hibernate.SessionFactory;
|
| 33 | //import org.onap.vid.exceptions.GenericUncheckedException;
|
| 34 | //import org.onap.vid.exceptions.OperationNotAllowedException;
|
| 35 | //import org.onap.vid.job.Job;
|
| 36 | //import org.onap.vid.job.JobAdapter;
|
| 37 | //import org.onap.vid.job.JobType;
|
| 38 | //import org.onap.vid.job.JobsBrokerService;
|
| 39 | //import org.onap.vid.job.impl.JobDaoImpl;
|
| 40 | //import org.onap.vid.job.impl.JobsBrokerServiceInDatabaseImpl;
|
| 41 | //import org.onap.vid.utils.DaoUtils;
|
| 42 | //import org.onap.vid.config.DataSourceConfig;
|
| 43 | //import org.onap.vid.config.JobAdapterConfig;
|
| 44 | //import org.onap.portalsdk.core.domain.support.DomainVo;
|
| 45 | //import org.onap.portalsdk.core.service.DataAccessService;
|
| 46 | //import org.onap.portalsdk.core.util.SystemProperties;
|
| 47 | //import org.springframework.test.context.ContextConfiguration;
|
| 48 | //import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
|
| 49 | //import org.testng.Assert;
|
| 50 | //import org.testng.annotations.AfterMethod;
|
| 51 | //import org.testng.annotations.BeforeMethod;
|
| 52 | //import org.testng.annotations.DataProvider;
|
| 53 | //import org.testng.annotations.Test;
|
| 54 | //
|
| 55 | //import javax.inject.Inject;
|
| 56 | //import java.lang.reflect.Method;
|
| 57 | //import java.time.LocalDateTime;
|
| 58 | //import java.time.ZoneId;
|
| 59 | //import java.util.*;
|
| 60 | //import java.util.concurrent.*;
|
| 61 | //import java.util.stream.Collectors;
|
| 62 | //import java.util.stream.IntStream;
|
| 63 | //import java.util.stream.Stream;
|
| 64 | //
|
| 65 | //import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
| 66 | //import static org.hamcrest.CoreMatchers.equalTo;
|
| 67 | //import static org.hamcrest.CoreMatchers.is;
|
| 68 | //import static org.hamcrest.MatcherAssert.assertThat;
|
| 69 | //import static org.hamcrest.Matchers.both;
|
| 70 | //import static org.hamcrest.Matchers.containsInAnyOrder;
|
| 71 | //import static org.onap.vid.job.Job.JobStatus.*;
|
| 72 | //import static org.onap.vid.utils.Streams.not;
|
| 73 | //import static org.testng.Assert.assertNotNull;
|
| 74 | //import static org.testng.AssertJUnit.assertEquals;
|
| 75 | //
|
| 76 | //@ContextConfiguration(classes = {DataSourceConfig.class, SystemProperties.class, JobAdapterConfig.class})
|
| 77 | //public class JobsBrokerServiceTest extends AbstractTestNGSpringContextTests {
|
| 78 | //
|
| 79 | // private static final int JOBS_COUNT = 127;
|
| 80 | // private static final boolean DELETED = true;
|
| 81 | // private final ExecutorService executor = Executors.newFixedThreadPool(90);
|
| 82 | //
|
| 83 | // private final Set<Long> threadsIds = new ConcurrentSkipListSet<>();
|
| 84 | //
|
| 85 | // private final long FEW = 500;
|
| 86 | //
|
| 87 | // private final String JOBS_SHOULD_MATCH = "the jobs that added and those that pulled must be the same";
|
| 88 | // private final String JOBS_PEEKED_SHOULD_MATCH = "the jobs that added and those that peeked must be the same";
|
| 89 | // private static final String DELETE_SERVICE_INFO_STATUS_EXCEPTION_MESSAGE = "Service status does not allow deletion from the queue";
|
| 90 | // private static final String DELETE_SERVICE_NOT_EXIST_EXCEPTION_MESSAGE = "Service does not exist";
|
| 91 | // private JobsBrokerService broker;
|
| 92 | //
|
| 93 | // @Inject
|
| 94 | // JobAdapter jobAdapter;
|
| 95 | // @Inject
|
| 96 | // private DataAccessService dataAccessService;
|
| 97 | // @Inject
|
| 98 | // private SessionFactory sessionFactory;
|
| 99 | //
|
| 100 | //
|
| 101 | // private class NoJobException extends RuntimeException {
|
| 102 | // }
|
| 103 | //
|
| 104 | // private Future<Job> newJobAsync(JobsBrokerService b) {
|
| 105 | // return newJobAsync(b, createMockJob("user id"));
|
| 106 | // }
|
| 107 | //
|
| 108 | // private Future<Job> newJobAsync(JobsBrokerService b, Job.JobStatus status) {
|
| 109 | // return newJobAsync(b, createMockJob("user id", status));
|
| 110 | // }
|
| 111 | //
|
| 112 | // private Job createMockJob(String userId) {
|
| 113 | // return jobAdapter.createJob(
|
| 114 | // JobType.NoOp,
|
| 115 | // new JobAdapter.AsyncJobRequest() {
|
| 116 | // public int nothing = 42;
|
| 117 | // },
|
| 118 | // UUID.randomUUID(),
|
| 119 | // userId,
|
| 120 | // RandomUtils.nextInt());
|
| 121 | // }
|
| 122 | //
|
| 123 | // private Job createMockJob(String userId, Job.JobStatus jobStatus) {
|
| 124 | // Job job = createMockJob(userId);
|
| 125 | // job.setStatus(jobStatus);
|
| 126 | // return job;
|
| 127 | // }
|
| 128 | //
|
| 129 | // private Future<Job> newJobAsync(JobsBrokerService b, Job job) {
|
| 130 | // final Future<Job> jobFuture = executor.submit(() -> {
|
| 131 | // accountThreadId();
|
| 132 | //
|
| 133 | // b.add(job);
|
| 134 | //
|
| 135 | // return job;
|
| 136 | // });
|
| 137 | // return jobFuture;
|
| 138 | // }
|
| 139 | //
|
| 140 | // private void pushBackJobAsync(JobsBrokerService b, Job job) {
|
| 141 | // executor.submit(() -> {
|
| 142 | // accountThreadId();
|
| 143 | // b.pushBack(job);
|
| 144 | // return job;
|
| 145 | // });
|
| 146 | // }
|
| 147 | //
|
| 148 | // private Future<Optional<Job>> pullJobAsync(JobsBrokerService broker) {
|
| 149 | // final Future<Optional<Job>> job = executor.submit(() -> {
|
| 150 | // accountThreadId();
|
| 151 | // // Pull only pending jobs, as H2 database does not support our SQL for in-progress jobs
|
| 152 | // return broker.pull(Job.JobStatus.PENDING, UUID.randomUUID().toString());
|
| 153 | // });
|
| 154 | // return job;
|
| 155 | // }
|
| 156 | //
|
| 157 | // private Job waitForFutureOptionalJob(Future<Optional<Job>> retrievedOptionalJobFuture) {
|
| 158 | // try {
|
| 159 | // return retrievedOptionalJobFuture.get(FEW, MILLISECONDS).orElseThrow(NoJobException::new);
|
| 160 | // } catch (TimeoutException | InterruptedException | ExecutionException e) {
|
| 161 | // throw new RuntimeException(e);
|
| 162 | // }
|
| 163 | // }
|
| 164 | //
|
| 165 | // private Job waitForFutureJob(Future<Job> retrievedJobFuture) {
|
| 166 | // try {
|
| 167 | // return retrievedJobFuture.get(FEW, MILLISECONDS);
|
| 168 | // } catch (TimeoutException | InterruptedException | ExecutionException e) {
|
| 169 | // throw new RuntimeException(e);
|
| 170 | // }
|
| 171 | // }
|
| 172 | //
|
| 173 | // private List<Job> putAndGetALotOfJobs(JobsBrokerService broker) {
|
| 174 | // final List<Job> originalJobs = putALotOfJobs(broker);
|
| 175 | // final List<Job> retrievedJobs = getAlotOfJobs(broker);
|
| 176 | //
|
| 177 | // assertThat(JOBS_SHOULD_MATCH, retrievedJobs, containsInAnyOrder(originalJobs.toArray()));
|
| 178 | //
|
| 179 | // return retrievedJobs;
|
| 180 | // }
|
| 181 | //
|
| 182 | // private List<Job> putALotOfJobs(JobsBrokerService broker) {
|
| 183 | // int n = JOBS_COUNT;
|
| 184 | // return IntStream.range(0, n)
|
| 185 | // .mapToObj(i -> newJobAsync(broker))
|
| 186 | // .map(this::waitForFutureJob)
|
| 187 | // .collect(Collectors.toList());
|
| 188 | // }
|
| 189 | //
|
| 190 | // private List<Job> getAlotOfJobs(JobsBrokerService broker) {
|
| 191 | // int n = JOBS_COUNT;
|
| 192 | // return IntStream.range(0, n)
|
| 193 | // .mapToObj(i -> pullJobAsync(broker))
|
| 194 | // .map(this::waitForFutureOptionalJob)
|
| 195 | // .collect(Collectors.toList());
|
| 196 | // }
|
| 197 | //
|
| 198 | // private void pushBackJobs(List<Job> jobs, JobsBrokerService broker) {
|
| 199 | // jobs.forEach(job -> pushBackJobAsync(broker, job));
|
| 200 | // }
|
| 201 | //
|
| 202 | // private void accountThreadId() {
|
| 203 | // threadsIds.add(Thread.currentThread().getId());
|
| 204 | // }
|
| 205 | //
|
| 206 | // @AfterMethod
|
| 207 | // public void threadsCounter() {
|
| 208 | // System.out.println("participating threads count: " + threadsIds.size());
|
| 209 | // threadsIds.clear();
|
| 210 | // }
|
| 211 | //
|
| 212 | // @BeforeMethod
|
| 213 | // public void initializeBroker() {
|
| 214 | // broker = new JobsBrokerServiceInDatabaseImpl(dataAccessService, sessionFactory, 200, 0);
|
| 215 | // ((JobsBrokerServiceInDatabaseImpl) broker).deleteAll();
|
| 216 | // }
|
| 217 | //
|
| 218 | // @Test
|
| 219 | // public void givenSingleJob_getIt_verifySameJob() {
|
| 220 | // final Job originalJob = waitForFutureJob(newJobAsync(broker));
|
| 221 | //
|
| 222 | // final Job retrievedJob = waitForFutureOptionalJob(pullJobAsync(broker));
|
| 223 | // assertThat(JOBS_SHOULD_MATCH, retrievedJob, is(originalJob));
|
| 224 | // }
|
| 225 | //
|
| 226 | // @Test
|
| 227 | // public void givenManyJobs_getJobsAndPushThemBack_alwaysSeeAllOfThemWithPeek() throws InterruptedException {
|
| 228 | // final List<Job> originalJobs = putALotOfJobs(broker);
|
| 229 | //
|
| 230 | // MILLISECONDS.sleep(FEW);
|
| 231 | // assertThat(JOBS_PEEKED_SHOULD_MATCH, broker.peek(), containsInAnyOrder(originalJobs.toArray()));
|
| 232 | //
|
| 233 | // final Job retrievedJob = waitForFutureOptionalJob(pullJobAsync(broker));
|
| 234 | //
|
| 235 | // MILLISECONDS.sleep(FEW);
|
| 236 | // assertThat(JOBS_PEEKED_SHOULD_MATCH, broker.peek(), containsInAnyOrder(originalJobs.toArray()));
|
| 237 | //
|
| 238 | // pushBackJobAsync(broker, retrievedJob);
|
| 239 | //
|
| 240 | // MILLISECONDS.sleep(FEW);
|
| 241 | // assertThat(JOBS_PEEKED_SHOULD_MATCH, broker.peek(), containsInAnyOrder(originalJobs.toArray()));
|
| 242 | // }
|
| 243 | //
|
| 244 | // @Test
|
| 245 | // public void givenManyJobs_getThemAll_verifySameJobs() {
|
| 246 | // putAndGetALotOfJobs(broker);
|
| 247 | // }
|
| 248 | //
|
| 249 | // @Test
|
| 250 | // public void givenManyJobs_getThemAllThenPushBackandGet_verifySameJobs() {
|
| 251 | // final List<Job> retrievedJobs1 = putAndGetALotOfJobs(broker);
|
| 252 | //
|
| 253 | // pushBackJobs(retrievedJobs1, broker);
|
| 254 | // final List<Job> retrievedJobs2 = getAlotOfJobs(broker);
|
| 255 | //
|
| 256 | // assertThat(JOBS_SHOULD_MATCH, retrievedJobs2, containsInAnyOrder(retrievedJobs1.toArray()));
|
| 257 | // }
|
| 258 | //
|
| 259 | // private static Date toDate(LocalDateTime localDateTime) {
|
| 260 | // return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
| 261 | // }
|
| 262 | //
|
| 263 | // private void setModifiedDateToJob(UUID jobUuid, Date date) {
|
| 264 | // DomainVo job = dataAccessService.getDomainObject(JobDaoImpl.class, jobUuid, DaoUtils.getPropsMap());
|
| 265 | // job.setModified(date);
|
| 266 | // DaoUtils.tryWithSessionAndTransaction(sessionFactory, session -> {
|
| 267 | // session.saveOrUpdate(job);
|
| 268 | // return 1;
|
| 269 | // });
|
| 270 | // }
|
| 271 | //
|
| 272 | //
|
| 273 | // public static JobDaoImpl createNewJob(Integer indexInBulk, UUID templateId, String userId, Job.JobStatus status, String takenBy, LocalDateTime date) {
|
| 274 | // return createNewJob(indexInBulk, templateId, userId, status, takenBy, date, false);
|
| 275 | // }
|
| 276 | //
|
| 277 | // public static JobDaoImpl createNewJob(Integer indexInBulk, UUID templateId, String userId, Job.JobStatus status, String takenBy, LocalDateTime date, boolean deleted){
|
| 278 | // JobDaoImpl job = new JobDaoImpl();
|
| 279 | // job.setTypeAndData(JobType.NoOp, ImmutableMap.of("x", RandomStringUtils.randomAlphanumeric(15)));
|
| 280 | // job.setIndexInBulk(indexInBulk);
|
| 281 | // job.setTemplateId(templateId);
|
| 282 | // job.setType(JobType.NoOp);
|
| 283 | // job.setStatus(status);
|
| 284 | // job.setTakenBy(takenBy);
|
| 285 | // job.setCreated(toDate(date));
|
| 286 | // job.setModified(toDate(date));
|
| 287 | // job.setUserId(userId);
|
| 288 | // if (deleted) {
|
| 289 | // job.setDeletedAt(new Date());
|
| 290 | // }
|
| 291 | // return job;
|
| 292 | // }
|
| 293 | //
|
| 294 | // @DataProvider
|
| 295 | // public static Object[][] jobs(Method test) {
|
| 296 | // LocalDateTime oldestDate = LocalDateTime.now().minusHours(30);
|
| 297 | // UUID sameTemplate = UUID.randomUUID();
|
| 298 | // return new Object[][]{
|
| 299 | // {ImmutableList.of(
|
| 300 | // createNewJob(11, UUID.randomUUID(), "userId", PENDING, null, oldestDate),
|
| 301 | // createNewJob(22, UUID.randomUUID(), "userId", PENDING, null, oldestDate),
|
| 302 | // createNewJob(11, UUID.randomUUID(), "userId", PENDING, null, LocalDateTime.now().minusHours(2)),
|
| 303 | // createNewJob(44, UUID.randomUUID(), "userId", PENDING, null, LocalDateTime.now().minusHours(5))),
|
| 304 | // 4,
|
| 305 | // 0,
|
| 306 | // PENDING,
|
| 307 | // "Broker should pull the first pending job by oldest date then by job index"
|
| 308 | // },
|
| 309 | // { ImmutableList.of(
|
| 310 | // createNewJob(11, UUID.randomUUID(), "userId", COMPLETED,null, oldestDate),
|
| 311 | // createNewJob(11, UUID.randomUUID(), "userId", PENDING,null, oldestDate, DELETED),createNewJob(12, UUID.randomUUID(), "userId", FAILED,null, oldestDate),
|
| 312 | // createNewJob(13, UUID.randomUUID(), "userId", IN_PROGRESS,null, oldestDate),
|
| 313 | // createNewJob(14, UUID.randomUUID(), "userId", STOPPED,null, oldestDate),
|
| 314 | // createNewJob(22, UUID.randomUUID(), "userId", PENDING,null, oldestDate),
|
| 315 | // createNewJob(33, UUID.randomUUID(), "userId", PENDING,null, LocalDateTime.now().minusHours(2))),
|
| 316 | // 6,
|
| 317 | // 5,
|
| 318 | // PENDING,
|
| 319 | // "Broker should pull the only pending - first pending job by oldest job - ignore deleted,completed, failed, in-progress and stopped statuses"
|
| 320 | // },
|
| 321 | // {ImmutableList.of(
|
| 322 | // createNewJob(11, UUID.randomUUID(), "userId", IN_PROGRESS, UUID.randomUUID().toString(), oldestDate),
|
| 323 | // createNewJob(22, UUID.randomUUID(), "userId", IN_PROGRESS, UUID.randomUUID().toString(), oldestDate),
|
| 324 | // createNewJob(33, UUID.randomUUID(), "userId", PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 325 | // 2,
|
| 326 | // -1,
|
| 327 | // PENDING,
|
| 328 | // "Broker should not pull any job when it exceeded mso limit with count (in-progress) statuses"
|
| 329 | // },
|
| 330 | // {ImmutableList.of(
|
| 331 | // createNewJob(11, UUID.randomUUID(), "userId", IN_PROGRESS, UUID.randomUUID().toString(), oldestDate),
|
| 332 | // createNewJob(22, UUID.randomUUID(), "userId", PENDING, UUID.randomUUID().toString(), oldestDate),
|
| 333 | // createNewJob(33, UUID.randomUUID(), "userId", PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 334 | // 2,
|
| 335 | // -1,
|
| 336 | // PENDING,
|
| 337 | // "Broker should not pull any job when it exceeded mso limit with count(in-progress or pending && taken) statuses"
|
| 338 | // },
|
| 339 | // {ImmutableList.of(
|
| 340 | // createNewJob(11, UUID.randomUUID(), "userId", IN_PROGRESS, UUID.randomUUID().toString(), oldestDate),
|
| 341 | // createNewJob(22, UUID.randomUUID(), "userId", PENDING, UUID.randomUUID().toString(), oldestDate),
|
| 342 | // createNewJob(33, UUID.randomUUID(), "userId", PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 343 | // 3,
|
| 344 | // 2,
|
| 345 | // PENDING,
|
| 346 | // "Broker should pull first job when it doesn't exceeded mso limit with count(in-progress or pending && taken) statuses"
|
| 347 | // },
|
| 348 | // {ImmutableList.of(
|
| 349 | // createNewJob(11, sameTemplate, "userId", PENDING, UUID.randomUUID().toString(), oldestDate),
|
| 350 | // createNewJob(22, sameTemplate, "userId", PENDING, null, oldestDate),
|
| 351 | // createNewJob(33, sameTemplate, "userId", PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 352 | // 3,
|
| 353 | // -1,
|
| 354 | // PENDING,
|
| 355 | // "Broker should not pull any job when there is another job from this template that was taken"
|
| 356 | // },
|
| 357 | // {ImmutableList.of(
|
| 358 | // createNewJob(11, sameTemplate, "userId", IN_PROGRESS, null, oldestDate),
|
| 359 | // createNewJob(22, sameTemplate, "userId", PENDING, null, oldestDate),
|
| 360 | // createNewJob(33, sameTemplate, "userId", PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 361 | // 3,
|
| 362 | // -1,
|
| 363 | // PENDING,
|
| 364 | // "Broker should not pull any job when there is another job from this template that in progress"
|
| 365 | // },
|
| 366 | // {ImmutableList.of(
|
| 367 | // createNewJob(11, sameTemplate, "userId", FAILED, null, oldestDate),
|
| 368 | // createNewJob(22, sameTemplate, "userId", STOPPED, null, oldestDate),
|
| 369 | // createNewJob(33, sameTemplate, "userId", PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 370 | // 3,
|
| 371 | // -1,
|
| 372 | // PENDING,
|
| 373 | // "Broker should not pull any job when there is another job from this template that was failed"
|
| 374 | // },
|
| 375 | // {ImmutableList.of(
|
| 376 | // createNewJob(11, sameTemplate, "userId", FAILED, null, oldestDate, DELETED),
|
| 377 | // createNewJob(22, sameTemplate, "userId", STOPPED,null, oldestDate),
|
| 378 | // createNewJob(33, sameTemplate, "userId", PENDING,null, LocalDateTime.now().minusHours(2))),
|
| 379 | // 3,
|
| 380 | // 2,
|
| 381 | // PENDING,
|
| 382 | // "Broker should pull pending job when there is another job from this template that was deleted, although failed"
|
| 383 | // },
|
| 384 | // { ImmutableList.of(
|
| 385 | // createNewJob(11, UUID.randomUUID(), "userA", IN_PROGRESS, null, oldestDate),
|
| 386 | // createNewJob(22, UUID.randomUUID(), "userA", PENDING, null, oldestDate),
|
| 387 | // createNewJob(33, UUID.randomUUID(), "userB", PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 388 | // 3,
|
| 389 | // 2,
|
| 390 | // PENDING,
|
| 391 | // "Broker should prioritize jobs of user that has no in-progress jobs"
|
| 392 | // },
|
| 393 | // {ImmutableList.of(
|
| 394 | // createNewJob(11, UUID.randomUUID(), "userA", PENDING, UUID.randomUUID().toString(), oldestDate),
|
| 395 | // createNewJob(22, UUID.randomUUID(), "userA", PENDING, null, oldestDate),
|
| 396 | // createNewJob(33, UUID.randomUUID(), "userB", PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 397 | // 3,
|
| 398 | // 2,
|
| 399 | // PENDING,
|
| 400 | // "Broker should prioritize jobs of user that has no taken jobs"
|
| 401 | // },
|
| 402 | // {ImmutableList.of(
|
| 403 | // createNewJob(11, UUID.randomUUID(), "userA", IN_PROGRESS, UUID.randomUUID().toString(), oldestDate),
|
| 404 | // createNewJob(22, UUID.randomUUID(), "userA", PENDING, null, LocalDateTime.now().minusHours(2)),
|
| 405 | // createNewJob(31, UUID.randomUUID(), "userB", IN_PROGRESS, null, LocalDateTime.now().minusHours(2)),
|
| 406 | // createNewJob(32, UUID.randomUUID(), "userB", IN_PROGRESS, null, LocalDateTime.now().minusHours(2)),
|
| 407 | // createNewJob(33, UUID.randomUUID(), "userB", PENDING, null, oldestDate)),
|
| 408 | // 5,
|
| 409 | // 4,
|
| 410 | // PENDING,
|
| 411 | // "Broker should take oldest job when there is one in-progress job to each user"
|
| 412 | // },
|
| 413 | // {ImmutableList.of(
|
| 414 | // createNewJob(11, UUID.randomUUID(), UUID.randomUUID().toString(), IN_PROGRESS, null, oldestDate),
|
| 415 | // createNewJob(22, UUID.randomUUID(), UUID.randomUUID().toString(), IN_PROGRESS, null, oldestDate),
|
| 416 | // createNewJob(33, UUID.randomUUID(), UUID.randomUUID().toString(), PENDING, null, LocalDateTime.now().minusHours(2))),
|
| 417 | // 2,
|
| 418 | // -1,
|
| 419 | // PENDING,
|
| 420 | // "Broker should not pull any job when it exceeded mso limit with count(in-progress or pending && taken) statuses"
|
| 421 | // },
|
| 422 | // {ImmutableList.of(
|
| 423 | // createNewJob(11, UUID.randomUUID(), "userId", IN_PROGRESS, UUID.randomUUID().toString(), oldestDate),
|
| 424 | // createNewJob(22, UUID.randomUUID(), "userId", IN_PROGRESS, null, oldestDate),
|
| 425 | // createNewJob(11, UUID.randomUUID(), "userId", IN_PROGRESS, null, LocalDateTime.now().minusHours(2)),
|
| 426 | // createNewJob(44, UUID.randomUUID(), "userId", IN_PROGRESS, null, LocalDateTime.now().minusHours(5))),
|
| 427 | // 20,
|
| 428 | // 1,
|
| 429 | // IN_PROGRESS,
|
| 430 | // "Broker with in progress topic should pull the first in progress and not taken job by oldest date"
|
| 431 | // },
|
| 432 | // {ImmutableList.of(
|
| 433 | // createNewJob(11, UUID.randomUUID(), "userId", COMPLETED, null, oldestDate),
|
| 434 | // createNewJob(12, UUID.randomUUID(), "userId", FAILED, null, oldestDate),
|
| 435 | // createNewJob(13, UUID.randomUUID(), "userId", PENDING,null, oldestDate),
|
| 436 | // createNewJob(14, UUID.randomUUID(), "userId", STOPPED,null, oldestDate),
|
| 437 | // createNewJob(11, UUID.randomUUID(), "userId", IN_PROGRESS,null, oldestDate, DELETED),createNewJob(22, UUID.randomUUID(), "userId", IN_PROGRESS,null, oldestDate),
|
| 438 | // createNewJob(33, UUID.randomUUID(), "userId", IN_PROGRESS,null, LocalDateTime.now().minusHours(2))),
|
| 439 | // 20,
|
| 440 | // 5,
|
| 441 | // IN_PROGRESS,
|
| 442 | // "Broker with in progress topic should pull only in-progress jobs - first in-progress job by oldest date - ignore deleted,completed, failed, pending and stopped statuses"
|
| 443 | // },
|
| 444 | // {ImmutableList.of(
|
| 445 | // createNewJob(11, UUID.randomUUID(), "userId", IN_PROGRESS, null, LocalDateTime.now()),
|
| 446 | // createNewJob(22, UUID.randomUUID(), "userId", IN_PROGRESS, null, LocalDateTime.now().minusSeconds(1)),
|
| 447 | // createNewJob(33, UUID.randomUUID(), "userId", IN_PROGRESS, null, LocalDateTime.now().minusSeconds(2))),
|
| 448 | // 20,
|
| 449 | // -1,
|
| 450 | // IN_PROGRESS,
|
| 451 | // "Broker with in progress topic should not pull any job if its modified date is smaller than now-interval (20 seconds)"
|
| 452 | // }
|
| 453 | //
|
| 454 | // };
|
| 455 | // }
|
| 456 | //
|
| 457 | //
|
| 458 | // @Test(dataProvider = "jobs")
|
| 459 | // public void givenSomeJobs_pullNextJob_returnNextOrNothingAsExpected(List<JobDaoImpl> jobs, int msoLimit, int expectedIndexSelected, Job.JobStatus topic, String assertionReason) {
|
| 460 | // JobsBrokerServiceInDatabaseImpl broker = new JobsBrokerServiceInDatabaseImpl(dataAccessService, sessionFactory, msoLimit, 20);
|
| 461 | // for (JobDaoImpl job : jobs) {
|
| 462 | // Date modifiedDate = job.getModified();
|
| 463 | // broker.add(job);
|
| 464 | // setModifiedDateToJob(job.getUuid(), modifiedDate);
|
| 465 | // }
|
| 466 | // Optional<Job> nextJob = broker.pull(topic, UUID.randomUUID().toString());
|
| 467 | // boolean shouldAnyBeSelected = expectedIndexSelected >= 0;
|
| 468 | // Assert.assertEquals(nextJob.isPresent(), shouldAnyBeSelected, assertionReason);
|
| 469 | // if (shouldAnyBeSelected) {
|
| 470 | // Assert.assertEquals(jobs.get(expectedIndexSelected), nextJob.get(), assertionReason);
|
| 471 | // }
|
| 472 | // }
|
| 473 | //
|
| 474 | // @DataProvider
|
| 475 | // public Object[][] topics() {
|
| 476 | // return Arrays.stream(Job.JobStatus.values())
|
| 477 | // .filter(not(t -> ImmutableList.of(PENDING, IN_PROGRESS).contains(t)))
|
| 478 | // .map(v -> new Object[]{v}).collect(Collectors.toList()).toArray(new Object[][]{});
|
| 479 | // }
|
| 480 | //
|
| 481 | // @Test(dataProvider = "topics", expectedExceptions = GenericUncheckedException.class, expectedExceptionsMessageRegExp = "Unsupported topic.*")
|
| 482 | // public void pullUnexpectedTopic_exceptionIsThrown(Job.JobStatus topic) {
|
| 483 | // broker.pull(topic, UUID.randomUUID().toString());
|
| 484 | // }
|
| 485 | //
|
| 486 | // @Test(expectedExceptions = NoJobException.class)
|
| 487 | // public void givenNonPendingJobs_getJobAsPendingTopic_verifyNothingRetrieved() {
|
| 488 | // Stream.of(Job.JobStatus.values())
|
| 489 | // .filter(not(s -> s.equals(PENDING)))
|
| 490 | // .map(s -> createMockJob("some user id", s))
|
| 491 | // .map(job -> newJobAsync(broker, job))
|
| 492 | // .map(this::waitForFutureJob)
|
| 493 | // .collect(Collectors.toList());
|
| 494 | //
|
| 495 | // waitForFutureOptionalJob(pullJobAsync(broker));
|
| 496 | // }
|
| 497 | //
|
| 498 | // @Test
|
| 499 | // public void givenPendingAndNonPendingJobs_getJobAsPendingTopic_verifyAJobRetrieved() {
|
| 500 | // newJobAsync(broker); // this negated the expected result of the call below
|
| 501 | // givenNonPendingJobs_getJobAsPendingTopic_verifyNothingRetrieved();
|
| 502 | // }
|
| 503 | //
|
| 504 | // @Test(expectedExceptions = NoJobException.class)
|
| 505 | // public void givenManyJobs_pullThemAllAndAskOneMore_verifyFinallyNothingRetrieved() {
|
| 506 | // putAndGetALotOfJobs(broker);
|
| 507 | // waitForFutureOptionalJob(pullJobAsync(broker));
|
| 508 | // }
|
| 509 | //
|
| 510 | // @Test(expectedExceptions = NoJobException.class)
|
| 511 | // public void givenNoJob_requestJob_verifyNothingRetrieved() throws InterruptedException, ExecutionException, TimeoutException {
|
| 512 | // final Future<Optional<Job>> futureOptionalJob = pullJobAsync(broker);
|
| 513 | // assertThat("job should not be waiting yet", futureOptionalJob.get(FEW, MILLISECONDS).isPresent(), is(false));
|
| 514 | // waitForFutureOptionalJob(futureOptionalJob);
|
| 515 | // }
|
| 516 | //
|
| 517 | // @Test(expectedExceptions = IllegalStateException.class)
|
| 518 | // public void givenSinglePulledJob_pushBackDifferentJob_verifyPushingRejected() {
|
| 519 | // waitForFutureJob(newJobAsync(broker));
|
| 520 | // waitForFutureJob(newJobAsync(broker));
|
| 521 | // waitForFutureOptionalJob(pullJobAsync(broker));
|
| 522 | //
|
| 523 | // Job myJob = createMockJob("user id");
|
| 524 | // myJob.setUuid(UUID.randomUUID());
|
| 525 | //
|
| 526 | // broker.pushBack(myJob); //Should fail
|
| 527 | // }
|
| 528 | //
|
| 529 | // @Test
|
| 530 | // public void givenSingleJob_pushBackModifiedJob_verifyPulledIsVeryVeryTheSame() {
|
| 531 | // final ImmutableMap<String, Object> randomDataForMostRecentJobType =
|
| 532 | // ImmutableMap.of("42", 42, "complex", ImmutableList.of("a", "b", "c"));
|
| 533 | //
|
| 534 | // waitForFutureJob(newJobAsync(broker));
|
| 535 | // final Job job = waitForFutureOptionalJob(pullJobAsync(broker));
|
| 536 | //
|
| 537 | // job.setStatus(Job.JobStatus.PENDING);
|
| 538 | // job.setTypeAndData(JobType.NoOp, ImmutableMap.of("good", "morning"));
|
| 539 | // job.setTypeAndData(JobType.HttpCall, ImmutableMap.of());
|
| 540 | // job.setTypeAndData(JobType.ServiceInstantiation, randomDataForMostRecentJobType);
|
| 541 | //
|
| 542 | // broker.pushBack(job);
|
| 543 | // final Job retrievedJob = waitForFutureOptionalJob(pullJobAsync(broker));
|
| 544 | //
|
| 545 | // assertThat(JOBS_SHOULD_MATCH, retrievedJob, is(job));
|
| 546 | // assertThat(JOBS_SHOULD_MATCH, retrievedJob.getData(), both(equalTo(job.getData())).and(equalTo(randomDataForMostRecentJobType)));
|
| 547 | // assertThat(JOBS_SHOULD_MATCH, jobDataReflected(retrievedJob), is(jobDataReflected(job)));
|
| 548 | // }
|
| 549 | //
|
| 550 | // private static String jobDataReflected(Job job) {
|
| 551 | // return new ReflectionToStringBuilder(job, ToStringStyle.SHORT_PREFIX_STYLE)
|
| 552 | // .setExcludeFieldNames("created", "modified", "takenBy")
|
| 553 | // .toString();
|
| 554 | // }
|
| 555 | //
|
| 556 | // @Test(expectedExceptions = IllegalStateException.class)
|
| 557 | // public void givenSingleJob_pushBackTwice_verifyPushingRejected() {
|
| 558 | // waitForFutureJob(newJobAsync(broker));
|
| 559 | // final Job job = waitForFutureOptionalJob(pullJobAsync(broker));
|
| 560 | //
|
| 561 | // broker.pushBack(job);
|
| 562 | // broker.pushBack(job); //Should fail
|
| 563 | // }
|
| 564 | //
|
| 565 | // @Test
|
| 566 | // public void addJob_PeekItById_verifySameJobWasPeeked() {
|
| 567 | // String userId = UUID.randomUUID().toString();
|
| 568 | // Job myJob = createMockJob(userId);
|
| 569 | // UUID uuid = broker.add(myJob);
|
| 570 | // Job peekedJob = broker.peek(uuid);
|
| 571 | // assertEquals("added testId is not the same as peeked TestsId",
|
| 572 | // userId,
|
| 573 | // peekedJob.getData().get("userId"));
|
| 574 | // }
|
| 575 | //
|
| 576 | // @Test(dataProvider = "jobStatusesForSuccessDelete", expectedExceptions = NoJobException.class)
|
| 577 | // public void givenOneJob_deleteIt_canPeekOnItButCantPull(Job.JobStatus status) {
|
| 578 | // final Job job = waitForFutureJob(newJobAsync(broker, status));
|
| 579 | // broker.delete(job.getUuid());
|
| 580 | // assertNotNull(((JobDaoImpl) broker.peek(job.getUuid())).getDeletedAt(), "job should be deleted");
|
| 581 | // waitForFutureOptionalJob(pullJobAsync(broker));
|
| 582 | // }
|
| 583 | //
|
| 584 | // @DataProvider
|
| 585 | // public static Object[][] jobStatusesForSuccessDelete() {
|
| 586 | // return new Object[][]{
|
| 587 | // {PENDING},
|
| 588 | // {STOPPED}
|
| 589 | // };
|
| 590 | // }
|
| 591 | //
|
| 592 | // @Test(
|
| 593 | // dataProvider = "jobStatusesForFailedDelete",
|
| 594 | // expectedExceptions = OperationNotAllowedException.class,
|
| 595 | // expectedExceptionsMessageRegExp=DELETE_SERVICE_INFO_STATUS_EXCEPTION_MESSAGE
|
| 596 | // )
|
| 597 | // public void deleteJob_notAllowedStatus_exceptionIsThrown(Job.JobStatus status, boolean taken) {
|
| 598 | // final Job job = waitForFutureJob(newJobAsync(broker, createMockJob("some user id", status)));
|
| 599 | //
|
| 600 | // if (taken) {
|
| 601 | // waitForFutureOptionalJob(pullJobAsync(broker));
|
| 602 | // }
|
| 603 | //
|
| 604 | //
|
| 605 | // broker.delete(job.getUuid());
|
| 606 | // }
|
| 607 | //
|
| 608 | // @DataProvider
|
| 609 | // public static Object[][] jobStatusesForFailedDelete() {
|
| 610 | // return new Object[][]{
|
| 611 | // {PENDING, true},
|
| 612 | // {IN_PROGRESS, false},
|
| 613 | // {COMPLETED, false},
|
| 614 | // {PAUSE, false},
|
| 615 | // {FAILED, false},
|
| 616 | // };
|
| 617 | // }
|
| 618 | //
|
| 619 | // @Test(expectedExceptions = OperationNotAllowedException.class, expectedExceptionsMessageRegExp = DELETE_SERVICE_NOT_EXIST_EXCEPTION_MESSAGE)
|
| 620 | // public void deleteJob_notExist_exceptionIsThrown() {
|
| 621 | // waitForFutureJob(newJobAsync(broker, createMockJob("some user id", PENDING)));
|
| 622 | // broker.delete(new UUID(111, 111));
|
| 623 | // }
|
| 624 | //
|
| 625 | //}
|