#AsyncTest help

1 messages · Page 1 of 1 (latest)

graceful pawn
#

Hello! Trying to do some testing on async jobs but I'm not sure what I can test. The asyncTest jobs array is empty. I've slept the thread a second and a half as well to get the scheduled task behavior and ensure the job is on the queue, but the docs don't have much help.

    @Test func testQueues() async throws {
        let app: Application = try await Application.make(.testing)
        app.queues.use(.asyncTest)
        app.queues.schedule(QuestionReliability()).everySecond() // QuestionReliability is an AsyncScheduledJob
        try app.queues.startScheduledJobs()
        /* Tests here ?*/
        try await app.asyncShutdown()
    }

Thanks for any help!

fierce badge
#

Are you adding any AsyncJobs anywhere?

#

Scheduled jobs are slightly different

graceful pawn
potent steppe
#

ScheduledJobs are stored in QueuesConfiguration (app.queues.configuration.scheduledJobs) however this property [AnyScheduledJob] is not publicly exposed. I guess technically you could get access to this by using a @testable import Queues in the test file itself? But I'd be pretty cautious of doing this and certainly only with the build configuration for your test target set to Debug mode.

high schoonerBOT
graceful pawn
potent steppe
#

Since its not a public property its not relevant for public documentation. And there are good reasons not for exposing this particular property as doing so would probably encourage mutation at runtime rather than correct use of the API. But I wouldn't be afraid to go poking around the source files for packages every now and then, either on Github or more easily just within Xcode itself 🙂
https://github.com/vapor/queues/blob/main/Sources/Queues/QueuesConfiguration.swift

GitHub

A queue system for Vapor. Contribute to vapor/queues development by creating an account on GitHub.

potent steppe
#

And actually even this doesnt technically test that the job has been scheduled but rather that it has been configured correctly with a ScheduleBuilder. There could be some use in testing the nextDate() logic on that ScheduleBuilder but that's not the same thing. The actual scheduledTasks (AnyScheduledJob.Task) sit inside the storage box of a QueuesCommand which is added when startScheduledJobs() is called. That storage box is marked as private so even if you wanted to use a @testable import you'll not be able to access anything

potent steppe
#

tldr; just wait and confirm that the job runs 😅 also, if you’re not aware already, there’s significantly more documentation to be found on https://api.vapor.codes (all of the DocC collections are hosted there) instead of just docs.vapor.codes

high schoonerBOT
high schoonerBOT
graceful pawn
potent steppe
# graceful pawn I'm trying to see if I can use Pointfree's clocks https://github.com/pointfreeco...

I doubt that’ll work - job scheduling uses NIO rather than Swift concurrency (i.e. no try await Task.sleep()) for its delay mechanism. Each iteration of the job calls AnyScheduledJob.schedule(context) which internally calls eventLoop.scheduleTask(initialDelay: Duration) where Duration is built from the ScheduleBuilder’s nextDate() method. It then waits for the job to complete and recursively calls for the next scheduling

#

But also - beyond checking that:

a) the job itself runs correctly (which you could do yourself just by calling ScheduledJob.run(context: )
and b) that your ScheduleBuilder is producing the schedule you expected

the responsibility for testing actual scheduling and execution is held by Vapor/Queues rather than your own tests. And you can see those tests here from lines 198-250:
https://github.com/vapor/queues/blob/main/Tests/QueuesTests/QueueTests.swift

GitHub

A queue system for Vapor. Contribute to vapor/queues development by creating an account on GitHub.

high schoonerBOT