1 | import test from "ava";
|
2 | import * as delay from "delay";
|
3 | import * as sinon from "sinon";
|
4 | import { Spider } from "../src/index";
|
5 | import { IPlan } from "../src/interfaces";
|
6 |
|
7 | test("testing 1 for retry", async (t) => {
|
8 | const s = new Spider();
|
9 |
|
10 | const failedSpy = sinon.spy();
|
11 | const handleSpy = sinon.spy();
|
12 |
|
13 | const plan: IPlan = {
|
14 | name: "test",
|
15 | process: async () => {
|
16 | await delay(100);
|
17 | handleSpy();
|
18 | throw new Error("error");
|
19 | },
|
20 | failed: failedSpy,
|
21 | retries: 3,
|
22 | };
|
23 | s.plan(plan);
|
24 | s.add("test", "http://test1.com");
|
25 |
|
26 | await delay(10000);
|
27 | t.is(1, failedSpy.callCount);
|
28 | t.is(4, handleSpy.callCount);
|
29 |
|
30 | });
|
31 |
|
32 | test("testing 2 for retry", async (t) => {
|
33 | const s = new Spider();
|
34 |
|
35 | const failedSpy = sinon.spy();
|
36 | const handleSpy = sinon.spy();
|
37 |
|
38 | const plan: IPlan = {
|
39 | name: "test",
|
40 | process: async () => {
|
41 | await delay(100);
|
42 | handleSpy();
|
43 | throw new Error("error");
|
44 | },
|
45 | failed: failedSpy,
|
46 | retries: 1,
|
47 | };
|
48 | s.plan(plan);
|
49 | s.add("test", "http://test1.com");
|
50 |
|
51 | await delay(10000);
|
52 | t.is(1, failedSpy.callCount);
|
53 | t.is(2, handleSpy.callCount);
|
54 |
|
55 | });
|
56 |
|
57 | test("testing 3 for retry", async (t) => {
|
58 | const s = new Spider();
|
59 |
|
60 | const failedSpy = sinon.spy();
|
61 | const handleSpy = sinon.spy();
|
62 |
|
63 | let isErr = true;
|
64 | const plan: IPlan = {
|
65 | name: "test",
|
66 | process: async () => {
|
67 | await delay(100);
|
68 | handleSpy();
|
69 | if (isErr) {
|
70 | isErr = false;
|
71 | throw new Error("error");
|
72 | } else {
|
73 | return null;
|
74 | }
|
75 | },
|
76 | failed: failedSpy,
|
77 | retries: 4,
|
78 | };
|
79 | s.plan(plan);
|
80 | s.add("test", "http://test1.com");
|
81 |
|
82 | await delay(10000);
|
83 | t.is(0, failedSpy.callCount);
|
84 | t.is(2, handleSpy.callCount);
|
85 | });
|