|
| 1 | +package ton.sdk.client.binding |
| 2 | + |
| 3 | +import org.scalatest.flatspec.AsyncFlatSpec |
| 4 | +import org.scalatest.matchers.should.Matchers._ |
| 5 | +import ton.sdk.client.binding.Context._ |
| 6 | +import ton.sdk.client.jni.Binding |
| 7 | +import ton.sdk.client.modules.Client |
| 8 | + |
| 9 | +import scala.concurrent.duration.DurationInt |
| 10 | +import scala.concurrent.{Await, ExecutionContext, Future} |
| 11 | +import scala.util.Try |
| 12 | + |
| 13 | +class ContextSpec extends AsyncFlatSpec { |
| 14 | + |
| 15 | + "Context" should "throw an exception if library is not loaded" in { |
| 16 | + try { |
| 17 | + implicit val effect: Context.Effect[Try] = Context.tryEffect |
| 18 | + Context(1).request(Client.Request.BuildInfo) |
| 19 | + fail("Should throw an exception") |
| 20 | + } catch { |
| 21 | + case _: UnsatisfiedLinkError => succeed |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + "Sync Context" should "not allow usage if not open" in { |
| 26 | + implicit val effect: Context.Effect[Try] = Context.tryEffect |
| 27 | + Binding.loadNativeLibrary() |
| 28 | + val c = Context.create(ClientConfig.MAIN_NET).get |
| 29 | + val result1 = c.request(Client.Request.BuildInfo) |
| 30 | + c.close() |
| 31 | + val result2 = c.request(Client.Request.BuildInfo) |
| 32 | + result1.isSuccess shouldEqual true |
| 33 | + result2.isSuccess shouldEqual false |
| 34 | + } |
| 35 | + |
| 36 | + "Async Context" should "not allow usage if not open" in { |
| 37 | + implicit def executionContext: ExecutionContext = ExecutionContext.Implicits.global |
| 38 | + implicit val ef: Effect[Future] = futureEffect |
| 39 | + |
| 40 | + val c = Context.create(ClientConfig.TEST_NET).get |
| 41 | + val result1 = c.request(Client.Request.BuildInfo) |
| 42 | + Await.ready(result1, 10.seconds) |
| 43 | + c.close() |
| 44 | + val result2 = c.request(Client.Request.BuildInfo) |
| 45 | + Await.ready(result2, 10.seconds) |
| 46 | + result1.value.get.isSuccess shouldEqual true |
| 47 | + result2.value.get.isSuccess shouldEqual false |
| 48 | + } |
| 49 | +} |
0 commit comments