Fixed lib and finished implementing it
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Jonatan Nilsson 2022-03-10 13:08:02 +00:00
parent 4024f1269a
commit 67606b9b3b
4 changed files with 72 additions and 3 deletions

View file

@ -379,7 +379,7 @@ export default class Application extends EventEmitter {
rej(errTimeout)
}, this.config.startWaitUntilFail)
let startRes = this.module.start(this.http, this.config.port, this.ctx)
let startRes = this.module.start(this.http, this.config.port || null, this.ctx)
if (startRes && startRes.then) {
return startRes.then(res, rej)
}

View file

@ -1,6 +1,7 @@
import Util from './util.mjs'
import getLog from './log.mjs'
import GetDB from './db.mjs'
import Application from './application.mjs'
import StaticProvider from './providers/static.mjs'
import Core from './core.mjs'
@ -11,7 +12,7 @@ export default class ServiceCore {
}
this._root_import_meta_url = root_import_meta_url
this.util = new Util(this._root_import_meta_url)
this.dbfilename = dbname
this.dbfilename = dbfilename
this.log = getLog(name)
this.name = name
this.config = {}
@ -20,6 +21,10 @@ export default class ServiceCore {
this.app = null
}
setConfig(config) {
this.config[this.name] = config
}
async init(module = null) {
this.db = await GetDB(this.config, this.log, this.dbfilename)
this.core = new Core(this.db, this.util, this.log)

View file

@ -1,6 +1,6 @@
import bunyan from 'bunyan-lite'
import { runner } from './core/runner.mjs'
import { ServiceCore } from './core/lib.mjs'
import ServiceCore from './core/lib.mjs'
import Core from './core/core.mjs'
import Application from './core/application.mjs'
import Util from './core/util.mjs'

64
test/lib.test.mjs Normal file
View file

@ -0,0 +1,64 @@
import { Eltro as t, assert, stub } from 'eltro'
import * as sc from '../index.mjs'
t.describe('', function() {
const module = {
start: stub()
}
t.beforeEach(function() {
module.start.reset()
})
t.test('should have ServiceCore defined', function() {
assert.ok(sc.ServiceCore)
})
t.test('constructor should work', function() {
const assertAppName = 'Gondola'
let core = new sc.ServiceCore(assertAppName, import.meta.url)
assert.strictEqual(core.util._root_import_meta_url, import.meta.url)
assert.strictEqual(core.name, assertAppName)
})
t.test('should support proper init', async function() {
const assertAppName = 'Hero Combat'
let core = new sc.ServiceCore(assertAppName, import.meta.url)
await core.init(module)
assert.strictEqual(core.app.name, assertAppName)
assert.strictEqual(core.app.module, module)
})
t.test('should call module start', async function() {
const assertError = new Error('Inbo')
module.start.rejects(assertError)
let core = new sc.ServiceCore('testapp', import.meta.url)
await core.init(module)
let err = await assert.isRejected(core.run())
assert.strictEqual(err, assertError)
assert.strictEqual(module.start.firstCall[0], core.app.http)
assert.strictEqual(module.start.firstCall[1], null)
assert.strictEqual(module.start.firstCall[2], core.app.ctx)
})
t.test('should support overwriting config', async function() {
const assertError = new Error('Inbo')
const assertPort = 9382
module.start.rejects(assertError)
let core = new sc.ServiceCore('testapp', import.meta.url)
core.setConfig({
port: assertPort
})
await core.init(module)
let err = await assert.isRejected(core.run())
assert.strictEqual(err, assertError)
assert.strictEqual(module.start.firstCall[0], core.app.http)
assert.strictEqual(module.start.firstCall[1], assertPort)
assert.strictEqual(module.start.firstCall[2], core.app.ctx)
})
})