UNPKG

1.96 kBPlain TextView Raw
1import test from "ava";
2import * as delay from "delay";
3import * as sinon from "sinon";
4import { Spider } from "../src/index";
5import { IPlan } from "../src/interfaces";
6
7// export type IStatus = "active" | "end" | "pause" | "vacant";
8
9test("testing 1 for status", async (t) => {
10 const s = new Spider({ heartbeat: 4000 });
11
12 await delay(1000);
13 t.is("vacant", s._STATE.status);
14
15 await delay(3100);
16 t.is("end", s._STATE.status);
17});
18
19test("testing 2 for status", async (t) => {
20 const s = new Spider({ heartbeat: 4000, concurrency: 2 });
21 s.plan({
22 name: "test",
23 process: async () => {
24 await delay(1000);
25 return null;
26 },
27 failed: () => null,
28 retries: 3,
29 });
30
31 await delay(1000);
32 t.is("vacant", s._STATE.status);
33
34 s.add("test", "http://teat1.com");
35 t.is("active", s._STATE.status);
36
37 await delay(4500);
38 t.is("end", s._STATE.status);
39});
40
41test("testing 3 for status", async (t) => {
42 const s = new Spider({ heartbeat: 4000, concurrency: 2 });
43 s.plan({
44 name: "test",
45 process: async () => {
46 await delay(1000);
47 return null;
48 },
49 failed: () => null,
50 retries: 3,
51 });
52
53 await delay(1000);
54 t.is("vacant", s._STATE.status);
55
56 s.pause();
57 t.is("pause", s._STATE.status);
58
59 s.add("test", "http://teat1.com");
60 t.is("pause", s._STATE.status);
61
62 await delay(4100);
63 t.is("pause", s._STATE.status);
64
65 s.active();
66
67 await delay(8100);
68 t.is("end", s._STATE.status);
69});
70
71test("testing 4 for status", async (t) => {
72 const s = new Spider({ heartbeat: 4000, stillAlive: true });
73 s.plan({
74 name: "test",
75 process: async () => {
76 await delay(200);
77 return null;
78 },
79 failed: () => null,
80 retries: 3,
81 });
82
83 await delay(4100);
84 t.is("vacant", s._STATE.status);
85
86 s.add("test", "http://test1.com");
87
88 await delay(8100);
89 t.is("vacant", s._STATE.status);
90});