aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/resolve-dns.test.ts
blob: f0db49b206376c3877c9899b5e0b496962ed491b (plain) (blame)
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
import { dns } from "bun";
import { describe, expect, it, test } from "bun:test";
import { withoutAggressiveGC } from "gc";

describe("dns.lookup", () => {
  const backends = [
    process.platform === "darwin" ? "system" : undefined,
    "libc",
    "c-ares",
  ].filter(Boolean);
  for (let backend of backends) {
    it(backend + " parallell x 10", async () => {
      const promises = [];
      for (let i = 0; i < 10; i++) {
        promises.push(dns.lookup("localhost", { backend }));
      }
      const results = (await Promise.all(promises)).flat();
      withoutAggressiveGC(() => {
        for (let { family, address } of results) {
          if (family === 4) {
            expect(address).toBe("127.0.0.1");
          } else if (family === 6) {
            expect(address).toBe("::1");
          } else {
            throw new Error("Unknown family");
          }
        }
      });
    });

    it(backend + " remote", async () => {
      const [first, second] = await dns.lookup("google.com", { backend });
      console.log(first, second);
    });
    it(backend + " local", async () => {
      const [first, second] = await dns.lookup("localhost", { backend });
      console.log(first, second);
    });

    it(
      backend +
        " failing domain throws an error without taking a very long time",
      async () => {
        try {
          await dns.lookup("yololololololo1234567.com", { backend });
          throw 42;
        } catch (e) {
          expect(typeof e).not.toBe("number");
          expect(e.code).toBe("DNS_ENOTFOUND");
        }
      },
    );
  }
});