bun icon indicating copy to clipboard operation
bun copied to clipboard

added a few tests for #7716

Open davidstevens37 opened this issue 9 months ago • 1 comments

What does this PR do?

Add a few tests for - node:http and #7716

How did you verify your code works?

Tests

Node.js Equivalent

import { describe, it } from 'node:test';
import assert from 'node:assert';
import { createServer } from 'node:http';

function expect(value) {
  return {
    toBe: function (expected) {
      assert.equal(value, expected);
    }
  }
}

describe('node:http', async () => {
  describe('IncomingRequest', async () => {
    it("emits `close` event when client disconnects", async () => {
      try {
        let close_event_emitted = false;
        var server = createServer((req, res) => {
          res.writeHead(200, { 'Content-Type': 'text/html' });
          const responseInterval = setInterval(() => {
            res.write('hello world');
            clearInterval(responseInterval);
            throw new Error('timeout not cleaned up -- memmory leak')
          }, 100);
          req.on('close', () => {
            close_event_emitted = true;
            clearInterval(responseInterval);
          });
        });
        await new Promise(resolve => {
          server.listen(3000, resolve);
        })
        const controller = new AbortController();
        setTimeout(() => controller.abort(), 50);
        const res = await fetch("http://localhost:3000", { signal: controller.signal }).catch(e => e.name);
        await new Promise(resolve => setTimeout(resolve, 0)); // allow the server a chance to handle the event and set the flag
        expect(res).toBe("AbortError");
        expect(close_event_emitted).toBe(true);
      } catch (e) {
        throw e;
      } finally {
        server.close();
      }
    });
  });
});

Output:

λ node tests.js                                     
▶ node:http
  ▶ IncomingRequest
    ✔ emits `close` event when client disconnects (57.005708ms)
  ▶ IncomingRequest (58.12275ms)

▶ node:http (58.792375ms)

ℹ tests 1
ℹ suites 2
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 62.561542

davidstevens37 avatar May 08 '24 10:05 davidstevens37

@nektro Thank you again for your support on 7716. Sorry, I'm unable to contribute any more, but the least I could do is add some tests. I hope it's helpful!

davidstevens37 avatar May 08 '24 10:05 davidstevens37

Do I understand correctly that these tests currently fail in the latest version of bun? If so, they should be changed from it(... to it.todo(..., as they're known-failing

gvilums avatar May 08 '24 23:05 gvilums

@gvilums the first test should emit `close` event on IncomingRequest when request is destroyed is passing. it correctly emits the close event on the req when req.destroy() is called.

The second test should emit `close` event on IncomingRequest when client disconnects is failing as it does not emit the close event on the req when the client disconnects.

I've updated the second test to .todo() thanks!

davidstevens37 avatar May 09 '24 02:05 davidstevens37

we shouldn't merge failing tests but leaving them as comments on the original issue will be the best place to leave them for whoever takes up the issue next (whether that be me or someone else). appreciate the reductions! :)

nektro avatar May 09 '24 02:05 nektro

okay, thanks @nektro! I'll close this PR out and move the tests to original issue.

davidstevens37 avatar May 09 '24 04:05 davidstevens37