mock-socket icon indicating copy to clipboard operation
mock-socket copied to clipboard

server.close() does not work??

Open Foxhoundn opened this issue 9 years ago • 8 comments

describe.only('Websockets store handlers', () => {
  let store;

  beforeEach(() => {
    store = createStore(ws,
      applyMiddleware(
        thunk,
        promise,
      )
    );
  });

  it('connects the websocket', () => {
    const server = new Server('ws://qorus.example.com/log/main');

    store.subscribe(() => {
      const { data } = store.getState();

      expect(data).to.eql({
        'log/main': {
          loading: true,
          connected: false,
          error: false,
          paused: false,
        },
      });
    });

    store.dispatch(actions.connect('log/main'));

    server.close();
  });

  it('disconnects the websocket', () => {
    const server = new Server('ws://qorus.example.com/log/main');

    store.dispatch(actions.connect('log/main'));
    store.subscribe(() => {
      const { data } = store.getState();

      expect(data).to.eql({
        'log/main': {
          loading: false,
          connected: false,
          error: false,
          paused: false,
        },
      });
    });
    store.dispatch(actions.disconnect('log/main'));

    server.close();
  });
});
  1. Websockets store handlers disconnects the websocket: Error: A mock server is already listening on this url

Foxhoundn avatar Sep 05 '16 08:09 Foxhoundn

Sorry, It's actually server.stop(), but that doesn't really matter since that one does not work either :)

Foxhoundn avatar Sep 05 '16 15:09 Foxhoundn

@Foxhoundn Thanks for finding this. Let me dig into it and see what is happening

thoov avatar Sep 06 '16 20:09 thoov

Because I don't have access to your store/actions its going to be hard for me to debug. Are you able to create a simpler example of this bug without hitting a store?

thoov avatar Sep 06 '16 20:09 thoov

In the meantime I found that force a new connection avoid this issue. But if one test fail all the following tests will trigger this Error: A mock server is already listening on this url error.

new Server('socketioserverurl', {
     forceNew: true,
     reconnection: false
})

Is it possible that the problem is due to reconnection activated by default?

Chalisi avatar Jan 11 '19 09:01 Chalisi

I'm having a similar "already listening" problem, and I tried

new Server('socketioserverurl', {
     forceNew: true,
     reconnection: false
})

and first typescript said those weren't valid keys, and when I @ts-ignore that error, I still have the same 'already listening' problem.

tuzmusic avatar Dec 03 '19 21:12 tuzmusic

I'm also getting this error when I attempt multiple vitest cases, both concurrent and non-concurrent

spacehash avatar Nov 03 '22 03:11 spacehash

@spacehash Can you please provide a minimal reproducible test case?

Atrue avatar Nov 08 '22 07:11 Atrue

In my case it was a sign of a bug in my code/test which was causing it not to reach the close call

dhulme avatar May 15 '23 11:05 dhulme