我在Mosca和Node.js上使用TypeScript,并且编写了一个自定义代理来满足我的需求。
当我分别测试方法(使用Jest UT库)时,一切似乎都可以正常工作,直到测试结束,然后出现以下错误:
layoutSubviews
这是我的broker.ts的样子:
Error: This ascoltatore is closed
at TrieAscoltatore.raiseIfClosed [as _raiseIfClosed] ([path]\node_modules\ascoltatori\lib\abstract_ascoltatore.js:72:11)
at TrieAscoltatore.subscribe ([path]\node_modules\ascoltatori\lib\trie_ascoltatore.js:40:8)
at Server.<anonymous> ([path]\node_modules\mosca\lib\server.js:267:22)
at Server.emit (events.js:194:15)
at [path]\node_modules\mosca\lib\server.js:242:12
at makeCall ([path]\node_modules\fastseries\series.js:117:7)
at ResultsHolder.release ([path]\node_modules\fastseries\series.js:96:9)
at Server.NoResultsHolder.release ([path]\node_modules\fastseries\series.js:72:22)
at Object.onceWrapper (events.js:277:13)
at Server.emit (events.js:189:13)
at emitListeningNT (net.js:1304:10)
at process._tickCallback (internal/process/next_tick.js:63:19)
[path]\node_modules\ascoltatori\lib\abstract_ascoltatore.js:72
throw new Error("This ascoltatore is closed");
^
这是我的broker.spec.ts测试文件:
import { Server, Client, Packet } from 'mosca';
/**
* Broker
* @description MQTT Broker
*/
export class Broker {
/** MQTT server */
public server: Server;
constructor(
) {
const settings = {
port: 1883,
host: '127.0.0.1'
};
this.server = new Server(settings);
});
[methods]
}
如您所见,我正在尝试使用import { Broker } from './broker';
import { Packet } from 'mosca';
describe("Broker", () => {
let broker = new Broker;
const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://127.0.0.1');
afterAll(() => {
broker.server.close();
});
[Some tests]
});
语法关闭以new Broker
开头创建的MQTT服务器。但是,由于某种原因,我会收到此错误。
请注意,我尝试在实际测试中使用afterAll
,在此测试中,每当客户端连接到我的经纪人时,我便调用该方法,并且运行良好,服务器在我询问时关闭了
我想知道我在这里做错了什么。该错误使我认为服务器已关闭,但是我不明白为什么不告诉服务器停止运行。