Skip to content

Commit

Permalink
WIP Make non-scala-reflect code green
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbr committed May 29, 2021
1 parent f5147dd commit b3436cb
Show file tree
Hide file tree
Showing 30 changed files with 285 additions and 254 deletions.
17 changes: 11 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ val baseSettings = Seq(
"-Yrangepos"
) ++ Seq(
"-source:3.0-migration",
"-rewrite",
//"-rewrite",
"-explain",
"-explain-types",
).filter(_ => isScala3(scalaBinaryVersion.value)),
Expand Down Expand Up @@ -237,7 +237,7 @@ lazy val util = Project(
utilJvm,
utilLint,
utilLogging,
utilMock,
//utilMock, // TODO Scala3
utilReflect,
utilRegistry,
utilRouting,
Expand Down Expand Up @@ -488,10 +488,10 @@ lazy val utilSlf4jApi = Project(
name := "util-slf4j-api",
libraryDependencies ++= Seq(
slf4jApi,
("org.mockito" %% "mockito-scala" % mockitoScalaVersion % "test").cross(CrossVersion.for3Use2_13),
"org.mockito" % "mockito-core" % mockitoVersion % "test",
"org.slf4j" % "slf4j-simple" % slf4jVersion % "test"
)
).dependsOn(utilCore % "test", utilMock % "test")
).dependsOn(utilCore % "test")

