Mon, 13 Sep 2010 09:37:26 +0200 -n
Emmanuel Lecharny <[email protected]> írta:

> On 9/13/10 9:20 AM, Kúti Zsolt wrote:
> > Hello,
> >
> > My decoder worked fine until fragmentation of data happend.
> > Test case is needed to cover this.
> >
> > Not knowing better I come up with code that feeds my decoder's
> > doDcode() method with fragmented data.
> >
> > I am curious how others solve this.
> 
> It depends on the protocol you are implementing. In any case, you
> have to gather the bytes that will produce a message once decoded.
> This can be done with a CumulativePotocolDecoder if you are usig a
> text based protocol with a clear limit (new line, a specific char,
> etc.) If you have a binay protocol, you'll have to handle
> fragmentation yourself.
> 
> The biggest advantage of the CumulativeProtocolDecoder is that not
> only you can tell it about the message boundary, but it will also
> call the decode() methd as many time as you have messages to decode
> (as you may have a fragmented message, you may also receive more than
> one message in a single buffer).
 
Hello Emmanuel,

Sorry for not being clear enough to outline my case!
I subclassed CumulativeProtocolDecoder. My decoder worked fine until
fragmentation happened in real life testing. Then, I have changed my
decoder to handle fragmentation and this works now in practice, too.
My question is: how to write a correct unit test for checking
fragmentation's case?

This is my solution:

@Test
    public void decodeFragmented() throws Exception {
        DummySession ios = new DummySession();
        MockCommand cmd = new MockCommand();
        initSession(ios, cmd);
        ProtocolDecoderOutput pdo = new ProtocolDecoderOutput() {
            public void write(Object o) {}
            public void flush(IoFilter.NextFilter nextFilter, IoSession
ioSession) {} };
        CommandDecoder dec = new CommandDecoder();
        byte[] fragment = new byte[] {1, 2, 3, 4, 5};
        IoBuffer buf = IoBuffer.allocate(fragment.length);
        buf.setAutoExpand(true);

        buf.put(fragment);   // first part
        buf.flip(); // mark buffer

        // only first part is given, needs more decoding
        assert dec.doDecode(ios, buf, pdo);

        buf.clear();
        buf.put(fragment);   // second part, no more needed
        buf.flip(); // mark buffer
        assert !dec.doDecode(ios, buf, pdo);
        assert cmd.getResult() != null;
    }

initSessions() put cmd into session, my decoder takes it from session
and delegates the real decoding work to the MockCommand.

Thanks!
Zsolt

Reply via email to