|
| 1 | +package metacall |
| 2 | + |
| 3 | +import cats.implicits._ |
| 4 | +import metacall.instances._ |
| 5 | +import org.scalatest.flatspec.AnyFlatSpec |
| 6 | + |
| 7 | +class CallerSpec extends AnyFlatSpec { |
| 8 | + // TODO: This won't work with NodeJS, it is not tolerant a reinitialization. |
| 9 | + // Probably we should split this into two tests, one for the caller (event loop based), |
| 10 | + // and another for MetaCall without event loop. So each test suite runs in a different process. |
| 11 | + "Caller" should "start successfully" in { |
| 12 | + Caller.start() |
| 13 | + } |
| 14 | + |
| 15 | + "Caller" should "load scripts into global scope successfully" in { |
| 16 | + Caller.loadFile(Runtime.Python, "./src/test/scala/scripts/main.py", None) |
| 17 | + } |
| 18 | + |
| 19 | + "Caller" should "load scripts into namespaces" in { |
| 20 | + Caller.loadFile(Runtime.Python, "./src/test/scala/scripts/s1.py", Some("s1")) |
| 21 | + Caller.loadFile(Runtime.Python, "./src/test/scala/scripts/s2.py", Some("s2")) |
| 22 | + |
| 23 | + assert( |
| 24 | + Caller.blocking.call("fn_in_s1", (), Some("s1")) == StringValue("Hello from s1") |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + "Caller" should "call functions and clean up arguments and returned pointers" in { |
| 29 | + val ret = Caller.blocking.callV( |
| 30 | + "hello_scala_from_python", |
| 31 | + List(StringValue("Hello "), StringValue("Scala!")) |
| 32 | + ) |
| 33 | + |
| 34 | + assert(ret == StringValue("Hello Scala!")) |
| 35 | + } |
| 36 | + |
| 37 | + "FunctionValues" should "be constructed and passed to foreign functions" in { |
| 38 | + val fnVal = FunctionValue { |
| 39 | + case LongValue(l) :: Nil => LongValue(l + 1L) |
| 40 | + case _ => NullValue |
| 41 | + } |
| 42 | + |
| 43 | + val ret = Caller.blocking.callV("apply_fn_to_one", fnVal :: Nil) |
| 44 | + |
| 45 | + assert(ret == LongValue(2L)) |
| 46 | + } |
| 47 | + |
| 48 | + "Generic API" should "operate on primitive Scala values" in { |
| 49 | + // with tuples |
| 50 | + val ret = Caller.blocking.call("big_fn", (1, "hello", 2.2)) |
| 51 | + assert(ret == DoubleValue(8.2)) |
| 52 | + |
| 53 | + // with single-element products (i.e. the List) |
| 54 | + val ret2 = Caller.blocking.call("sumList", List(1, 2, 3)) |
| 55 | + assert(ret2 == LongValue(6)) |
| 56 | + |
| 57 | + // with HLists |
| 58 | + import shapeless._ |
| 59 | + |
| 60 | + val ret3 = Caller.blocking.call("big_fn", 1 :: "hello" :: 2.2 :: HNil) |
| 61 | + assert(ret3 == DoubleValue(8.2)) |
| 62 | + } |
| 63 | + |
| 64 | + "Using `Caller` from multiple threads" should "work" in { |
| 65 | + import scala.concurrent._, duration._ |
| 66 | + import ExecutionContext.Implicits.global |
| 67 | + |
| 68 | + val rangeValues: List[ArrayValue] = |
| 69 | + List.range(1, 50).map(n => ArrayValue(Vector.range(1, n).map(IntValue))) |
| 70 | + |
| 71 | + val resSum = rangeValues |
| 72 | + .traverse { range => |
| 73 | + Future(Caller.blocking.callV("sumList", range :: Nil)) map { |
| 74 | + case n: NumericValue[_] => n.long.value |
| 75 | + case other => fail("Returned value should be a number, but got " + other) |
| 76 | + } |
| 77 | + } |
| 78 | + .map(_.sum) |
| 79 | + |
| 80 | + val result = Await.result(resSum, 10.seconds) |
| 81 | + |
| 82 | + assert(result == 19600) |
| 83 | + } |
| 84 | + |
| 85 | + "Calling functions many times in parallel" should "work" in { |
| 86 | + import scala.concurrent._, duration._ |
| 87 | + import ExecutionContext.Implicits.global |
| 88 | + |
| 89 | + val rangeValues: List[ArrayValue] = |
| 90 | + List.range(1, 50).map(n => ArrayValue(Vector.range(1, n).map(IntValue))) |
| 91 | + |
| 92 | + val resSum = rangeValues |
| 93 | + .traverse { range => |
| 94 | + Caller.callV("sumList", range :: Nil) map { |
| 95 | + case n: NumericValue[_] => n.long.value |
| 96 | + case other => fail("Returned value should be a number, but got " + other) |
| 97 | + } |
| 98 | + } |
| 99 | + .map(_.sum) |
| 100 | + |
| 101 | + val result = Await.result(resSum, 10.seconds) |
| 102 | + |
| 103 | + assert(result == 19600) |
| 104 | + } |
| 105 | + |
| 106 | + "Caller" should "be destroyed correctly" in { |
| 107 | + Caller.destroy() |
| 108 | + } |
| 109 | +} |
0 commit comments