/
test.ts
77 lines (76 loc) · 2.89 KB
/
test.ts
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
import {sd_notify} from "./index"
import {createSocket, ReceiveInfo} from "unix-dgram"
// import {} from "mocha"
import * as chai from "chai"
import * as os from "os"
import {unlinkSync} from "fs"
describe("sd_notify", () => {
it("should send message on socket", async () => {
const socket = createSocket('unix_dgram')
const path = `${__dirname}/dist/test-socket`
process.env.NOTIFY_SOCKET = path
socket.bind(path)
try {
const receivedMessagePromise = new Promise((resolve, reject) => {
socket.on("message", (buf: Buffer, rinfo: ReceiveInfo) => {
resolve(buf)
})
socket.on("error", reject)
})
chai.expect(await sd_notify("hello")).to.be.true
const msg = await receivedMessagePromise as Buffer
chai.expect(msg.toString("hex"))
.to.equal(Buffer.from("hello").toString("hex"))
} finally {
try {
socket.close()
} finally {
unlinkSync(path)
}
}
})
it("should delete NOTIFY_SOCKET if unsetEnvironment", async () => {
const socket = createSocket('unix_dgram')
const path = `${__dirname}/dist/test-socket`
process.env.NOTIFY_SOCKET = path
socket.bind(path)
try {
chai.expect(await sd_notify("hello")).to.be.true
chai.expect(process.env.NOTIFY_SOCKET).to.equal(path)
chai.expect(await sd_notify("hello", {unsetEnvironment: true})).to.be.true
chai.expect(process.env.NOTIFY_SOCKET).to.be.undefined
} finally {
try {
socket.close()
} finally {
unlinkSync(path)
}
}
})
it("should resolve to false when NOTIFY_SOCKET is unset", async () => {
delete process.env.NOTIFY_SOCKET
chai.expect(await sd_notify("hello")).to.be.false
chai.expect(await sd_notify("hello", {unsetEnvironment: true})).to.be.false
})
it("should be able to connect to abstract namespace sockets on linux", async () => {
if (os.platform() !== "linux") return
const socket = createSocket('unix_dgram')
const name = "sd-notify-test"
process.env.NOTIFY_SOCKET = `@${name}`
socket.bind(`\0${name}`)
try {
const receivedMessagePromise = new Promise((resolve, reject) => {
socket.on("message", (buf: Buffer, rinfo: ReceiveInfo) => {
resolve(buf)
})
socket.on("error", reject)
})
chai.expect(await sd_notify("hello")).to.be.true
const msg = await receivedMessagePromise as Buffer
chai.expect(msg.toString("hex"))
.to.equal(Buffer.from("hello").toString("hex"))
} finally {
socket.close()
}
})
})