mardi 23 mars 2021

How to make sure a given future completes first in tests?

I am writing a fake implementation of functions foo1, foo2, and foo3 for tests.

def foo1(x: Int): Future[Int] = ??? 
def foo2(x: Int): Future[Int] = ??? 
def foo3(x: Int): Future[Int] = ??? 

Test #1 invokes all these functions and checks if the result of foo1 completes first

val fut1 = foo1(0)
val fut2 = foo2(0)
val fut3 = foo3(0)

// make sure `fut1` completes first
// Future.firstCompletedOf(Array(fut1, fut2, fut3)) should be fut1

Test #2 invokes all these functions and makes sure that the result of foo2 completes first.
Test #3 invokes all these functions and makes sure that the result of foo3 completes first.

My question is how to implement the functions foo1, foo2, and foo3 and the tests.

I am thinking about using binary semaphores to implement these function and tests but it looks complicated.

Aucun commentaire:

Enregistrer un commentaire