lundi 24 septembre 2018

RxCpp: How to make a UnitTest for hot observable

I have a simple class CFrameParser for processing Socket byte stream into frames.

class CDataLinkFrame {
public:
    CDataLinkFrame();
    void consumeByte(char ch);
    observable<std::string> getFramesObservable();
private:
    subjects::subject<std::string> _framesSubject;
}

Clients will subscribe for frames by:

dlf.getFramesObservable().subcribe()

It is a hot observable, socket data is consumed in a separate thread by:

dlf.consumeByte(ch)

and frames are published by:

_framesSubject.get_subscriber().on_next(...);

How to make a UnitTest to test my own RxCpp observable?

I want to subscribe to CDataLinkFrameget.getFramesObservable(), then pass a predefined byte stream to it and test for expected frames messages:

const char testData[] = {(char) 0x1, (char) 0x02, (char) 0xFD, 'H', 'e', 'l', 'l', 'o', '!', (char) 0xFF};
auto observable = dlf.getFramesObservable();
// Subscribe to some kind of test_observer

// Provide some data to fire the frame
dlf.consumeData(buff);

// Test for expected frames. 
// In my case exactly 1 frame of std::string type ('Hello!')

How can I do this in RxCpp?

Aucun commentaire:

Enregistrer un commentaire