fix: convert network broker stopped lifecycle method to async (#31927)
Co-authored-by: Diego Sampaio <8591547+sampaiodiego@users.noreply.github.com>pull/31810/head^2
parent
703cb7ebb0
commit
b876e4e0fc
@ -0,0 +1,6 @@ |
||||
--- |
||||
"@rocket.chat/meteor": patch |
||||
"@rocket.chat/core-services": patch |
||||
--- |
||||
|
||||
`stopped` lifecycle method was unexpectedly synchronous when using microservices, causing our code to create race conditions. |
||||
@ -0,0 +1,39 @@ |
||||
import { ServiceClass } from '@rocket.chat/core-services'; |
||||
import { expect } from 'chai'; |
||||
import sinon from 'sinon'; |
||||
|
||||
import { BrokerMocked } from '../../../../tests/mocks/server/BrokerMocked'; |
||||
import { NetworkBroker } from '../../../server/NetworkBroker'; |
||||
|
||||
class DelayedStopBroker extends BrokerMocked { |
||||
async destroyService(name: string) { |
||||
const instance = this.services.get(name); |
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000)); |
||||
|
||||
await instance.stopped(); |
||||
|
||||
await super.destroyService(name); |
||||
} |
||||
} |
||||
|
||||
const broker = new NetworkBroker(new DelayedStopBroker() as any); |
||||
|
||||
describe('NetworkBroker', () => { |
||||
it('should wait services to be fully destroyed', async () => { |
||||
const stoppedStub = sinon.stub(); |
||||
|
||||
const instance = new (class extends ServiceClass { |
||||
name = 'test'; |
||||
|
||||
async stopped() { |
||||
stoppedStub(); |
||||
} |
||||
})(); |
||||
|
||||
broker.createService(instance); |
||||
await broker.destroyService(instance); |
||||
|
||||
expect(stoppedStub.called).to.be.true; |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue