dfinity / pocketic

A canister smart contract testing solution for the Internet Computer.
Other
22 stars 1 forks source link

Timer Support #19

Closed Gekctek closed 4 months ago

Gekctek commented 4 months ago

Is there currently timer support for pocketic? I am using advanceTime() and tick() but nothing seems to be triggering it in my motoko canister

michael-weigelt commented 4 months ago

Hi, PocketIC does support timers, and calling advanceTime and tick should work. How are you setting up your timers?

Gekctek commented 4 months ago

Not exactly sure how to answer this, I tried with a few timers that are simple delays calling start or end of a 'scenario'

createTimer<system>(
    endTime,
    func() : async* () {
        Debug.print("Ending scenario with timer. Scenario id: " # Nat.toText(scenarioId));
        let ?scenario = scenarios.get(scenarioId) else Debug.trap("Scenario not found: " # Nat.toText(scenarioId));
        let teamVotingResult = switch (calculateResultsInternal(scenario, true)) {
            case (#consensus(teamVotingResult)) teamVotingResult;
            case (#noConsensus) Prelude.unreachable();
        };
        await* end(scenario, teamVotingResult);
    },
);

https://github.com/edjCase/daoball/blob/dd01675ce11b9a5e17118d19ae9a7557e60e2f33/src/backend/league/ScenarioHandler.mo#L684

Here is where I am using it in my test. Something is not right, not just with timers, so I might be doing something wrong The idea is I have a 'scenario' that users vote on and will trigger resolving the scenario on majority vote or on expiration of the scenario, neither seem to be triggering https://github.com/edjCase/daoball/blob/dd01675ce11b9a5e17118d19ae9a7557e60e2f33/src/frontend/test/pocket-ic/scenario.test.ts#L378

I first found this problem when I had my scenario 'start' timer, but then i just did an optimization to not create a timer if it starts 'now', which solved that issue

The common thing amongst all of these scenarios, timers and voting majority, is they call an await like `await end(...)` which has async calls in it

This has been working for months using my local and ic canisters, just not on pocket IC

Any help on diagnosing the issue would be great. Thanks

nathanosdev commented 4 months ago

Calls to setTime, advanceTime and tick should all be await'ed. The lack of await on these calls is the likely cause of your issue.

Gekctek commented 4 months ago

Calls to setTime, advanceTime and tick should all be await'ed. The lack of await on these calls is the likely cause of your issue.

Always something simple.... My bad. Thanks guys! Confirmed it is working now