Skip to content

Testing event loop based callbacks/delegates #5

@shripadk

Description

@shripadk

How do I test callbacks/delegates which are not triggered
immediately? Especially when I need to do unit tests with an
event loop?
A simple example from my codebase (SaaSy here is a custom HTTP
client to an API endpoint):

class TestSaaSy {
    mixin TestMixin;

    // this works
    void test_encoded_auth() {
        auto ev = new EventLoop;
        auto saasy = new SaaSy(ev, "test", "test");
        assert(saasy.encoded_auth == "dGVzdDp0ZXN0dGVzdA==",  "encoding issue");
        ev.close;
        ev.run;
    }

    // won't work. test gets finished even before the callback is fired!
    void test_get_subscription() {
        auto ev = new EventLoop;
        auto saasy = new SaaSy(ev, "test", "test");
        saasy.getSubscription("ref363466", (bool err, string response) {
            assert(err == true);
            ev.close;
        });
        ev.run;
    }
}

Discussion in D forum: http://forum.dlang.org/thread/znwsshaqilmrfxswavjw@forum.dlang.org?page=2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions