2

抱歉,这个有点乱。我的项目在nodejs中。我有一个摩卡测试。在其中我打开一个到 geteventstore 的连接并订阅一个流。这基本上开始发出事件。

我将该事件订阅包装在 rxjs 可观察对象中,然后将其写入控制台。

一半的时间我得到一个充满事件的流一半的时间我没有。

我感觉到事件循环开始监听,没有听到任何声音并在 geteventstore 开始用事件爆破它之前关闭。

我有点不知所措。我可以告诉 geteventstore 正在发送数据,因为我得到它的一半时间。我的理解是,只要有人订阅了一个事件,例如有一个事件监听器,循环就会保持打开状态。

所以也许问题出在 rxjs 上?

我不知道,任何帮助将不胜感激。

- - 编辑

我不知道这是否会有所帮助,但测试看起来像这样。

context('when calling subscription', ()=> {
    it('should stay open', function () {
        mut = bootstrap.getInstanceOf('gesConnection');
        var rx = bootstrap.getInstanceOf('rx');
        var subscription = mut.subscribeToAllFrom();

        rx.Observable.fromEvent(subscription, 'event').forEach(x=> console.log(x));

        subscription.on('event', function (payload) {
            console.log('event received by dispatcher');
            console.log('event processed by dispatcher');
        });
        mut._handler._connectingPhase.must.equal('Connected');
    })
});

所以 mut 是到 geteventstore 的连接,rx 是 rxjs,订阅对象是一个事件发射器,它将数据从 geteventstore 中抽出。

我知道这个问题是因为它涉及至少两个有点不寻常的产品,geteventstore 和 rxjs。

我的意思是我非常有信心 gesConnection 和订阅实际上是连接和发射的。我只是不知道如何进一步测试/调查。

谢谢

4

1 回答 1

1

我没有看到您使用Mocha 的异步测试工具

MochaJs 不知道它应该等待您的测试比您的函数返回所需的时间更长。

通常你会返回一个承诺:

    it('must stay open', () => {
        mut = bootstrap.getInstanceOf('gesConnection');
        var rx = bootstrap.getInstanceOf('rx');
        var subscription = mut.subscribeToAllFrom();

        subscription.on('event', function (payload) {
            console.log('event received by dispatcher');
            console.log('event processed by dispatcher');
        });

        var promise = rx.Observable
            .fromEvent(subscription, 'event')
            .take(100) // stop test after 100 events
            .do(x => console.log(x))
            .finally(() => {
                // do any cleanup here.
                // such as close your connection
                // or "subscription" variable
            })
            .toPromise();

        mut._handler._connectingPhase.must.equal('Connected');

        // tells Mocha to wait until the observable completes
        return promise;
    });
于 2015-07-07T21:58:04.243 回答