|
| 1 | +import { appRouter } from "../routers/_app"; |
| 2 | +import { expect, test } from "vitest"; |
| 3 | +import { initRedisInstance, NUM_OF_SCHEDULERS } from "./test.utils"; |
| 4 | +import { TRPCError } from "@trpc/server"; |
| 5 | + |
| 6 | +test("list schedulers", async () => { |
| 7 | + const { ctx, firstQueue } = await initRedisInstance(); |
| 8 | + try { |
| 9 | + const caller = appRouter.createCaller(ctx); |
| 10 | + const list = await caller.scheduler.list({ |
| 11 | + queueName: firstQueue.queue.name, |
| 12 | + }); |
| 13 | + |
| 14 | + expect(list.length).toBe(NUM_OF_SCHEDULERS); |
| 15 | + } catch (e) { |
| 16 | + if (firstQueue.type !== "bullmq") { |
| 17 | + expect(e).toBeInstanceOf(TRPCError); |
| 18 | + if (e instanceof TRPCError) { |
| 19 | + expect(e.code).toBe("BAD_REQUEST"); |
| 20 | + } |
| 21 | + } else { |
| 22 | + throw e; |
| 23 | + } |
| 24 | + } |
| 25 | +}); |
| 26 | + |
| 27 | +test("remove scheduler", async () => { |
| 28 | + const { ctx, firstQueue } = await initRedisInstance(); |
| 29 | + try { |
| 30 | + const caller = appRouter.createCaller(ctx); |
| 31 | + |
| 32 | + const schedulers = await caller.scheduler.list({ |
| 33 | + queueName: firstQueue.queue.name, |
| 34 | + }); |
| 35 | + const scheduler = schedulers[0]; |
| 36 | + |
| 37 | + await caller.scheduler.remove({ |
| 38 | + queueName: firstQueue.queue.name, |
| 39 | + jobSchedulerId: scheduler.key, |
| 40 | + }); |
| 41 | + |
| 42 | + const list = await caller.scheduler.list({ |
| 43 | + queueName: firstQueue.queue.name, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(list.length).toBe(NUM_OF_SCHEDULERS - 1); |
| 47 | + } catch (e) { |
| 48 | + if (firstQueue.type !== "bullmq") { |
| 49 | + expect(e).toBeInstanceOf(TRPCError); |
| 50 | + if (e instanceof TRPCError) { |
| 51 | + expect(e.code).toBe("BAD_REQUEST"); |
| 52 | + } |
| 53 | + } else { |
| 54 | + throw e; |
| 55 | + } |
| 56 | + } |
| 57 | +}); |
| 58 | + |
| 59 | +test("bulk remove schedulers", async () => { |
| 60 | + const { ctx, firstQueue } = await initRedisInstance(); |
| 61 | + try { |
| 62 | + const caller = appRouter.createCaller(ctx); |
| 63 | + |
| 64 | + const schedulers = await caller.scheduler.list({ |
| 65 | + queueName: firstQueue.queue.name, |
| 66 | + }); |
| 67 | + |
| 68 | + await caller.scheduler.bulkRemove({ |
| 69 | + queueName: firstQueue.queue.name, |
| 70 | + jobSchedulerIds: schedulers.map((scheduler) => scheduler.key), |
| 71 | + }); |
| 72 | + |
| 73 | + const list = await caller.scheduler.list({ |
| 74 | + queueName: firstQueue.queue.name, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(list.length).toBe(0); |
| 78 | + } catch (e) { |
| 79 | + if (firstQueue.type !== "bullmq") { |
| 80 | + expect(e).toBeInstanceOf(TRPCError); |
| 81 | + if (e instanceof TRPCError) { |
| 82 | + expect(e.code).toBe("BAD_REQUEST"); |
| 83 | + } |
| 84 | + } else { |
| 85 | + throw e; |
| 86 | + } |
| 87 | + } |
| 88 | +}); |
0 commit comments