Skip to content

Commit

Permalink
test: fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 committed Oct 1, 2024
1 parent a8dd363 commit dc34f15
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions packages/serverless/test/backend_connector_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const uninstrumentedHttp = require('../src/uninstrumentedHttp');
const config = require('../../core/test/config');
const delay = require('../../core/test/test_util/delay');
const retry = require('../../core/test/test_util/retry');
const { set } = require('lodash');

const sendBundle = async () => {
return new Promise(resolve => {
Expand All @@ -32,18 +33,23 @@ describe('[UNIT] backend connector', () => {
this.timeout(config.getTestTimeout());

let onStub;
let onceStub;
let destroyStub;
let setTimeoutStub;

beforeEach(() => {
sinon.spy(global, 'setInterval');
sinon.spy(global, 'clearInterval');

onStub = sinon.stub();
onceStub = sinon.stub();
destroyStub = sinon.stub();
setTimeoutStub = sinon.stub();

sinon.stub(uninstrumentedHttp.http, 'request').returns({
on: onStub,
setTimeout: sinon.stub(),
once: onceStub,
setTimeout: setTimeoutStub,
end: sinon.stub(),
removeAllListeners: sinon.stub(),
destroy: destroyStub
Expand Down Expand Up @@ -72,7 +78,7 @@ describe('[UNIT] backend connector', () => {
expect(uninstrumentedHttp.http.request.called).to.be.true;
expect(uninstrumentedHttp.http.request.callCount).to.eql(1);

const onError = onStub.getCalls().find(call => call.firstArg === 'error').callback;
const onError = onceStub.getCalls().find(call => call.firstArg === 'error').callback;
onError();

expect(global.clearInterval.called).to.be.true;
Expand All @@ -86,19 +92,21 @@ describe('[UNIT] backend connector', () => {
expect(uninstrumentedHttp.http.request.called).to.be.true;
expect(uninstrumentedHttp.http.request.callCount).to.eql(1);

return retry(async () => {
const prom = sendBundle();
await delay(200);
expect(setTimeoutStub.called).to.be.true;
expect(destroyStub.called).to.be.false;

const onTimeout = onStub.getCalls().find(call => call.firstArg === 'timeout').callback;
const onEnd = uninstrumentedHttp.http.request
.getCalls()
.find(call => call.firstArg.path === '/bundle').callback;
const firstCallArgs = setTimeoutStub.getCall(0).args;

onTimeout();
expect(global.clearInterval.called).to.be.true;
await delay(200);

setTimeout(onEnd, 200);
// simulate timeout of extension
firstCallArgs[1]();

return retry(async () => {
expect(destroyStub.called).to.be.true;

const prom = sendBundle();
await delay(200);
await prom;

expect(destroyStub.called).to.be.true;
Expand All @@ -117,7 +125,7 @@ describe('[UNIT] backend connector', () => {
const prom = sendBundle();
await delay(200);

const onError = onStub.getCalls().find(call => call.firstArg === 'error').callback;
const onError = onceStub.getCalls().find(call => call.firstArg === 'error').callback;
const onEnd = uninstrumentedHttp.http.request
.getCalls()
.find(call => call.firstArg.path === '/bundle').callback;
Expand Down Expand Up @@ -150,11 +158,14 @@ describe('[UNIT] backend connector', () => {
const prom = sendBundle();
await delay(200);

const onceFinish = onceStub.getCalls().find(call => call.firstArg === 'finish').callback;
const onFinish = onStub.getCalls().find(call => call.firstArg === 'finish').callback;
const onEnd = uninstrumentedHttp.http.request
.getCalls()
.find(call => call.firstArg.path === '/bundle').callback;

onceFinish();

setTimeout(onEnd, 250);
setTimeout(onFinish, 200);

Expand Down

0 comments on commit dc34f15

Please sign in to comment.