Skip to content

Commit e366af9

Browse files
committed
Adds singleton test and fixes singleton bug
1 parent ccbcc06 commit e366af9

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

lib/Singleton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var map = {};
22

33
exports.get = function (key, createCb, returnCb) {
44
if (map.hasOwnProperty(key)) {
5-
return map[key];
5+
return returnCb(map[key]);
66
}
77

88
createCb(function (value) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
common.createConnection(function (err, db) {
5+
db.driver.db.query([
6+
"CREATE TEMPORARY TABLE `test_get_singleton` (",
7+
"`id` INT (5) NOT NULL PRIMARY KEY AUTO_INCREMENT,",
8+
"`name` VARCHAR(100) NOT NULL",
9+
")"
10+
].join(""), function () {
11+
db.driver.db.query("INSERT INTO `test_get_singleton` VALUES (1, 'test')", function (err) {
12+
if (err) throw err;
13+
14+
var TestModel = db.define('test_get_singleton');
15+
16+
TestModel.get(1, function (err, Instance1) {
17+
assert.equal(err, null);
18+
TestModel.get(1, function (err, Instance2) {
19+
assert.equal(err, null);
20+
assert.strictEqual(Instance1, Instance2);
21+
db.close();
22+
});
23+
});
24+
});
25+
});
26+
});

0 commit comments

Comments
 (0)