-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReadmeLogSubmissionCodeSnippetsTest.scala
More file actions
225 lines (192 loc) · 7.77 KB
/
ReadmeLogSubmissionCodeSnippetsTest.scala
File metadata and controls
225 lines (192 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 2018-2026 LaserDisc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import scala.annotation.nowarn
@nowarn final class ReadmeLogSubmissionCodeSnippetsTest extends AnyWordSpecLike with Matchers {
"`in a monadic sequence of effects` snippet should compile" in {
import cats.effect.Sync
import cats.syntax.flatMap._
import cats.syntax.functor._
import log.effect.LogWriter
def process[F[_]](implicit F: Sync[F], log: LogWriter[F]): F[(Int, Int)] =
for {
_ <- log.trace("We start")
a <- F.delay(10)
_ <- log.trace("Keep going")
b <- F.delay(20)
_ <- log.trace("We reached this point")
_ <- log.info(s"Process complete: ${(a, b)}")
} yield (a, b)
}
"`in a streaming environment using `LogWriter`'s syntax` snippet should compile" in {
import java.nio.channels.AsynchronousChannelGroup
import cats.effect.Async
import cats.syntax.flatMap._
import log.effect.LogWriter
import scala.concurrent.ExecutionContext
sealed trait RedisClient[F[_]] {
def address: String
}
object RedisClient {
def apply[F[_]](addr: String): fs2.Stream[F, RedisClient[F]] =
fs2.Stream.emit(new RedisClient[F] { val address = addr })
}
implicit def EC: ExecutionContext = ???
implicit def CG: AsynchronousChannelGroup = ???
def redisClient[F[_]: Async](
address: String
)(implicit log: LogWriter[F]): fs2.Stream[F, RedisClient[F]] =
RedisClient[F](address) evalMap { client =>
log.info(s"Laserdisc Redis client for $address") >> Async[F].pure(client)
}
}
"`in a streaming environment using `fs2` streams' syntax` snippet should compile" in {
import java.nio.channels.AsynchronousChannelGroup
import cats.effect.Async
import log.effect.LogWriter
import log.effect.fs2.syntax._
import scala.concurrent.ExecutionContext
sealed trait RedisClient[F[_]] {
def address: String
}
object RedisClient {
def apply[F[_]](addr: String): fs2.Stream[F, RedisClient[F]] =
fs2.Stream.emit(new RedisClient[F] { val address = addr })
}
implicit def EC: ExecutionContext = ???
implicit def CG: AsynchronousChannelGroup = ???
def redisCache[F[_]: Async](
address: String
)(implicit log: LogWriter[F]): fs2.Stream[F, RedisClient[F]] =
for {
_ <- log.infoS(s"About to connect a Laserdisc Redis client for $address")
client <- RedisClient[F](address)
_ <- log.infoS("The connection went fine. Talking to the server")
} yield client
}
"`through the companion's syntax` snippet should compile" in {
import cats.effect.Sync
import cats.syntax.apply._
import cats.syntax.flatMap._
import log.effect.LogWriter
def double[F[_]: Sync: LogWriter](source: fs2.Stream[F, Int]): fs2.Stream[F, Int] =
source evalMap { n =>
LogWriter.debug("Processing a number") >>
LogWriter.debug(n.toString) >>
Sync[F].pure(n * 2) <*
LogWriter.debug("Processed")
} handleErrorWith { th =>
fs2.Stream eval (
LogWriter.error("Ops, something didn't work", th) >> Sync[F].pure(0)
)
}
}
"`through the companion's syntax for `fs2` streams` snippet should compile" in {
import cats.effect.Sync
import cats.syntax.apply._
import cats.syntax.flatMap._
import log.effect.LogWriter
import log.effect.fs2.syntax._
def double[F[_]: Sync: LogWriter](source: fs2.Stream[F, Int]): fs2.Stream[F, Int] =
(source >>= { n =>
LogWriter.debugS("Processing a number") >>
LogWriter.debugS(n.toString) >>
fs2.Stream.eval(Sync[F].pure(n * 2)) <*
LogWriter.debugS("Processed")
}) handleErrorWith { th =>
LogWriter.errorS("Ops, something didn't work", th) >> fs2.Stream.eval(Sync[F].pure(0))
}
}
"`through the companion's accessor for the `write` method` 1 snippet should compile" in {
import java.nio.channels.AsynchronousChannelGroup
import cats.Show
import cats.effect.Async
import cats.instances.string._
import cats.syntax.either._
import cats.syntax.flatMap._
import log.effect.LogLevels.{Debug, Error}
import log.effect.fs2.interop.show._
import log.effect.{Failure, LogWriter}
import scala.concurrent.ExecutionContext
sealed trait RedisClient[F[_]] {
def address: String
}
object RedisClient {
def apply[F[_]](addr: String): fs2.Stream[F, RedisClient[F]] =
fs2.Stream.emit(new RedisClient[F] { val address = addr })
}
type |[A, B] = Either[A, B]
implicit def EC: ExecutionContext = ???
implicit def CG: AsynchronousChannelGroup = ???
def redisClient[F[_]: Async: LogWriter](
address: String
): fs2.Stream[F, Throwable | RedisClient[F]] = {
// Cats Show instances are needed for every logged type
implicit val clientShow: Show[RedisClient[F]] = ???
RedisClient[F](address) evalMap { client =>
LogWriter.write(Debug, "Connected client details:") >> // Or
LogWriter.debug(address) >> // And
LogWriter.debug(client) >>
Async[F].pure(client.asRight)
} handleErrorWith { th =>
fs2.Stream eval (
LogWriter.write(
Error,
Failure("Ops, something didn't work", th)
) >> Async[F].pure(th.asLeft)
)
}
}
}
"`or using the fs2 Stream specific syntax like `writeS` or the level alternatives for types that provide a `cats.Show` instance` snippet should compile" in {
import cats.Show
import cats.effect.Sync
import log.effect.LogLevels.Error
import log.effect.{Failure, LogWriter}
import log.effect.fs2.syntax._
trait A
object A {
def empty: A = ???
implicit val aShow: Show[A] = new Show[A] {
override def show(t: A): String = ???
}
}
def double[F[_]: Sync: LogWriter](source: fs2.Stream[F, Int]): fs2.Stream[F, A] = {
// Cats Show instances are needed for every logged type
implicit def intShow: Show[Int] = ???
def processAnInt: Int => A = ???
(for {
n <- source
_ <- LogWriter.debugS("Processing a number")
_ <- LogWriter.debugS(n) // N.B. the syntax requires a `cats.Show` for `Int`
r <- (processAnInt andThen fs2.Stream.emit)(n)
_ <- LogWriter.debugS("Processed")
_ <- LogWriter.debugS(r) // Same here, a `cats.Show` for `A` is needed
} yield r) handleErrorWith { th =>
LogWriter.writeS(
Error,
Failure("Ops, something didn't work", th)
) >> fs2.Stream.emit(A.empty) // and `write again`
}
}
}
}