Skip to content

Commit eaa239a

Browse files
keremncjenkins
authored and
jenkins
committed
[lint] finagle: avoid implicit widening from integer division to floating point
In scala 2.12.20, it is a lint violation to perform an integer division that is widened to floating point like: ``` def compute(i: Int): Float = i / 2 ``` Differential Revision: https://phabricator.twitter.biz/D1206654
1 parent 7e255d3 commit eaa239a

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

finagle-exp/src/main/scala/com/twitter/finagle/exp/fiber_scheduler/util/Optimizer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private[fiber_scheduler] final case class Optimizer(
9191
log.error(ex, "optimizer failure")
9292
stats.failure.incr()
9393
} finally {
94-
stats.latency.add((System.nanoTime() - start) / 1000000)
94+
stats.latency.add((System.nanoTime() - start) / 1000000f)
9595
}
9696
}
9797

finagle-mux/src/main/scala/com/twitter/finagle/mux/lease/exp/ByteCounter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ private[lease] class WindowedByteCounter private[lease] (val info: JvmInfo, ctx:
7474
@volatile private[lease] var passCount: Int = 0
7575

7676
/** @return allocation rate in bytes per millisecond. */
77-
def rate(): Double = sum().inBytes / W.inMilliseconds
78-
private[this] def lastRate(): Double = allocs(idx).inBytes / P.inMilliseconds
77+
def rate(): Double = sum().inBytes / W.inMilliseconds.toDouble
78+
private[this] def lastRate(): Double = allocs(idx).inBytes / P.inMilliseconds.toDouble
7979

8080
override def toString =
8181
"WindowedByteCounter(windowed=" +

finagle-mux/src/test/scala/com/twitter/finagle/mux/lease/exp/WindowedByteCounterTest.scala

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package com.twitter.finagle.mux.lease.exp
33
import com.twitter.conversions.DurationOps._
44
import com.twitter.util._
55
import com.twitter.conversions.StorageUnitOps._
6-
import org.scalatest.concurrent.{Eventually, IntegrationPatience}
6+
import org.scalatest.concurrent.Eventually
7+
import org.scalatest.concurrent.IntegrationPatience
78
import scala.util.Random
9+
import org.scalactic.Tolerance._
810
import org.scalatest.funsuite.AnyFunSuite
911

1012
class WindowedByteCounterTest extends AnyFunSuite with Eventually with IntegrationPatience {
1113

14+
private[this] val epsilon = 1e-3
15+
1216
trait ByteCounterHelper {
1317
val fakePool = new FakeMemoryPool(new FakeMemoryUsage(StorageUnit.zero, StorageUnit.zero))
1418
val fakeBean = new FakeGarbageCollectorMXBean(0, 0)
@@ -93,9 +97,9 @@ class WindowedByteCounterTest extends AnyFunSuite with Eventually with Integrati
9397
nextPeriod()
9498
}
9599

96-
assert(
97-
counter.rate() == (WindowedByteCounter.N.kilobytes).inBytes / WindowedByteCounter.W.inMilliseconds
98-
)
100+
val expectedRate =
101+
WindowedByteCounter.N.kilobytes.inBytes / WindowedByteCounter.W.inMilliseconds.toDouble
102+
assert(counter.rate() === expectedRate +- epsilon)
99103
}
100104
}
101105

@@ -111,9 +115,9 @@ class WindowedByteCounterTest extends AnyFunSuite with Eventually with Integrati
111115
nextPeriod()
112116
}
113117

114-
assert(
115-
counter.rate() == (WindowedByteCounter.N.kilobytes).inBytes / WindowedByteCounter.W.inMilliseconds
116-
)
118+
val expectedRate1 =
119+
WindowedByteCounter.N.kilobytes.inBytes / WindowedByteCounter.W.inMilliseconds.toDouble
120+
assert(counter.rate() === expectedRate1 +- epsilon)
117121

118122
for (i <- 1 to WindowedByteCounter.N) {
119123
fakePool.setSnapshot(
@@ -122,10 +126,9 @@ class WindowedByteCounterTest extends AnyFunSuite with Eventually with Integrati
122126
nextPeriod()
123127
}
124128

125-
assert(
126-
counter
127-
.rate() == (2 * (WindowedByteCounter.N.kilobytes).inBytes / WindowedByteCounter.W.inMilliseconds)
128-
)
129+
val expectedRate2 =
130+
2 * WindowedByteCounter.N.kilobytes.inBytes / WindowedByteCounter.W.inMilliseconds.toDouble
131+
assert(counter.rate() === expectedRate2 +- epsilon)
129132
}
130133
}
131134

@@ -145,7 +148,8 @@ class WindowedByteCounterTest extends AnyFunSuite with Eventually with Integrati
145148
nextPeriod()
146149
}
147150

148-
assert(counter.rate() == x.inBytes / WindowedByteCounter.W.inMilliseconds)
151+
val expectedRate = x.inBytes / WindowedByteCounter.W.inMilliseconds.toDouble
152+
assert(counter.rate() === expectedRate +- epsilon)
149153
}
150154
}
151155

@@ -176,9 +180,9 @@ class WindowedByteCounterTest extends AnyFunSuite with Eventually with Integrati
176180
nextPeriod()
177181
}
178182

179-
assert(
180-
counter.rate() == WindowedByteCounter.N.kilobytes.inBytes / WindowedByteCounter.W.inMilliseconds
181-
)
183+
val expectedRate =
184+
WindowedByteCounter.N.kilobytes.inBytes / WindowedByteCounter.W.inMilliseconds.toDouble
185+
assert(counter.rate() === expectedRate +- epsilon)
182186
}
183187
}
184188

finagle-netty4/src/main/scala/com/twitter/finagle/netty4/threading/EventLoopGroupTrackingRunnable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private[threading] class EventLoopGroupTrackingRunnable(
124124
cpuTimeCounter.incr(TimeUnit.NANOSECONDS.toMillis(cpuTime))
125125
if (prevWallTimeNs != 0 && wallTimeNs != 0) {
126126
cpuUtilStat.add(
127-
10000 * cpuTime / wallTimeNs
127+
10000f * cpuTime / wallTimeNs
128128
)
129129
}
130130
prevCPUTimeNs = currCPUTimeNs

0 commit comments

Comments
 (0)