forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkitemqueue.test.ts
More file actions
169 lines (142 loc) · 8.71 KB
/
Copy pathworkitemqueue.test.ts
File metadata and controls
169 lines (142 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var wtf = require('wtfnode');
const path = require("path");
const env = path.join(process.cwd(), 'config', '.env');
require("dotenv").config({ path: env }); // , debug: false
import { AddWorkitem, NoderedUtil, WebSocketClient, Workitem } from '@openiap/openflow-api';
import { suite, test, timeout } from '@testdeck/mocha';
import assert = require('assert');
import { Config } from '../OpenFlow/src/Config';
import { Logger } from '../OpenFlow/src/Logger';
@suite class workitemqueue {
private socket: WebSocketClient = null;
@timeout(10000)
async before() {
Config.workitem_queue_monitoring_enabled = false;
Config.disablelogging();
Logger.configure(true, false);
if (!this.socket) this.socket = new WebSocketClient(null, "wss://pc.openiap.io", true);
// if (!this.socket) this.socket = new WebSocketClient(null, "wss://demo.openiap.io", true, true);
this.socket.agent = "test-cli";
await this.socket.Connect();
await NoderedUtil.SigninWithUsername({ username: "testuser", password: "testuser" });
}
@timeout(5000)
async after() {
await this.socket.close(1000, "Close by user");
this.socket.events.removeAllListeners()
await Logger.shutdown();
// wtf.dump()
}
@timeout(10000)
@test
async 'basic workitem test'() {
let q = await NoderedUtil.GetWorkitemQueue({ name: "test queue" });
if (q == null) q = await NoderedUtil.AddWorkitemQueue({ name: "test queue" });
assert.notStrictEqual(q, null, "Failed getting test queue");
assert.notStrictEqual(q, undefined, "Failed getting test queue");
let item: Workitem;
do {
item = await NoderedUtil.PopWorkitem({ wiq: q.name });
if (item != null) await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "successful" });
} while (item != null)
item = await NoderedUtil.AddWorkitem({ name: "Test Work Item", payload: { "find": "me" }, wiq: q.name });
assert.notStrictEqual(item, null, "Failed adding test work item");
assert.notStrictEqual(item, undefined, "Failed adding test work item");
assert.strictEqual(item.name, "Test Work Item", "Failed matching name on new work item");
assert.strictEqual(item.state, "new", "New Workitem is not in status new");
item = await NoderedUtil.PopWorkitem({ wiq: q.name });
assert.notStrictEqual(item, null, "Failed getting test work item");
assert.notStrictEqual(item, undefined, "Failed getting test work item");
assert.strictEqual(item.name, "Test Work Item", "Failed matching name on work item");
assert.strictEqual(item.state, "processing", "Updated Workitem is not in state processing");
item = await NoderedUtil.UpdateWorkitem({ _id: item._id });
let testitem = await NoderedUtil.PopWorkitem({ wiq: q.name });
assert.strictEqual(testitem, undefined, "Failed queue test, can still pop items while processing the added item!");
item = await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "retry" });
assert.strictEqual(item.state, "new", "Workitem sent for retry is not in status new");
item = await NoderedUtil.PopWorkitem({ wiq: q.name });
assert.strictEqual(item.state, "processing");
assert.notStrictEqual(item, null, "Failed getting test work item");
assert.notStrictEqual(item, undefined, "Failed getting test work item");
assert.strictEqual(item.name, "Test Work Item", "Failed matching name on work item");
await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "successful" });
// await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "successful" });
}
@timeout(10000)
@test async 'basic workitem test with files'() {
let q = await NoderedUtil.GetWorkitemQueue({ name: "test queue" });
if (q == null) q = await NoderedUtil.AddWorkitemQueue({ name: "test queue" });
assert.notStrictEqual(q, null, "Failed getting test queue");
assert.notStrictEqual(q, undefined, "Failed getting test queue");
let item: Workitem;
do {
item = await NoderedUtil.PopWorkitem({ wiq: q.name });
if (item != null) await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "successful" });
} while (item != null)
item = await NoderedUtil.AddWorkitem({
name: "Test Work Item with files", payload: { "find": "me" }, wiq: q.name,
files: await NoderedUtil.CreateWorkitemFilesArray([__filename], true)
});
assert.notStrictEqual(item, null, "Failed adding Test Work Item with files");
assert.notStrictEqual(item, undefined, "Failed adding Test Work Item with files");
assert.strictEqual(item.name, "Test Work Item with files", "Failed matching name on new work item");
assert.strictEqual(item.state, "new");
item = await NoderedUtil.PopWorkitem({ wiq: q.name });
assert.notStrictEqual(item, null, "Failed getting Test Work Item with files");
assert.notStrictEqual(item, undefined, "Failed getting Test Work Item with files");
assert.strictEqual(item.files.length, 1);
assert.strictEqual(item.name, "Test Work Item with files", "Failed matching name on work item");
assert.strictEqual(item.state, "processing");
item = await NoderedUtil.UpdateWorkitem({
_id: item._id,
files: await NoderedUtil.CreateWorkitemFilesArray([path.join(__dirname, 'tsconfig.json')], false)
});
let testitem = await NoderedUtil.PopWorkitem({ wiq: q.name });
assert.strictEqual(testitem, undefined, "Failed queue test, can still pop items while processing the added item!");
item = await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "retry" });
assert.strictEqual(item.state, "new");
assert.strictEqual(item.files.length, 2);
item = await NoderedUtil.PopWorkitem({ wiq: q.name });
assert.strictEqual(item.state, "processing");
assert.notStrictEqual(item, null, "Failed getting Test Work Item with files");
assert.notStrictEqual(item, undefined, "Failed getting Test Work Item with files");
assert.strictEqual(item.name, "Test Work Item with files", "Failed matching name on work item");
assert.strictEqual(item.files.length, 2);
await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "successful" });
// await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "successful" });
}
@timeout(10000)
@test async 'multiple workitem test with files'() {
let q = await NoderedUtil.GetWorkitemQueue({ name: "test queue" });
if (q == null) q = await NoderedUtil.AddWorkitemQueue({ name: "test queue" });
assert.notStrictEqual(q, null, "Failed getting test queue");
assert.notStrictEqual(q, undefined, "Failed getting test queue");
let item: Workitem;
do {
item = await NoderedUtil.PopWorkitem({ wiq: q.name });
if (item != null) await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "successful" });
} while (item != null)
const items: AddWorkitem[] = [];
items.push(AddWorkitem.parse({ name: "multi item 1", files: await NoderedUtil.CreateWorkitemFilesArray([__filename], true) }));
items.push(AddWorkitem.parse({ name: "multi item 2", files: await NoderedUtil.CreateWorkitemFilesArray([__filename], true) }));
items.push(AddWorkitem.parse({ name: "multi item 3", files: await NoderedUtil.CreateWorkitemFilesArray([__filename], true) }));
await NoderedUtil.AddWorkitems({ items, wiq: q.name })
for (var i = 0; i < 3; i++) {
item = await NoderedUtil.PopWorkitem({ wiq: q.name, });
assert.notStrictEqual(item, null, "Failed getting test work item");
assert.notStrictEqual(item, undefined, "Failed getting test work item");
assert.strictEqual(item.files.length, 1);
assert.notStrictEqual(["multi item 1", "multi item 2", "multi item 3"].indexOf(item.name), -1, "Failed matching name on work item");
assert.strictEqual(item.state, "processing");
item = await NoderedUtil.UpdateWorkitem({
_id: item._id,
files: await NoderedUtil.CreateWorkitemFilesArray([path.join(__dirname, 'tsconfig.json')], false)
});
assert.strictEqual(item.files.length, 2);
await NoderedUtil.UpdateWorkitem({ _id: item._id, state: "successful" });
}
let testitem = await NoderedUtil.PopWorkitem({ wiq: q.name });
assert.strictEqual(testitem, undefined, "Failed multiple queue test, can still pop items after processing the 3 items!");
}
}
// cls | ./node_modules/.bin/_mocha 'test/**/workitemqueue.test.ts'