|
| 1 | +/* |
| 2 | + * Scala.js (https://www.scala-js.org/) |
| 3 | + * |
| 4 | + * Copyright EPFL. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (https://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package org.scalajs.nscplugin.test |
| 14 | + |
| 15 | +import org.scalajs.nscplugin.test.util._ |
| 16 | +import org.scalajs.nscplugin.test.util.VersionDependentUtils.scalaSupportsNoWarn |
| 17 | + |
| 18 | +import org.junit.Assume._ |
| 19 | +import org.junit.Test |
| 20 | + |
| 21 | +class GlobalExecutionContextWarnTest extends DirectTest with TestHelpers { |
| 22 | + |
| 23 | + @Test |
| 24 | + def warnOnUsage: Unit = { |
| 25 | + """ |
| 26 | + import scala.concurrent.ExecutionContext.global |
| 27 | +
|
| 28 | + object Enclosing { |
| 29 | + global |
| 30 | + } |
| 31 | + """ hasWarns |
| 32 | + """ |
| 33 | + |newSource1.scala:5: warning: The global execution context in Scala.js is based on JS Promises (microtasks). |
| 34 | + |Using it may prevent macrotasks (I/O, timers, UI rendering) from running reliably. |
| 35 | + | |
| 36 | + |Unfortunately, there is no way with ECMAScript only to implement a performant |
| 37 | + |macrotask execution context (and hence Scala.js core does not contain one). |
| 38 | + | |
| 39 | + |We recommend you use: https://github.com/scala-js/scala-js-macrotask-executor |
| 40 | + |Please refer to the README.md of that project for more details regarding |
| 41 | + |microtask vs. macrotask execution contexts. |
| 42 | + | |
| 43 | + |If you do not care about macrotask fairness, you can silence this warning by: |
| 44 | + |- Adding @nowarn("cat=other") (Scala >= 2.13.x only) |
| 45 | + |- Setting the -P:scalajs:nowarnGlobalExecutionContext compiler option |
| 46 | + |- Using scala.scalajs.concurrent.JSExecutionContext.queue |
| 47 | + | (the implementation of ExecutionContext.global in Scala.js) directly. |
| 48 | + | |
| 49 | + |If you do not care about performance, you can use |
| 50 | + |scala.scalajs.concurrent.QueueExecutionContext.timeouts(). |
| 51 | + |It is based on setTimeout which makes it fair but slow (due to clamping). |
| 52 | + | |
| 53 | + | global |
| 54 | + | ^ |
| 55 | + """ |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + def warnOnImplicitUsage: Unit = { |
| 60 | + """ |
| 61 | + import scala.concurrent.ExecutionContext.Implicits.global |
| 62 | +
|
| 63 | + object Enclosing { |
| 64 | + scala.concurrent.Future { } |
| 65 | + } |
| 66 | + """ hasWarns |
| 67 | + """ |
| 68 | + |newSource1.scala:5: warning: The global execution context in Scala.js is based on JS Promises (microtasks). |
| 69 | + |Using it may prevent macrotasks (I/O, timers, UI rendering) from running reliably. |
| 70 | + | |
| 71 | + |Unfortunately, there is no way with ECMAScript only to implement a performant |
| 72 | + |macrotask execution context (and hence Scala.js core does not contain one). |
| 73 | + | |
| 74 | + |We recommend you use: https://github.com/scala-js/scala-js-macrotask-executor |
| 75 | + |Please refer to the README.md of that project for more details regarding |
| 76 | + |microtask vs. macrotask execution contexts. |
| 77 | + | |
| 78 | + |If you do not care about macrotask fairness, you can silence this warning by: |
| 79 | + |- Adding @nowarn("cat=other") (Scala >= 2.13.x only) |
| 80 | + |- Setting the -P:scalajs:nowarnGlobalExecutionContext compiler option |
| 81 | + |- Using scala.scalajs.concurrent.JSExecutionContext.queue |
| 82 | + | (the implementation of ExecutionContext.global in Scala.js) directly. |
| 83 | + | |
| 84 | + |If you do not care about performance, you can use |
| 85 | + |scala.scalajs.concurrent.QueueExecutionContext.timeouts(). |
| 86 | + |It is based on setTimeout which makes it fair but slow (due to clamping). |
| 87 | + | |
| 88 | + | scala.concurrent.Future { } |
| 89 | + | ^ |
| 90 | + """ |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + def noWarnIfSelectivelyDisabled: Unit = { |
| 95 | + assumeTrue(scalaSupportsNoWarn) |
| 96 | + |
| 97 | + """ |
| 98 | + import scala.annotation.nowarn |
| 99 | + import scala.concurrent.ExecutionContext.global |
| 100 | +
|
| 101 | + object Enclosing { |
| 102 | + global: @nowarn("cat=other") |
| 103 | + } |
| 104 | + """.hasNoWarns() |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + def noWarnQueue: Unit = { |
| 109 | + /* Test that JSExecutionContext.queue does not warn for good measure. |
| 110 | + * We explicitly say it doesn't so we want to notice if it does. |
| 111 | + */ |
| 112 | + |
| 113 | + """ |
| 114 | + import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue |
| 115 | +
|
| 116 | + object Enclosing { |
| 117 | + scala.concurrent.Future { } |
| 118 | + } |
| 119 | + """.hasNoWarns() |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments