|
3 | 3 | [clojure.test :refer [deftest is testing]])
|
4 | 4 | (:import [java.util.concurrent
|
5 | 5 | CompletionStage
|
| 6 | + CompletableFuture |
6 | 7 | Executors]))
|
7 | 8 |
|
8 | 9 | (defn fn->Function [function]
|
|
323 | 324 | (dorun (for [method-info alt-method-info
|
324 | 325 | mode [:raw :async :with-executor]]
|
325 | 326 | (test-alt-error method-info mode executor))))))
|
| 327 | + |
| 328 | + |
| 329 | + |
| 330 | +(def compose-method-info |
| 331 | + {:methods {:raw |
| 332 | + (fn [^CompletionStage this operator _] |
| 333 | + (.thenCompose this operator)) |
| 334 | + :async |
| 335 | + (fn [^CompletionStage this operator _] |
| 336 | + (.thenComposeAsync this operator)) |
| 337 | + :with-executor |
| 338 | + (fn [^CompletionStage this operator executor] |
| 339 | + (.thenComposeAsync this operator executor))} |
| 340 | + :interface fn->Function |
| 341 | + :inner-assertion #(is (= 1 %)) |
| 342 | + :post-assertion #(is (= 2 %))}) |
| 343 | + |
| 344 | +(defn- test-compose-success [method-info mode executor] |
| 345 | + |
| 346 | + (let [was-called (atom false) |
| 347 | + |
| 348 | + method (get-in method-info [:methods mode]) |
| 349 | + {:keys [inner-assertion post-assertion] |
| 350 | + to-java-interface :interface} method-info |
| 351 | + |
| 352 | + d1 (d/success-deferred 1) |
| 353 | + d2 (method |
| 354 | + d1 |
| 355 | + (to-java-interface |
| 356 | + (fn [x] |
| 357 | + (inner-assertion x) |
| 358 | + (reset! was-called true) |
| 359 | + (d/success-deferred 2))) |
| 360 | + executor)] |
| 361 | + |
| 362 | + (is (= @d1 1)) |
| 363 | + (post-assertion @d2) |
| 364 | + (is (= true @was-called)))) |
| 365 | + |
| 366 | + |
| 367 | +(deftest test-compose |
| 368 | + |
| 369 | + (let [executor (Executors/newSingleThreadExecutor)] |
| 370 | + (testing "compose success" |
| 371 | + (dorun (for [method-info [compose-method-info] |
| 372 | + mode [:raw :async :with-executor]] |
| 373 | + (test-compose-success method-info mode executor)))) |
| 374 | + |
| 375 | + (testing "compose error" |
| 376 | + (dorun (for [method-info [compose-method-info] |
| 377 | + mode [:raw :async :with-executor]] |
| 378 | + (test-functor-error method-info mode executor)))))) |
| 379 | + |
| 380 | +(deftest test-compose-into-completable-future |
| 381 | + |
| 382 | + (testing "deferred can compose into CompletableFuture" |
| 383 | + (let [d1 ^CompletionStage (d/success-deferred 10) |
| 384 | + d2 (.thenCompose |
| 385 | + d1 |
| 386 | + (fn->Function |
| 387 | + (fn [x] (CompletableFuture/completedFuture (inc x))) |
| 388 | + ))] |
| 389 | + (is (= @d2 11))))) |
0 commit comments