diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3042ca43e5..80d3ccab8a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,12 @@ Note that ``PHAB_ID=#`` and ``RB_ID=#`` correspond to associated messages in com Unreleased ---------- +New Features +~~~~~~~~~~~~ + +* finagle-core: Added variant of `c.t.f.Address.ServiceFactory.apply` that does not require + specifying `c.t.f.Addr.Metadata` and defaults to `c.t.f.Addr.Metadata.empty`. ``PHAB_ID=D605438`` + 21.1.0 ------ diff --git a/finagle-core/src/main/scala/com/twitter/finagle/Address.scala b/finagle-core/src/main/scala/com/twitter/finagle/Address.scala index d5e8f767c2..d6dbc0dd79 100644 --- a/finagle-core/src/main/scala/com/twitter/finagle/Address.scala +++ b/finagle-core/src/main/scala/com/twitter/finagle/Address.scala @@ -86,6 +86,12 @@ object Address { metadata: Addr.Metadata) extends Address + object ServiceFactory { + def apply[Req, Rep]( + factory: com.twitter.finagle.ServiceFactory[Req, Rep] + ): ServiceFactory[Req, Rep] = ServiceFactory(factory, Addr.Metadata.empty) + } + /** Create a new [[Address]] with given [[java.net.InetSocketAddress]]. */ def apply(addr: InetSocketAddress): Address = Address.Inet(addr, Addr.Metadata.empty) diff --git a/finagle-core/src/test/scala/com/twitter/finagle/AddressTest.scala b/finagle-core/src/test/scala/com/twitter/finagle/AddressTest.scala index 45e61c280b..9a32ec9dc4 100644 --- a/finagle-core/src/test/scala/com/twitter/finagle/AddressTest.scala +++ b/finagle-core/src/test/scala/com/twitter/finagle/AddressTest.scala @@ -1,9 +1,23 @@ package com.twitter.finagle +import com.twitter.util.Future import java.net.{InetAddress, InetSocketAddress} import org.scalatest.FunSuite class AddressTest extends FunSuite { + + def assertFactory[Req, Rep](address: Address, factory: ServiceFactory[Req, Rep]): Unit = + address match { + case Address.ServiceFactory(sf, _) => assert(sf == factory) + case _ => fail(s"$address is not an Address.ServiceFactory") + } + + def assertMetadata(address: Address, metadata: Addr.Metadata): Unit = + address match { + case Address.ServiceFactory(_, md) => assert(md == metadata) + case _ => fail(s"$address is not an Address.ServiceFactory") + } + test("constructor with no host points to localhost") { assert(Address(8080) == Address(new InetSocketAddress(InetAddress.getLoopbackAddress, 8080))) } @@ -20,6 +34,26 @@ class AddressTest extends FunSuite { val cmp12 = ordering.compare(sorted(0), sorted(1)) val cmp23 = ordering.compare(sorted(1), sorted(2)) assert(cmp13 == 0 && cmp12 == 0 && cmp23 == 0 || cmp13 < 0 && (cmp12 < 0 || cmp23 < 0)) + } + + test("can be created via a ServiceFactory") { + val service: Service[String, String] = Service.mk(_ => Future.value("Whatever")) + val sf: ServiceFactory[String, String] = ServiceFactory.const(service) + + // Without Metadata + val address1: Address = Address.ServiceFactory(sf) + assertFactory(address1, sf) + assertMetadata(address1, Addr.Metadata.empty) + + // With Metadata + val metadata = Addr.Metadata("a" -> "b") + val address2: Address = Address.ServiceFactory(sf, metadata) + assertFactory(address2, sf) + assertMetadata(address2, metadata) + // Without Metadata via Address.apply + val address3: Address = Address(sf) + assertFactory(address3, sf) + assertMetadata(address3, Addr.Metadata.empty) } }