Skip to content

Commit

Permalink
Add Run Before (#840)
Browse files Browse the repository at this point in the history
* Add Run Before

* Add Run Before and After

* use renamed operator
  • Loading branch information
d11-amitsingh authored Jan 15, 2022
1 parent 9044cb2 commit 907da63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions zio-http/src/main/scala/zhttp/http/Middleware.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ sealed trait Middleware[-R, +E, +AIn, -BIn, -AOut, +BOut] { self =>
final def runAfter[R1 <: R, E1 >: E](effect: ZIO[R1, E1, Any]): Middleware[R1, E1, AIn, BIn, AOut, BOut] =
self.mapZIO(bOut => effect.as(bOut))

final def runBefore[R1 <: R, E1 >: E](effect: ZIO[R1, E1, Any]): Middleware[R1, E1, AIn, BIn, AOut, BOut] =
self.contramapZIO(b => effect.as(b))

/**
* Applies Middleware based only if the condition function evaluates to true
*/
Expand Down
16 changes: 13 additions & 3 deletions zio-http/src/test/scala/zhttp/http/MiddlewareSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,20 @@ object MiddlewareSpec extends DefaultRunnableSpec with HExitAssertion {
val app = Http.identity[Int] @@ mid
assertM(app(0))(equalTo(3))
} +
testM("runBefore") {
val mid = Middleware.identity.runBefore(console.putStrLn("A"))
val app = Http.fromZIO(console.putStrLn("B")) @@ mid
assertM(app(()) *> TestConsole.output)(equalTo(Vector("A\n", "B\n")))
} +
testM("runAfter") {
val mid = Middleware.succeed(1).runAfter(console.putStrLn("A"))
val app = Http.succeed(1) @@ mid
assertM(app(()) *> TestConsole.output)(equalTo(Vector("A\n")))
val mid = Middleware.identity.runAfter(console.putStrLn("B"))
val app = Http.fromZIO(console.putStrLn("A")) @@ mid
assertM(app(()) *> TestConsole.output)(equalTo(Vector("A\n", "B\n")))
} +
testM("runBefore and runAfter") {
val mid = Middleware.identity.runBefore(console.putStrLn("A")).runAfter(console.putStrLn("C"))
val app = Http.fromZIO(console.putStrLn("B")) @@ mid
assertM(app(()) *> TestConsole.output)(equalTo(Vector("A\n", "B\n", "C\n")))
} +
testM("race") {
val mid = Middleware.succeed('A').delay(2 second) race Middleware.succeed("B").delay(1 second)
Expand Down

7 comments on commit 907da63

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 826796.57

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 855114.01

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 839123.62

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 834958.04

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 856531.90

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 835569.52

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 847252.43

Please sign in to comment.