Task.await in test cases?

I have one test case function. Inside that, I have two helper modules that insert some data to test DB. I want to make sure that this test will run func1() first and then it can go to func2(). What is the best way to do it?

I’m using task.await but it’s not giving me what I want.

test "something" do
     a = func1()
     b = func2()
end

Can you share more details as to what func1 and func2 are doing? Do they return a Task? If so, how about calling Task.await(func1()) and Task.await(func2())?

They are not returning Task. They are just inserting some data into Test DB in runtime. But anyway even if it is one function that is doing anything and I want to have control over that in the same way. Is there a way to do it?

What kind of control do you have in mind? Are those functions doing asynchronous work? In your example:

test "something" do
   # func1 is executed synchronously
   a = func1()
   # the execution waits till it completes
   # func2 is executed synchronously
   b = func2()
   # both func1 and func2 have completed
end

Yes in my function sometimes func2() runs first then func1(). So I want to make sure that always it will run func1() first.

Technically speaking - if they are doing asynchrous work - they run at the same time. It’s that sometimes func2 completes before func1. You need to wait for func1 to finish it’s work. It has to signal somehow that it’s done it’s job. It needs to send a message to the calling process or return a Task that can be awaited.

1 Like

Why not use Ecto.Multi or just call both synchronous functions one after another? It doesn’t seem like you actually need asynchronous tasks in this case.