|
| 1 | +package effectie.instances.future |
| 2 | + |
| 3 | +import cats.data.EitherT |
| 4 | +import cats.syntax.all._ |
| 5 | +import effectie.core._ |
| 6 | +import effectie.syntax.all._ |
| 7 | +import effectie.testing.types._ |
| 8 | +import extras.concurrent.testing.ConcurrentSupport |
| 9 | +import extras.concurrent.testing.types.{ErrorLogger, WaitFor} |
| 10 | +import hedgehog._ |
| 11 | +import hedgehog.runner._ |
| 12 | + |
| 13 | +/** @author Kevin Lee |
| 14 | + * @since 2020-07-31 |
| 15 | + */ |
| 16 | +object canCatchFutureSpec extends Properties { |
| 17 | + |
| 18 | + private implicit val errorLogger: ErrorLogger[Throwable] = ErrorLogger.printlnDefaultErrorLogger |
| 19 | + |
| 20 | + override def tests: List[Test] = futureSpecs |
| 21 | + |
| 22 | + /* Future */ |
| 23 | + val futureSpecs = List( |
| 24 | + example( |
| 25 | + "test CanCatch[Future]catchNonFatalEitherT should catch NonFatal", |
| 26 | + FutureSpec.testCanCatch_Future_catchNonFatalEitherTShouldCatchNonFatal, |
| 27 | + ), |
| 28 | + example( |
| 29 | + "test CanCatch[Future]catchNonFatalEitherT should return the successful result", |
| 30 | + FutureSpec.testCanCatch_Future_catchNonFatalEitherTShouldReturnSuccessfulResult, |
| 31 | + ), |
| 32 | + example( |
| 33 | + "test CanCatch[Future]catchNonFatalEitherT should return the failed result", |
| 34 | + FutureSpec.testCanCatch_Future_catchNonFatalEitherTShouldReturnFailedResult, |
| 35 | + ), |
| 36 | + ) |
| 37 | + |
| 38 | + @SuppressWarnings(Array("org.wartremover.warts.Throw")) |
| 39 | + def throwThrowable[A](throwable: => Throwable): A = |
| 40 | + throw throwable // scalafix:ok DisableSyntax.throw |
| 41 | + |
| 42 | + def run[F[*]: FxCtor, A](a: => A): F[A] = |
| 43 | + FxCtor[F].effectOf(a) |
| 44 | + |
| 45 | + object FutureSpec { |
| 46 | + import effectie.instances.future.canCatch._ |
| 47 | + import effectie.instances.future.fxCtor._ |
| 48 | + |
| 49 | + import java.util.concurrent.{ExecutorService, Executors} |
| 50 | + import scala.concurrent.duration._ |
| 51 | + import scala.concurrent.{ExecutionContext, Future} |
| 52 | + |
| 53 | + val waitFor = WaitFor(1.second) |
| 54 | + |
| 55 | +// def testCanCatch_Future_catchNonFatalThrowableShouldCatchNonFatal: Result = { |
| 56 | +// |
| 57 | +// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 58 | +// implicit val ec: ExecutionContext = |
| 59 | +// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 60 | +// |
| 61 | +// val expectedExpcetion = new RuntimeException("Something's wrong") |
| 62 | +// val fa = run[Future, Int](throwThrowable[Int](expectedExpcetion)) |
| 63 | +// val expected = expectedExpcetion.asLeft[Int] |
| 64 | +// val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 65 | +// executorService, |
| 66 | +// waitFor |
| 67 | +// )(CanCatch[Future].catchNonFatalThrowable(fa)) |
| 68 | +// |
| 69 | +// actual ==== expected |
| 70 | +// } |
| 71 | +// |
| 72 | +// def testCanCatch_Future_catchNonFatalShouldCatchNonFatal: Result = { |
| 73 | +// |
| 74 | +// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 75 | +// implicit val ec: ExecutionContext = |
| 76 | +// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 77 | +// |
| 78 | +// val expectedExpcetion = new RuntimeException("Something's wrong") |
| 79 | +// val fa = run[Future, Int](throwThrowable[Int](expectedExpcetion)) |
| 80 | +// val expected = SomeError.someThrowable(expectedExpcetion).asLeft[Int] |
| 81 | +// val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 82 | +// executorService, |
| 83 | +// waitFor |
| 84 | +// )(CanCatch[Future].catchNonFatal(fa)(SomeError.someThrowable)) |
| 85 | +// |
| 86 | +// actual ==== expected |
| 87 | +// } |
| 88 | +// |
| 89 | +// def testCanCatch_Future_catchNonFatalThrowableShouldReturnSuccessfulResult: Result = { |
| 90 | +// |
| 91 | +// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 92 | +// implicit val ec: ExecutionContext = |
| 93 | +// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 94 | +// |
| 95 | +// val fa = run[Future, Int](1) |
| 96 | +// val expected = 1.asRight[Throwable] |
| 97 | +// val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 98 | +// executorService, |
| 99 | +// waitFor |
| 100 | +// )(CanCatch[Future].catchNonFatalThrowable(fa)) |
| 101 | +// |
| 102 | +// actual ==== expected |
| 103 | +// } |
| 104 | +// |
| 105 | +// def testCanCatch_Future_catchNonFatalShouldReturnSuccessfulResult: Result = { |
| 106 | +// |
| 107 | +// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 108 | +// implicit val ec: ExecutionContext = |
| 109 | +// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 110 | +// |
| 111 | +// val fa = run[Future, Int](1) |
| 112 | +// val expected = 1.asRight[SomeError] |
| 113 | +// val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 114 | +// executorService, |
| 115 | +// waitFor |
| 116 | +// )(CanCatch[Future].catchNonFatal(fa)(SomeError.someThrowable)) |
| 117 | +// |
| 118 | +// actual ==== expected |
| 119 | +// } |
| 120 | +// |
| 121 | +// def testCanCatch_Future_catchNonFatalEitherShouldCatchNonFatal: Result = { |
| 122 | +// |
| 123 | +// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 124 | +// implicit val ec: ExecutionContext = |
| 125 | +// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 126 | +// |
| 127 | +// val expectedExpcetion = new RuntimeException("Something's wrong") |
| 128 | +// val fa = run[Future, Either[SomeError, Int]](throwThrowable[Either[SomeError, Int]](expectedExpcetion)) |
| 129 | +// val expected = SomeError.someThrowable(expectedExpcetion).asLeft[Int] |
| 130 | +// val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 131 | +// executorService, |
| 132 | +// waitFor |
| 133 | +// )(CanCatch[Future].catchNonFatalEither(fa)(SomeError.someThrowable)) |
| 134 | +// |
| 135 | +// actual ==== expected |
| 136 | +// } |
| 137 | +// |
| 138 | +// def testCanCatch_Future_catchNonFatalEitherShouldReturnSuccessfulResult: Result = { |
| 139 | +// |
| 140 | +// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 141 | +// implicit val ec: ExecutionContext = |
| 142 | +// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 143 | +// |
| 144 | +// val fa = run[Future, Either[SomeError, Int]](1.asRight[SomeError]) |
| 145 | +// val expected = 1.asRight[SomeError] |
| 146 | +// val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 147 | +// executorService, |
| 148 | +// waitFor |
| 149 | +// )(CanCatch[Future].catchNonFatalEither(fa)(SomeError.someThrowable)) |
| 150 | +// |
| 151 | +// actual ==== expected |
| 152 | +// } |
| 153 | +// |
| 154 | +// def testCanCatch_Future_catchNonFatalEitherShouldReturnFailedResult: Result = { |
| 155 | +// |
| 156 | +// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 157 | +// implicit val ec: ExecutionContext = |
| 158 | +// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 159 | +// |
| 160 | +// val expectedFailure = SomeError.message("Failed") |
| 161 | +// val fa = run[Future, Either[SomeError, Int]](expectedFailure.asLeft[Int]) |
| 162 | +// val expected = expectedFailure.asLeft[Int] |
| 163 | +// val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 164 | +// executorService, |
| 165 | +// waitFor |
| 166 | +// )(CanCatch[Future].catchNonFatalEither(fa)(SomeError.someThrowable)) |
| 167 | +// |
| 168 | +// actual ==== expected |
| 169 | +// } |
| 170 | + |
| 171 | + def testCanCatch_Future_catchNonFatalEitherTShouldCatchNonFatal: Result = { |
| 172 | + |
| 173 | + implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 174 | + implicit val ec: ExecutionContext = |
| 175 | + ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 176 | + |
| 177 | + val expectedExpcetion = new RuntimeException("Something's wrong") |
| 178 | + val fa = EitherT(run[Future, Either[SomeError, Int]](throwThrowable[Either[SomeError, Int]](expectedExpcetion))) |
| 179 | + val expected = SomeError.someThrowable(expectedExpcetion).asLeft[Int] |
| 180 | + val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 181 | + executorService, |
| 182 | + waitFor, |
| 183 | + )(CanCatch[Future].catchNonFatalEitherT(fa)(SomeError.someThrowable).value) |
| 184 | + |
| 185 | + actual ==== expected |
| 186 | + } |
| 187 | + |
| 188 | + def testCanCatch_Future_catchNonFatalEitherTShouldReturnSuccessfulResult: Result = { |
| 189 | + |
| 190 | + implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 191 | + implicit val ec: ExecutionContext = |
| 192 | + ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 193 | + |
| 194 | + val fa = EitherT(run[Future, Either[SomeError, Int]](1.asRight[SomeError])) |
| 195 | + val expected = 1.asRight[SomeError] |
| 196 | + val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 197 | + executorService, |
| 198 | + waitFor, |
| 199 | + )(CanCatch[Future].catchNonFatalEitherT(fa)(SomeError.someThrowable).value) |
| 200 | + |
| 201 | + actual ==== expected |
| 202 | + } |
| 203 | + |
| 204 | + def testCanCatch_Future_catchNonFatalEitherTShouldReturnFailedResult: Result = { |
| 205 | + |
| 206 | + implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1) |
| 207 | + implicit val ec: ExecutionContext = |
| 208 | + ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger) |
| 209 | + |
| 210 | + val expectedFailure = SomeError.message("Failed") |
| 211 | + val fa = EitherT(run[Future, Either[SomeError, Int]](expectedFailure.asLeft[Int])) |
| 212 | + val expected = expectedFailure.asLeft[Int] |
| 213 | + val actual = ConcurrentSupport.futureToValueAndTerminate( |
| 214 | + executorService, |
| 215 | + waitFor, |
| 216 | + )(CanCatch[Future].catchNonFatalEitherT(fa)(SomeError.someThrowable).value) |
| 217 | + |
| 218 | + actual ==== expected |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | +} |
0 commit comments