Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ function setupDataSources(app, instructions) {
useEnvVars: true,
};
obj = getUpdatedConfigObject(app, obj, opts);
var lazyConnect = process.env.LB_LAZYCONNECT_DATASOURCES;
if (lazyConnect) {
obj.lazyConnect =
lazyConnect === 'false' || lazyConnect === '0' ? false : true;
}
app.dataSource(key, obj);
});
}
Expand Down
56 changes: 56 additions & 0 deletions test/executor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,62 @@ describe('executor', function() {
});
});

describe('when booting with lazy connect', function() {
var SAMPLE_INSTRUCTION = someInstructions({
dataSources: {
lazyConnector: {
connector: 'testLazyConnect',
name: 'lazyConnector',
},
},
});
var connectTriggered = true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: the value is always reset in beforeEach hook below, there is no need to assign a value here.

var connectTriggered;
beforeEach(function() {
  connectTriggered = true;
  // ...
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bajtos actually the variable will not reset lol. it's defined outside the hook, and stay the same value as previous test without reset.

But i think a more reasonable logic is

if (dataSource.settings.lazyConnect) {
     connectTriggered = false;
} else {
     connectTriggered = true;
}

then there is no need to reset it at the beginning.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do reset it at the beginning. If it happens that the code in your custom connector does not get called by some weird error that is swallowed, or perhaps because the connector API changed and our mocked connector is no longer valid, then test will see the result of the previous call, which is not right.

IMO, the most correct solution is to:

  • reset the variable in beforeEach hook to undefined or null
  • inside the connector, set the variable to true/false depending on the value of lazyConnect, as you shown in the code snippet above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, will fix it with a reset


beforeEach(function() {
app.connector('testLazyConnect', {
initialize: function(dataSource, callback) {
if (dataSource.settings.lazyConnect) {
connectTriggered = false;
} else {
connectTriggered = true;
}
},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this configuration is same for both tests, please refactor it into a shared variable, e.g. DATASOURCE_THROWING_CONNECTION_ERROR

});
});

it('should trigger connect with ENV undefined', function(done) {
delete process.env.LB_LAZYCONNECT_DATASOURCES;
boot.execute(app, SAMPLE_INSTRUCTION, function() {
expect(connectTriggered).to.equal(true);
done();
});
});

it('should not trigger connect with ENV true', function(done) {
process.env.LB_LAZYCONNECT_DATASOURCES = 'true';
boot.execute(app, SAMPLE_INSTRUCTION, function() {
expect(connectTriggered).to.equal(false);
done();
});
});

it('should trigger connect with ENV false', function(done) {
process.env.LB_LAZYCONNECT_DATASOURCES = 'false';
boot.execute(app, SAMPLE_INSTRUCTION, function() {
expect(connectTriggered).to.equal(true);
done();
});
});

it('should trigger connect with ENV 0', function(done) {
process.env.LB_LAZYCONNECT_DATASOURCES = '0';
boot.execute(app, SAMPLE_INSTRUCTION, function() {
expect(connectTriggered).to.equal(true);
done();
});
});
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit-tests for the following cases too - notice the value is always a string!

  • LB_LAZYCONNECT_DATASOURCES='false'
  • LB_LAZYCONNECT_DATASOURCES='0'

describe('dynamic configuration for datasources.json', function() {
beforeEach(function() {
delete process.env.DYNAMIC_HOST;
Expand Down