Skip to content

Commit e89d6da

Browse files
committed
test: add a soda worker for majordomo
1 parent 586fe8d commit e89d6da

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

examples/majordomo/README.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ The example will start a broker and some workers, then do some requests. The
2020
output will be similar to this:
2121

2222
```
23-
2423
starting broker on tcp://127.0.0.1:5555
25-
starting worker on tcp://127.0.0.1:5555
26-
starting worker on tcp://127.0.0.1:5555
27-
starting worker on tcp://127.0.0.1:5555
24+
starting broker on tcp://127.0.0.1:5555
25+
starting worker soda on tcp://127.0.0.1:5555
26+
starting worker tea on tcp://127.0.0.1:5555
27+
starting worker coffee on tcp://127.0.0.1:5555
28+
starting worker tea on tcp://127.0.0.1:5555
2829
---------- Started -----------
2930
requesting 'cola' from 'soda'
3031
requesting 'oolong' from 'tea'
@@ -41,27 +42,30 @@ registered worker 00800041a9 for 'tea'
4142
dispatching 'tea' 00800041ab req -> 00800041a7
4243
dispatching 'tea' 00800041ac req -> 00800041a9
4344
dispatching 'coffee' 00800041af req -> 00800041a8
44-
dispatching 'tea' 00800041ac <- rep 00800041a9
45-
dispatching 'tea' 00800041ad req -> 00800041a9
46-
received 'sencha' from 'tea'
47-
dispatching 'tea' 00800041ad <- rep 00800041a9
48-
dispatching 'tea' 00800041ae req -> 00800041a9
49-
received 'earl grey, with milk' from 'tea'
5045
dispatching 'coffee' 00800041af <- rep 00800041a8
5146
dispatching 'coffee' 00800041b0 req -> 00800041a8
5247
received 'cappuccino' from 'coffee'
48+
dispatching 'tea' 00800041ab <- rep 00800041a7
49+
dispatching 'tea' 00800041ad req -> 00800041a7
50+
received 'oolong' from 'tea'
5351
dispatching 'coffee' 00800041b0 <- rep 00800041a8
5452
dispatching 'coffee' 00800041b1 req -> 00800041a8
5553
received 'latte, with soy milk' from 'coffee'
5654
dispatching 'coffee' 00800041b1 <- rep 00800041a8
5755
dispatching 'coffee' 00800041b2 req -> 00800041a8
5856
received 'espresso' from 'coffee'
57+
registered worker 00800041b3 for 'soda'
58+
dispatching 'soda' 00800041aa req -> 00800041b3
59+
dispatching 'soda' 00800041aa <- rep 00800041b3
60+
received 'cola' from 'soda'
61+
dispatching 'tea' 00800041ac <- rep 00800041a9
62+
dispatching 'tea' 00800041ae req -> 00800041a9
63+
received 'sencha' from 'tea'
5964
dispatching 'tea' 00800041ae <- rep 00800041a9
6065
received 'jasmine' from 'tea'
6166
dispatching 'coffee' 00800041b2 <- rep 00800041a8
6267
received 'irish coffee' from 'coffee'
63-
dispatching 'tea' 00800041ab <- rep 00800041a7
64-
received 'oolong' from 'tea'
65-
timeout expired waiting for 'soda'
68+
dispatching 'tea' 00800041ad <- rep 00800041a7
69+
received 'earl grey, with milk' from 'tea'
6670
---------- Stopping -----------
6771
```

examples/majordomo/index.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ async function sleep(msec: number) {
99
})
1010
}
1111

12+
class SodaWorker extends Worker {
13+
service = "soda"
14+
15+
override async process(...msgs: Buffer[]): Promise<Buffer[]> {
16+
await sleep(Math.random() * 1000)
17+
return msgs
18+
}
19+
}
20+
1221
class TeaWorker extends Worker {
1322
service = "tea"
1423

@@ -29,7 +38,12 @@ class CoffeeWorker extends Worker {
2938

3039
const broker = new Broker()
3140

32-
const workers = [new TeaWorker(), new CoffeeWorker(), new TeaWorker()]
41+
const workers = [
42+
new SodaWorker(),
43+
new TeaWorker(),
44+
new CoffeeWorker(),
45+
new TeaWorker(),
46+
]
3347

3448
async function request(
3549
service: string,
@@ -46,12 +60,12 @@ async function request(
4660
console.log(`received '${res.join(", ")}' from '${service}'`)
4761
return res
4862
} catch (err) {
49-
console.log(`timeout expired waiting for '${service}'`)
63+
console.log(`timeout expired waiting for '${service}'`, err)
5064
}
5165
}
5266

5367
async function main() {
54-
const started = Promise.all([
68+
const _started = Promise.all([
5569
// start the broker
5670
broker.start(),
5771
// start the workers
@@ -81,8 +95,6 @@ async function main() {
8195
// stop the workers
8296
...workers.map(worker => worker.stop()),
8397
])
84-
// await outstanding promises
85-
await started
8698
}
8799

88100
main().catch(err => {

examples/majordomo/worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class Worker {
1313
}
1414

1515
async start() {
16-
console.log(`starting worker on ${this.address}`)
16+
console.log(`starting worker ${this.service} on ${this.address}`)
1717
await this.socket.send([null, Header.Worker, Message.Ready, this.service])
1818

1919
for await (const [_blank1, _header, _type, client, _blank2, ...req] of this

examples/queue/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {Queue} from "./queue"
44

55
async function main() {
66
const sender = new Dealer()
7-
await sender.bind("tcp://127.0.0.1:5555")
8-
console.log("sender bound to port 5555")
7+
await sender.bind("tcp://127.0.0.1:4444")
8+
console.log("sender bound to port 4444")
99

1010
const queue = new Queue(sender)
1111

@@ -16,8 +16,8 @@ async function main() {
1616
])
1717

1818
const receiver = new Dealer()
19-
receiver.connect("tcp://127.0.0.1:5555")
20-
console.log("receiver connected to port 5555")
19+
receiver.connect("tcp://127.0.0.1:4444")
20+
console.log("receiver connected to port 4444")
2121

2222
for await (const [msg] of receiver) {
2323
if (msg.length === 0) {

0 commit comments

Comments
 (0)