Skip to content

Commit

Permalink
Refactor Leader class and add new test case
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmolyuk committed Apr 2, 2024
1 parent 5f8e21f commit f3543a0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ lint:
@PHONY: lint

test:
npx jest tests/*.js
npx jest --detectOpenHandles tests/*.js
@PHONY: test

upgrade:
Expand Down
25 changes: 18 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Leader extends EventEmitter {
this.options = {}
this.options.ttl = Math.max(options.ttl || 0, 1000) // Lock time to live
this.options.wait = Math.max(options.wait || 0, 100) // Time between tries to be elected
this.key =
'leader-' +
crypto
.createHash('sha1')
.update(options.key || 'default')
.digest('hex')
this.stopped = false // Flag to stop the leader election

this.initDatabase().then(() => this.elect())
const hash = crypto
.createHash('sha1')
.update(options.key || 'default')
.digest('hex')

this.key = `leader-${hash}`
}

initDatabase() {
Expand Down Expand Up @@ -56,6 +56,7 @@ class Leader extends EventEmitter {
}

elect() {
if (this.stopped) return
this.db
.collection(this.key)
.findOneAndUpdate(
Expand All @@ -74,6 +75,7 @@ class Leader extends EventEmitter {
}

renew() {
if (this.stopped) return
this.db
.collection(this.key)
.findOneAndUpdate(
Expand All @@ -90,6 +92,15 @@ class Leader extends EventEmitter {
}
})
}

stop() {
this.stopped = true
}

start() {
this.stopped = false
this.initDatabase().then(() => this.elect())
}
}

module.exports = { Leader }
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"homepage": "https://github.com/andrewmolyuk/mongo-leader#readme",
"devDependencies": {
"@jest/globals": "^29.7.0",
"eslint": "^8.57.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
Expand Down
23 changes: 16 additions & 7 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
'use strict'

const assert = require('assert')
const { describe, it, expect, afterEach } = require('@jest/globals')

describe('Your Test Suite', () => {
it('should test something', () => {
// Your test code here
})
const { Leader } = require('../index')
const { mockDb } = require('./mockDb')

describe('Leader', () => {
describe('constructor', () => {
it('should set default options', function () {
// Act
const leader = new Leader(mockDb)

it('should test something else', () => {
// Your test code here
// Assert
expect(leader.db).toBe(mockDb)
expect(leader.options).not.toBeNull()
expect(leader.options.ttl).toBe(1000)
expect(leader.options.wait).toBe(100)
expect(leader.key).toMatch(/^leader-?/)
})
})
})
32 changes: 32 additions & 0 deletions tests/mockDb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const mockAdmin = {
command: () => Promise.resolve(),
}

const mockDb = {
command: () => Promise.resolve({}),
admin: () => mockAdmin,
listCollections() {
return {
hasNext() {
return Promise.resolve(false)
},
}
},
collection(key) {
return {
createIndex() {
return Promise.resolve()
},
findOneAndUpdate() {
return Promise.resolve()
},
}
},
createCollection() {
return Promise.resolve()
},
}

module.exports = { mockAdmin, mockDb }

0 comments on commit f3543a0

Please sign in to comment.