Skip to content

Commit

Permalink
use pactBrokerClientTimeout setting for result publisher too (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbwheatley authored Oct 20, 2020
1 parent cbdbb74 commit 2ab9e38
Show file tree
Hide file tree
Showing 31 changed files with 422 additions and 464 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.itv.scalapact.plugin

import com.itv.scalapact.shared.{BrokerPublishData, ScalaPactSettings}
import com.itv.scalapact.shared.ScalaPactSettings
import com.itv.scalapact.shared.typeclasses.BrokerPublishData

import scala.concurrent.duration._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.itv.scalapact.plugin.shared
import com.itv.scalapact.shared.ColourOutput._
import com.itv.scalapact.shared._
import com.itv.scalapactcore.common.LocalPactFileLoader
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IResultPublisherBuilder, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.ProviderStateResult.SetupProviderState
import com.itv.scalapactcore.verifier.Verifier

Expand All @@ -29,7 +29,7 @@ object ScalaPactVerifyCommand {
)(implicit pactReader: IPactReader,
pactWriter: IPactWriter,
httpClientBuilder: IScalaPactHttpClientBuilder[F],
publisher: IResultPublisher): Unit = {
publisher: IResultPublisherBuilder): Unit = {
PactLogger.message("*************************************".white.bold)
PactLogger.message("** ScalaPact: Running Verifier **".white.bold)
PactLogger.message("*************************************".white.bold)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import com.itv.scalapactcore.common._
import scala.util.Left
import com.itv.scalapact.shared.PactLogger
import com.itv.scalapact.shared.ProviderStateResult.SetupProviderState
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IScalaPactHttpClient, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IResultPublisherBuilder, IScalaPactHttpClient, IScalaPactHttpClientBuilder}

import scala.concurrent.duration.DurationInt

class Verifier[F[_]](pactBrokerClient: PactBrokerClient[F])(implicit pactReader: IPactReader,
httpClientBuilder: IScalaPactHttpClientBuilder[F],
publisher: IResultPublisher) {
publisherBuilder: IResultPublisherBuilder) {

def verify(
loadPactFiles: String => ScalaPactSettings => List[Pact],
Expand Down Expand Up @@ -105,9 +107,11 @@ class Verifier[F[_]](pactBrokerClient: PactBrokerClient[F])(implicit pactReader:
else
PactLogger.message(scalaPactLogPrefix + s"$failureCount Pact verify tests failed.".red)

arguments.publishResultsEnabled.foreach(
arguments.publishResultsEnabled.foreach {
val publisher = publisherBuilder
.build(pactVerifySettings.pactBrokerClientTimeout.getOrElse(2.seconds), pactVerifySettings.sslContextName)
publisher.publishResults(pactVerifyResults, _, pactVerifySettings.pactBrokerAuthorization)
)
}

testCount > 0 && failureCount == 0
}
Expand Down Expand Up @@ -185,7 +189,7 @@ object Verifier {
def apply[F[_]](implicit pactReader: IPactReader,
pactWriter: IPactWriter,
httpClient: IScalaPactHttpClientBuilder[F],
publisher: IResultPublisher): Verifier[F] =
publisher: IResultPublisherBuilder): Verifier[F] =
new Verifier[F](new PactBrokerClient[F])
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.itv.scalapact.http4s16a.impl

import com.itv.scalapact.shared.typeclasses.{IPactStubber, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.{IResultPublisher, SslContextMap}
import com.itv.scalapact.shared.typeclasses.{IPactStubber, IResultPublisherBuilder, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.SslContextMap
import scalaz.concurrent.Task

import scala.concurrent.duration.Duration
Expand All @@ -16,5 +16,6 @@ trait HttpInstances {
implicit def httpClientBuilder(implicit sslContextMap: SslContextMap): IScalaPactHttpClientBuilder[Task] =
(clientTimeout: Duration, sslContextName: Option[String]) => ScalaPactHttpClient(clientTimeout, sslContextName)

implicit val resultPublisher: IResultPublisher = new ResultPublisher(Http4sClientHelper.doRequest)
implicit def resultPublisherBuilder(implicit sslContextMap: SslContextMap): IResultPublisherBuilder =
(clientTimeout: Duration, sslContextName: Option[String]) => ResultPublisher(clientTimeout, sslContextName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package com.itv.scalapact.http4s16a.impl

import com.itv.scalapact.shared.ColourOutput._
import com.itv.scalapact.shared._
import com.itv.scalapact.shared.typeclasses.{BrokerPublishData, IResultPublisher}
import org.http4s.client.Client
import scalaz.concurrent.Task

import scala.concurrent.duration._

class ResultPublisher(fetcher: (SimpleRequest, Client) => Task[SimpleResponse]) extends IResultPublisher {

val maxTotalConnections: Int = 2
private val clientTimeout = 10.seconds

class ResultPublisher(client: Client)(fetcher: (SimpleRequest, Client) => Task[SimpleResponse]) extends IResultPublisher {
override def publishResults(
pactVerifyResults: List[PactVerifyResult],
brokerPublishData: BrokerPublishData,
pactBrokerAuthorization: Option[PactBrokerAuthorization])(implicit sslContextMap: SslContextMap): Unit = {
pactBrokerAuthorization: Option[PactBrokerAuthorization]): Unit = {
val list = pactVerifyResults.map { result =>
result.pact._links.flatMap(_.get("pb:publish-verification-results")).map(_.href) match {
case Some(link) =>
Expand All @@ -29,20 +26,15 @@ class ResultPublisher(fetcher: (SimpleRequest, Client) => Task[SimpleResponse])
None
)

SslContextMap(request)(
sslContext =>
simpleRequestWithoutFakeHeader => {
val client = Http4sClientHelper.buildPooledBlazeHttpClient(maxTotalConnections, clientTimeout, sslContext)
fetcher(simpleRequestWithoutFakeHeader, client)
.map { response =>
if (response.is2xx) {
PactLogger.message(s"Verification results published for provider ${result.pact.provider} and consumer ${result.pact.consumer}")
} else {
PactLogger.error(s"Publish verification results failed with ${response.statusCode}".red)
}
}
fetcher(request, client)
.map { response =>
if (response.is2xx) {
PactLogger.message(s"Verification results published for provider ${result.pact.provider} and consumer ${result.pact.consumer}")
} else {
PactLogger.error(s"Publish verification results failed with ${response.statusCode}".red)
}
)
}

case None =>
Task.now(PactLogger.error("Unable to publish verification results as there is no pb:publish-verification-results link".red))
}
Expand All @@ -55,4 +47,12 @@ class ResultPublisher(fetcher: (SimpleRequest, Client) => Task[SimpleResponse])
val buildUrl = brokerPublishData.buildUrl.fold("")(u => s""", "buildUrl": "$u"""")
Option(s"""{ "success": $success, "providerApplicationVersion": "${brokerPublishData.providerVersion}"$buildUrl }""")
}
}

object ResultPublisher {
def apply(clientTimeout: Duration, sslContextName: Option[String])(implicit sslContextMap: SslContextMap): IResultPublisher = {
val sslContext = sslContextMap(sslContextName)
val client = Http4sClientHelper.buildPooledBlazeHttpClient(2, clientTimeout, sslContext)
new ResultPublisher(client)(Http4sClientHelper.doRequest)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.itv.scalapact.http4s16a.impl
import com.itv.scalapact.shared._
import com.itv.scalapact.shared.typeclasses.BrokerPublishData
import org.http4s.client.Client
import org.scalatest.{BeforeAndAfter, FunSpec, Matchers}
import scalaz.concurrent.Task
Expand Down Expand Up @@ -54,7 +55,7 @@ class ResultPublisherSpec extends FunSpec with Matchers with BeforeAndAfter {
Task.now(SimpleResponse(200))
}

private val resultPublisher = new ResultPublisher(fakeCaller)
private val resultPublisher = new ResultPublisher(null)(fakeCaller)

before {
requests = ArrayBuffer.empty[SimpleRequest]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
//package com.itv.scalapact.http4s16a.impl
//
//import com.itv.scalapact.shared._
//import org.http4s.client.Client
//import org.scalatest.{FunSpec, Matchers}
//import scalaz.concurrent.Task
//
//import scala.concurrent.duration._
//
//class ScalaPactHttpClientSpec extends FunSpec with Matchers {
//
// describe("Making an interaction request") {
//
// it("should be able to make and interaction request and get an interaction response") {
//
// val requestDetails = InteractionRequest(
// method = Some("GET"),
// headers = None,
// query = None,
// path = Some("/foo"),
// body = None,
// matchingRules = None
// )
//
// val responseDetails = InteractionResponse(
// status = Some(200),
// headers = None,
// body = None,
// matchingRules = None
// )
//
// val fakeCaller: (SimpleRequest, Client) => Task[SimpleResponse] = (_, _) => Task.now(SimpleResponse(200))
//
// val result = new ScalaPactHttpClient(fakeCaller)
// .doInteractionRequest("", requestDetails, 1.second, sslContextName = None)
// .unsafePerformSync
//
// result shouldEqual responseDetails
//
// }
//
// }
//
//}
package com.itv.scalapact.http4s16a.impl

import com.itv.scalapact.shared._
import org.http4s.client.Client
import org.scalatest.{FunSpec, Matchers}
import scalaz.concurrent.Task

class ScalaPactHttpClientSpec extends FunSpec with Matchers {

describe("Making an interaction request") {

it("should be able to make and interaction request and get an interaction response") {

val requestDetails = InteractionRequest(
method = Some("GET"),
headers = None,
query = None,
path = Some("/foo"),
body = None,
matchingRules = None
)

val responseDetails = InteractionResponse(
status = Some(200),
headers = None,
body = None,
matchingRules = None
)

val fakeCaller: (SimpleRequest, Client) => Task[SimpleResponse] = (_, _) => Task.now(SimpleResponse(200))

val result = new ScalaPactHttpClient(null)(fakeCaller)
.doInteractionRequest("", requestDetails)
.unsafePerformSync

result shouldEqual responseDetails

}

}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.itv.scalapact.http4s17.impl

import com.itv.scalapact.shared.typeclasses.{IPactStubber, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.{IResultPublisher, SslContextMap}
import com.itv.scalapact.shared.typeclasses.{IPactStubber, IResultPublisherBuilder, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.SslContextMap
import fs2.Task

import scala.concurrent.duration.Duration
Expand All @@ -16,5 +16,6 @@ trait HttpInstances {
implicit def httpClientBuilder(implicit sslContextMap: SslContextMap): IScalaPactHttpClientBuilder[Task] =
(clientTimeout: Duration, sslContextName: Option[String]) => ScalaPactHttpClient(clientTimeout, sslContextName)

implicit val resultPublisher: IResultPublisher = new ResultPublisher(Http4sClientHelper.doRequest)
implicit def resultPublisherBuilder(implicit sslContextMap: SslContextMap): IResultPublisherBuilder =
(clientTimeout: Duration, sslContextName: Option[String]) => ResultPublisher(clientTimeout, sslContextName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package com.itv.scalapact.http4s17.impl

import com.itv.scalapact.shared.ColourOutput._
import com.itv.scalapact.shared._
import com.itv.scalapact.shared.typeclasses.{BrokerPublishData, IResultPublisher}
import fs2.Task
import org.http4s.client.Client

import scala.concurrent.duration._

class ResultPublisher(fetcher: (SimpleRequest, Client) => Task[SimpleResponse]) extends IResultPublisher {
class ResultPublisher(client: Client)(fetcher: (SimpleRequest, Client) => Task[SimpleResponse]) extends IResultPublisher {

val maxTotalConnections: Int = 2
private val clientTimeout = 10.seconds

override def publishResults(pactVerifyResults: List[PactVerifyResult], brokerPublishData: BrokerPublishData, pactBrokerAuthorization: Option[PactBrokerAuthorization])(implicit sslContextMap: SslContextMap): Unit = {
override def publishResults(pactVerifyResults: List[PactVerifyResult], brokerPublishData: BrokerPublishData, pactBrokerAuthorization: Option[PactBrokerAuthorization]): Unit = {
Task.traverse(pactVerifyResults){ result =>
result.pact._links.flatMap(_.get("pb:publish-verification-results")).map(_.href) match {
case Some(link) =>
Expand All @@ -26,20 +24,14 @@ class ResultPublisher(fetcher: (SimpleRequest, Client) => Task[SimpleResponse])
None
)

SslContextMap(request)(
sslContext =>
simpleRequestWithoutFakeHeader => {
val client = Http4sClientHelper.buildPooledBlazeHttpClient(maxTotalConnections, clientTimeout, sslContext)
fetcher(simpleRequestWithoutFakeHeader, client)
.map { response =>
if (response.is2xx) {
PactLogger.message(s"Verification results published for provider ${result.pact.provider} and consumer ${result.pact.consumer}")
} else {
PactLogger.error(s"Publish verification results failed with ${response.statusCode}".red)
}
}
fetcher(request, client)
.map { response =>
if (response.is2xx) {
PactLogger.message(s"Verification results published for provider ${result.pact.provider} and consumer ${result.pact.consumer}")
} else {
PactLogger.error(s"Publish verification results failed with ${response.statusCode}".red)
}
)
}
case None =>
Task.now(PactLogger.error("Unable to publish verification results as there is no pb:publish-verification-results link".red))
}
Expand All @@ -51,3 +43,11 @@ class ResultPublisher(fetcher: (SimpleRequest, Client) => Task[SimpleResponse])
Option(s"""{ "success": $success, "providerApplicationVersion": "${brokerPublishData.providerVersion}"$buildUrl }""")
}
}

object ResultPublisher {
def apply(clientTimeout: Duration, sslContextName: Option[String])(implicit sslContextMap: SslContextMap): IResultPublisher = {
val sslContext = sslContextMap(sslContextName)
val client = Http4sClientHelper.buildPooledBlazeHttpClient(2, clientTimeout, sslContext)
new ResultPublisher(client)(Http4sClientHelper.doRequest)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.itv.scalapact.http4s17.impl
import com.itv.scalapact.shared._
import com.itv.scalapact.shared.typeclasses.BrokerPublishData
import fs2.Task
import org.http4s.client.Client
import org.scalatest.{BeforeAndAfter, FunSpec, Matchers}
Expand Down Expand Up @@ -54,7 +55,7 @@ class ResultPublisherSpec extends FunSpec with Matchers with BeforeAndAfter {
Task.now(SimpleResponse(200))
}

private val resultPublisher = new ResultPublisher(fakeCaller)
private val resultPublisher = new ResultPublisher(null)(fakeCaller)

before {
requests = ArrayBuffer.empty[SimpleRequest]
Expand Down
Loading

0 comments on commit 2ab9e38

Please sign in to comment.