-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util-app: Add App.loadServiceBindings API
Summary: Problem Some users want compile time assurance that a specific implementation will be used for `LoadService.apply`. Solution Provide a new `App.loadServiceBindings` API that allows a specific implementation to be specified. Result Users can have more assurance of which implementation is used. A lint rule is added to protect against duplicate registrations. JIRA Issues: csl-6125 Differential Revision: https://phabricator.twitter.biz/D146554
- Loading branch information
1 parent
476e635
commit c090167
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
server/src/main/scala/com/twitter/server/lint/DuplicateLoadServiceBindings.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.twitter.server.lint | ||
|
||
import com.twitter.app.LoadService | ||
import com.twitter.util.lint.{Category, Issue, Rule} | ||
|
||
/** | ||
* Lint rule for duplicate calls to `LoadService.bind`. | ||
*/ | ||
object DuplicateLoadServiceBindings { | ||
|
||
def apply(): Rule = Rule( | ||
Category.Configuration, | ||
"Duplicate calls to `LoadService.bind`", | ||
""" | ||
|`LoadService.bind` allows users to specify a specific | ||
|implementation. If this is getting called multiple | ||
|times for the same interface, it indicates a setup/configuration | ||
|issue that may cause surprises. | ||
""".stripMargin | ||
) { | ||
issues(LoadService.duplicateBindings) | ||
} | ||
|
||
/** exposed for testing */ | ||
private[lint] def issues(dupes: Set[Class[_]]): Seq[Issue] = | ||
if (dupes.isEmpty) { | ||
Nil | ||
} else { | ||
dupes.map { dupe => | ||
Issue(s"Duplicate for interface: ${dupe.getName}") | ||
}.toSeq | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
server/src/test/scala/com/twitter/server/lint/DuplicateLoadServiceBindingsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.twitter.server.lint | ||
|
||
import org.scalatest.FunSuite | ||
|
||
class DuplicateLoadServiceBindingsTest extends FunSuite { | ||
|
||
private trait TestTrait | ||
|
||
test("no duplicates") { | ||
assert(DuplicateLoadServiceBindings.issues(Set.empty).isEmpty) | ||
} | ||
|
||
test("includes the names of the duplicates") { | ||
val cls = classOf[TestTrait] | ||
assert(cls.getName == "com.twitter.server.lint.DuplicateLoadServiceBindingsTest$TestTrait") | ||
val issues = DuplicateLoadServiceBindings.issues(Set(cls)) | ||
assert(issues.head.details.contains(cls.getName)) | ||
} | ||
|
||
} |