-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongo-client): implement
MongoClient.prototype.startSession
- Loading branch information
Showing
3 changed files
with
109 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
const MongoClient = require('../..').MongoClient; | ||
const expect = require('chai').expect; | ||
const assign = require('../../lib/utils').assign; | ||
const mock = require('../mock'); | ||
|
||
const test = {}; | ||
describe('Sessions', function() { | ||
describe('unit', function() { | ||
afterEach(() => mock.cleanup()); | ||
beforeEach(() => { | ||
return mock.createServer().then(server => { | ||
test.server = server; | ||
}); | ||
}); | ||
|
||
it('should throw an exception if sessions are not supported', { | ||
metadata: { requires: { topology: 'single' } }, | ||
test: function(done) { | ||
test.server.setMessageHandler(request => { | ||
var doc = request.document; | ||
if (doc.ismaster) { | ||
request.reply(assign({}, mock.DEFAULT_ISMASTER)); | ||
} | ||
}); | ||
|
||
MongoClient.connect(`mongodb://${test.server.uri()}/test`, function(err, client) { | ||
expect(err).to.not.exist; | ||
expect(() => { | ||
client.startSession(); | ||
}).to.throw(/Current topology does not support sessions/); | ||
|
||
client.close(); | ||
done(); | ||
}); | ||
} | ||
}); | ||
|
||
it('should return a client session when requested if the topology supports it', { | ||
metadata: { requires: { topology: 'single' } }, | ||
|
||
test: function(done) { | ||
test.server.setMessageHandler(request => { | ||
var doc = request.document; | ||
if (doc.ismaster) { | ||
request.reply( | ||
assign({}, mock.DEFAULT_ISMASTER, { | ||
logicalSessionTimeoutMinutes: 10 | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
MongoClient.connect(`mongodb://${test.server.uri()}/test`, function(err, client) { | ||
expect(err).to.not.exist; | ||
let session = client.startSession(); | ||
expect(session).to.exist; | ||
|
||
client.close(); | ||
done(); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |