roc-streaming / roc-go

Golang bindings for Roc Toolkit.
https://roc-streaming.org
MIT License
22 stars 10 forks source link

Add unit tests for endpoint errors #51

Closed dhanusaputra closed 1 year ago

dhanusaputra commented 1 year ago

close #42

it's not easy to set arguments where the test failed in the not covered errors so use mock func instead any guidance welcome 🙏

gavv commented 1 year ago

Thanks for PR!

You're right, some branches can't be covered without mocks, but apparently these branches are just impossible. For instance, if roc_endpoint_set_uri did not return error, then fromC will never fail.

I think it's OK to keep these impossible cases uncovered, and avoid usage of mocks at all. In these tests, it's much more interesting to cover combination of go bindings + C lib, not just bindings alone, because most bugs will happen when they start working together.

We can still cover a few possible cases without need for mocks:

Here is a part from URI ragel grammar (endpoint_uri_parse.rl from roc-toolkit):

        proto = ( [a-z0-9+]+ ) >start_token %set_proto;

        host = ('[' [^/@\[\]]+ ']' | [^/:@\[\]]+) >start_token %set_host;
        port = (digit+) >start_token %set_port;

        pchar = [^?#];

        path = ('/' pchar*) >start_token %set_path;
        query = (pchar*) >start_token %set_query;

        uri = ( proto '://' host (':' port)? )? path? ('?' query)?;

Here, path? ('?' query)? corresponds to resource. You can use this grammar to have an idea how to compose invalid resource.

dhanusaputra commented 1 year ago

thanks for your help! did some changes based on your guidance but only able to apply 2, please see below

coveralls commented 1 year ago

Coverage Status

Coverage: 73.784% (+0.8%) from 72.939% when pulling c0cf7890cc36cdeda066c59385ac35071b0e0d23 on dhanusaputra:unit-test-endpoint-errors into c445824928cf569915e65d5a55c8062976feaad1 on roc-streaming:main.

gavv commented 1 year ago

Great, thanks for your analysis.

Could you also add a few more cases please?

Probably these cases won't increase reported coverage, but they're still important.

roc_endpoint_get_uri will fail if proto does not have default port, but port is -1 (e.g. RTP) -> this branch already covered by other test case

roc_endpoint_get_uri will fail if proto does not support resource part, but resource is set (e.g. RTP) -> this branch already covered by other test case

All cases in TestEndpoint use RTSP, which supports default port and resource part. Let's add two cases which try to use RTP (not RTSP) with:

gavv commented 1 year ago

I just got a thought, it would be also nice to add one successful case per each protocol (see here) to be sure that all constants in bindings are correctly mapped to corresponding C values.

(Currently we cover only RTSP).

gavv commented 1 year ago

Thank you!

I also tried to add this test:

when port is zero (should work without error)

And realized that there is a bug, created an issue: https://github.com/roc-streaming/roc-toolkit/issues/519

Here is a small follow-up PR: https://github.com/roc-streaming/roc-go/pull/56