lazy val utilSlf4jJulBridge = Project(
id = "util-slf4j-jul-bridge",
Expand Down Expand Up @@ -542,9 +542,11 @@ lazy val utilStats = Project(
scalacheckLib,
"com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion exclude ("com.google.guava", "guava"),
"org.scalatestplus" %% "mockito-3-3" % "3.1.2.0" % "test",
"org.scalatestplus" %% "scalacheck-1-14" % "3.1.2.0" % "test"
"org.scalatestplus" %% "scalacheck-1-14" % "3.1.2.0" % "test",
).map(_.cross(CrossVersion.for3Use2_13)) ++ {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, major)) =>
Seq("org.scala-lang.modules" %% "scala-parallel-collections" % "1.0.3" % "test")
case Some((2, major)) if major >= 13 =>
Seq("org.scala-lang.modules" %% "scala-parallel-collections" % "0.2.0" % "test")
case _ =>
Expand Down Expand Up @@ -593,10 +595,13 @@ lazy val utilTunable = Project(
sharedSettings
).settings(
name := "util-tunable",
resolvers += // TODO Scala3 remove this once the library is published properly
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
libraryDependencies ++= Seq(
"com.fasterxml.jackson.core" % "jackson-core" % jacksonVersion,
"com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion,
("com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion exclude ("com.google.guava", "guava")).cross(CrossVersion.for3Use2_13)
//("com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion exclude ("com.google.guava", "guava")).cross(CrossVersion.for3Use2_13)
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.13.0-SNAPSHOT", // TODO Scala3 global jackson upgrade first?
)
).dependsOn(utilApp, utilCore)

Expand Down
6 changes: 4 additions & 2 deletions util-app/src/main/scala/com/twitter/app/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.collection.immutable.TreeSet
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.jdk.CollectionConverters._
import scala.reflect.ClassTag
import scala.util.control.NonFatal

/**
Expand Down Expand Up @@ -277,8 +278,9 @@ class Flags(argv0: String, includeGlobal: Boolean, failFastUntilParsed: Boolean)
* @param name The name of the flag.
* @param help The help string of the flag.
*/
def apply[T](name: String, help: String)(implicit _f: Flaggable[T], m: Manifest[T]): Flag[T] = {
val f = new Flag[T](name, help, m.toString, failFastUntilParsed)
def apply[T](name: String, help: String)(implicit _f: Flaggable[T], m: ClassTag[T]): Flag[T] = {
// TODO Scala3 Is ClassTag ok? Should we capitalize the type name?
val f = new Flag[T](name, help, m.runtimeClass.getTypeName.capitalize, failFastUntilParsed)
add(f)
f
}
Expand Down
6 changes: 4 additions & 2 deletions util-app/src/main/scala/com/twitter/app/GlobalFlag.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.twitter.app

import java.lang.reflect.Modifier
import scala.reflect.ClassTag
import scala.util.control.NonFatal

/**
Expand Down Expand Up @@ -85,8 +86,9 @@ abstract class GlobalFlag[T] private[app] (
*
* @param help documentation regarding usage of this [[Flag]].
*/
def this(help: String)(implicit _f: Flaggable[T], m: Manifest[T]) =
this(Right(m.toString), help)
def this(help: String)(implicit _f: Flaggable[T], m: ClassTag[T]) =
// TODO Scala3 Is ClassTag ok? Should we capitalize the type name?
this(Right(m.runtimeClass.getTypeName.capitalize), help)

/**
* The "name", or "id", of this [[Flag]].
Expand Down
2 changes: 1 addition & 1 deletion util-app/src/test/scala/com/twitter/app/AppTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class AppTest extends AnyFunSuite {
assert(n2 == 0)
assert(!f.isDefined)

p.setDone
p.setDone()
assert(n2 == 1)
assert(f.isDefined)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.twitter.conversions

import com.twitter.util.Duration

import java.util.concurrent.TimeUnit
import scala.language.implicitConversions

/**
* Implicits for writing readable [[com.twitter.util.Duration]]s.
Expand All @@ -19,7 +21,12 @@ import java.util.concurrent.TimeUnit
*/
object DurationOps {

implicit class RichDuration(val numNanos: Long) extends AnyVal {
/**
* Forwarder for Int, as Scala 3.0 seems to not like the implicit conversion to Long.
*/
implicit def richDurationFromIntNanos(numNanos: Int): RichDuration = new RichDuration(numNanos.toLong)

implicit class RichDuration(private val numNanos: Long) extends AnyVal {
def nanoseconds: Duration = Duration(numNanos, TimeUnit.NANOSECONDS)
def nanosecond: Duration = nanoseconds
def microseconds: Duration = Duration(numNanos, TimeUnit.MICROSECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.twitter.conversions

import com.twitter.util.StorageUnit

import scala.language.implicitConversions

/**
* Implicits for writing readable [[com.twitter.util.StorageUnit]]s.
*
Expand All @@ -16,7 +18,12 @@ import com.twitter.util.StorageUnit
*/
object StorageUnitOps {

implicit class RichStorageUnit(val numBytes: Long) extends AnyVal {
/**
* Forwarder for Int, as Scala 3.0 seems to not like the implicit conversion to Long.
*/
implicit def numBytesFromInt(numBytes: Int): RichStorageUnit = new RichStorageUnit(numBytes.toLong)

implicit class RichStorageUnit(private val numBytes: Long) extends AnyVal {
def byte: StorageUnit = bytes
def bytes: StorageUnit = StorageUnit.fromBytes(numBytes)
def kilobyte: StorageUnit = kilobytes
Expand Down
2 changes: 1 addition & 1 deletion util-core/src/main/scala/com/twitter/util/CloseOnce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import scala.util.control.NonFatal
* @see [[ClosableOnce.of]] for creating a proxy to a [[Closable]]
* that has already been instantiated.
*/
trait CloseOnce { self: Closable =>
trait CloseOnce extends Closable {
// Our intrinsic lock for mutating the `closed` field
private[this] val closePromise: Promise[Unit] = Promise[Unit]()
@volatile private[this] var closed: Boolean = false
Expand Down
2 changes: 1 addition & 1 deletion util-core/src/main/scala/com/twitter/util/Duration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Duration extends TimeLikeOps[Duration] {
if (nanoseconds == 0L) Zero
else new Duration(nanoseconds)

// FIXME figure out why the overrides cause issues in Scala 3.0
// TODO Scala3 figure out why the overrides cause issues in Scala 3.0
// This is needed for Java compatibility.
def fromFractionalSecondsJ(seconds: Double): Duration =
super.fromFractionalSeconds(seconds)
Expand Down
2 changes: 1 addition & 1 deletion util-core/src/main/scala/com/twitter/util/Time.scala
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private[util] object TimeBox {
object Time extends TimeLikeOps[Time] {
def fromNanoseconds(nanoseconds: Long): Time = new Time(nanoseconds)

// FIXME figure out why the overrides cause issues in Scala 3.0
// TODO Scala3 figure out why the overrides cause issues in Scala 3.0
// This is needed for Java compatibility.
def fromFractionalSecondsJ(seconds: Double): Time = super.fromFractionalSeconds(seconds)
def fromSecondsJ(seconds: Int): Time = super.fromSeconds(seconds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public Future<BoxedUnit> closeOnce(Time deadline) {
closeCount++;
return Future.Done();
}

@Override
public Future<BoxedUnit> close(Time deadline) {
return Future.Done();
}
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion util-jvm/src/main/scala/com/twitter/jvm/Jvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ trait Jvm {
buffer = (gc :: buffer).takeWhile(_.timestamp > floor)
}

since: Time => buffer.takeWhile(_.timestamp > since)
(since: Time) => buffer.takeWhile(_.timestamp > since)
}

def forceGc(): Unit
Expand Down
21 changes: 13 additions & 8 deletions util-jvm/src/test/scala/com/twitter/jvm/JvmTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package com.twitter.jvm

import com.twitter.conversions.DurationOps._
import com.twitter.util.Time

import java.util.logging.{Level, Logger}
import org.mockito.ArgumentMatchers.contains
import org.mockito.Mockito.{times, verify, when}
import org.scalatestplus.mockito.MockitoSugar

import scala.collection.mutable
import org.scalatest.wordspec.AnyWordSpec

import java.util.concurrent.ScheduledExecutorService

class JvmTest extends AnyWordSpec with MockitoSugar {
"Jvm" should {
class JvmHelper(suppliedLogger: Option[Logger] = None) extends Jvm {
Expand All @@ -28,7 +32,8 @@ class JvmTest extends AnyWordSpec with MockitoSugar {
currentSnap = snap
}

override val executor = new MockScheduledExecutorService
val mockExecutor = new MockScheduledExecutorService
override val executor: ScheduledExecutorService = mockExecutor

def snap = currentSnap

Expand Down Expand Up @@ -62,10 +67,10 @@ class JvmTest extends AnyWordSpec with MockitoSugar {
"Capture interleaving GCs with different names" in {
val jvm = new JvmHelper()
val b = mutable.Buffer[Gc]()
assert(jvm.executor.schedules == List())
assert(jvm.mockExecutor.schedules == List())
jvm foreachGc { b += _ }
assert(jvm.executor.schedules.size == 1)
val r = jvm.executor.schedules.head._1
assert(jvm.mockExecutor.schedules.size == 1)
val r = jvm.mockExecutor.schedules.head._1
r.run()
assert(b == List())
val gc = Gc(0, "pcopy", Time.now, 1.millisecond)
Expand Down Expand Up @@ -99,8 +104,8 @@ class JvmTest extends AnyWordSpec with MockitoSugar {
val jvm = new JvmHelper(Some(logger))

jvm.foreachGc(_ => () /*ignore*/ )
assert(jvm.executor.schedules.size == 1)
val r = jvm.executor.schedules.head._1
assert(jvm.mockExecutor.schedules.size == 1)
val r = jvm.mockExecutor.schedules.head._1
val gc = Gc(0, "pcopy", Time.now, 1.millisecond)
r.run()
jvm.pushGc(gc)
Expand All @@ -126,8 +131,8 @@ class JvmTest extends AnyWordSpec with MockitoSugar {
"queries gcs in range, in reverse chronological order" in Time.withCurrentTimeFrozen { tc =>
val jvm = new JvmHelper()
val query = jvm.monitorGcs(10.seconds)
assert(jvm.executor.schedules.size == 1)
val r = jvm.executor.schedules.head._1
assert(jvm.mockExecutor.schedules.size == 1)
val r = jvm.mockExecutor.schedules.head._1
val gc0 = Gc(0, "pcopy", Time.now, 1.millisecond)
val gc1 = Gc(1, "CMS", Time.now, 1.millisecond)
jvm.pushGc(gc1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class FileHandlerTest extends AnyWordSpec with TempFolder {
}

val files = listLogFiles(folderName + "/LogFileDir")
files.foreach { f: File =>
files.foreach { (f: File) =>
val len = f.length().bytes
if (len > fileSizeInMegaBytes.megabytes) {
fail("Failed to roll over the log file")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.scalatest.funsuite.AnyFunSuite
class LogRecordTest extends AnyFunSuite {
test("LogRecord should getMethod properly") {
Logger.withLoggers(Nil) {
new LogRecordTestHelper({ r: JRecord => r.getSourceMethodName() }) {
new LogRecordTestHelper({ (r: JRecord) => r.getSourceMethodName() }) {
def makingLogRecord() = {
logger.log(Level.INFO, "OK")
assert(handler.get == "makingLogRecord")
Expand Down Expand Up @@ -34,7 +34,7 @@ abstract class LogRecordTestHelper(formats: JRecord => String) {
logger.addHandler(handler)
}

class Foo extends LogRecordTestHelper({ r: JRecord => r.getSourceClassName() }) {
class Foo extends LogRecordTestHelper({ (r: JRecord) => r.getSourceClassName() }) {
def makingLogRecord(): Unit = {
logger.log(Level.INFO, "OK")
}
Expand Down
18 changes: 9 additions & 9 deletions util-logging/src/test/scala/com/twitter/logging/LoggerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class LoggerTest extends AnyWordSpec with TempFolder with BeforeAndAfter {
"asdf" + executed + " hi there"
}

logger.debugLazy(function)
logger.debugLazy(function())
assert(!executed)
}

Expand All @@ -136,7 +136,7 @@ class LoggerTest extends AnyWordSpec with TempFolder with BeforeAndAfter {
executed = true
"asdf" + executed + " hi there"
}
logger.debugLazy(function)
logger.debugLazy(function())
assert(executed)
}

Expand Down Expand Up @@ -263,11 +263,11 @@ class LoggerTest extends AnyWordSpec with TempFolder with BeforeAndAfter {
val otherFactories = List(LoggerFactory(node = "", level = Some(Level.INFO)))
Logger.configure(initialFactories)

assert(Logger.get("").getLevel == Level.DEBUG)
assert(Logger.get("").getLevel() == Level.DEBUG)
Logger.withLoggers(otherFactories) {
assert(Logger.get("").getLevel() == Level.INFO)
}
assert(Logger.get("").getLevel == Level.DEBUG)
assert(Logger.get("").getLevel() == Level.DEBUG)
}

"configure logging" should {
Expand All @@ -294,7 +294,7 @@ class LoggerTest extends AnyWordSpec with TempFolder with BeforeAndAfter {
) :: Nil
).apply()

assert(log.getLevel == Level.DEBUG)
assert(log.getLevel() == Level.DEBUG)
assert(log.getHandlers().length == 1)
val handler = log.getHandlers()(0).asInstanceOf[FileHandler]
val fileName = folderName + separator + "test.log"
Expand Down Expand Up @@ -325,7 +325,7 @@ class LoggerTest extends AnyWordSpec with TempFolder with BeforeAndAfter {
) :: Nil
).apply()

assert(log.getHandlers.length == 1)
assert(log.getHandlers().length == 1)
val h = log.getHandlers()(0).asInstanceOf[SyslogHandler]
assert(h.dest.asInstanceOf[InetSocketAddress].getHostName == "localhost")
assert(h.dest.asInstanceOf[InetSocketAddress].getPort == 212)
Expand Down Expand Up @@ -365,9 +365,9 @@ class LoggerTest extends AnyWordSpec with TempFolder with BeforeAndAfter {
) :: Nil

Logger.configure(factories)
assert(Logger.get("").getLevel == Level.INFO)
assert(Logger.get("w3c").getLevel == Level.OFF)
assert(Logger.get("bad_jobs").getLevel == Level.INFO)
assert(Logger.get("").getLevel() == Level.INFO)
assert(Logger.get("w3c").getLevel() == Level.OFF)
assert(Logger.get("bad_jobs").getLevel() == Level.INFO)
try {
Logger.get("").getHandlers()(0).asInstanceOf[ThrottledHandler]
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SyslogHandlerTest extends AnyWordSpec {
syslog.publish(record1)
syslog.publish(record2)

SyslogFuture.sync
SyslogFuture.sync()
val p = new DatagramPacket(new Array[Byte](1024), 1024)
serverSocket.receive(p)
assert(
Expand Down Expand Up @@ -78,7 +78,7 @@ class SyslogHandlerTest extends AnyWordSpec {
).apply()
syslog.publish(record1)

SyslogFuture.sync
SyslogFuture.sync()
val p = new DatagramPacket(new Array[Byte](1024), 1024)
serverSocket.receive(p)
assert(
Expand All @@ -104,7 +104,7 @@ class SyslogHandlerTest extends AnyWordSpec {
).apply()
syslog.publish(record1)

SyslogFuture.sync
SyslogFuture.sync()
val p = new DatagramPacket(new Array[Byte](1024), 1024)
serverSocket.receive(p)
assert(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.twitter.util.routing

import com.twitter.util.routing.simple.{SimpleRoute, SimpleRouter}
impo()rt org.scalatest.funsuite.AnyFunSuite
import org.scalatest.funsuite.AnyFunSuite

class RouterBuilderTest extends AnyFunSuite {

Expand Down
Loading

0 comments on commit b3436cb

Please sign in to comment.