|
481 | 481 | (h/assert-take output-ch)))
|
482 | 482 | (server/shutdown server))))
|
483 | 483 |
|
| 484 | +(defn- core-async-dispatch-thread? [^Thread thread] |
| 485 | + (re-matches #"async-dispatch-\d+" (.getName thread))) |
| 486 | + |
| 487 | +(deftest can-determine-core-async-dispatch-thread |
| 488 | + (testing "current thread" |
| 489 | + (is (not (core-async-dispatch-thread? (Thread/currentThread))))) |
| 490 | + (testing "thread running go blocks" |
| 491 | + (let [thread (async/<!! (async/go (Thread/currentThread)))] |
| 492 | + (is (core-async-dispatch-thread? thread)))) |
| 493 | + (testing "thread running core.async thread macro" |
| 494 | + (let [thread (async/<!! (async/thread (Thread/currentThread)))] |
| 495 | + (is (not (core-async-dispatch-thread? thread)))))) |
| 496 | + |
| 497 | +(deftest request-should-complete-on-a-suitable-executor |
| 498 | + (testing "successful completion" |
| 499 | + (let [input-ch (async/chan 3) |
| 500 | + output-ch (async/chan 3) |
| 501 | + server (server/chan-server {:output-ch output-ch |
| 502 | + :input-ch input-ch}) |
| 503 | + _ (server/start server nil) |
| 504 | + thread-p (-> (server/send-request server "req" {:body "foo"}) |
| 505 | + (p/then (fn [_] (Thread/currentThread)))) |
| 506 | + client-rcvd-msg (h/assert-take output-ch) |
| 507 | + _ (async/put! input-ch (lsp.responses/response (:id client-rcvd-msg) {:result "good"})) |
| 508 | + thread (deref thread-p 100 nil)] |
| 509 | + (is (not (core-async-dispatch-thread? thread))) |
| 510 | + (is (instance? java.util.concurrent.ForkJoinWorkerThread thread) |
| 511 | + "completes on default ForkJoinPool executor") |
| 512 | + (server/shutdown server))) |
| 513 | + (testing "exceptional completion" |
| 514 | + (let [input-ch (async/chan 3) |
| 515 | + output-ch (async/chan 3) |
| 516 | + server (server/chan-server {:output-ch output-ch |
| 517 | + :input-ch input-ch}) |
| 518 | + _ (server/start server nil) |
| 519 | + thread-p (-> (server/send-request server "req" {:body "foo"}) |
| 520 | + (p/catch (fn [_] (Thread/currentThread)))) |
| 521 | + client-rcvd-msg (h/assert-take output-ch) |
| 522 | + _ (async/put! input-ch |
| 523 | + (-> (lsp.responses/response (:id client-rcvd-msg)) |
| 524 | + (lsp.responses/error {:code 1234 |
| 525 | + :message "Something bad" |
| 526 | + :data {:body "foo"}}))) |
| 527 | + thread (deref thread-p 100 nil)] |
| 528 | + (is (not (core-async-dispatch-thread? thread))) |
| 529 | + (is (instance? java.util.concurrent.ForkJoinWorkerThread thread) |
| 530 | + "completes on default ForkJoinPool executor") |
| 531 | + (server/shutdown server))) |
| 532 | + (testing "completion with :current-thread executor for legacy behavior" |
| 533 | + (let [input-ch (async/chan 3) |
| 534 | + output-ch (async/chan 3) |
| 535 | + server (server/chan-server {:output-ch output-ch |
| 536 | + :input-ch input-ch |
| 537 | + :response-executor :current-thread}) |
| 538 | + _ (server/start server nil) |
| 539 | + thread-p (-> (server/send-request server "req" {:body "foo"}) |
| 540 | + (p/then (fn [_] (Thread/currentThread)))) |
| 541 | + client-rcvd-msg (h/assert-take output-ch) |
| 542 | + _ (async/put! input-ch (lsp.responses/response (:id client-rcvd-msg) {:result "good"})) |
| 543 | + thread (deref thread-p 100 nil)] |
| 544 | + (is (core-async-dispatch-thread? thread) "completes on core.async dispatch thread") |
| 545 | + (server/shutdown server)))) |
| 546 | + |
484 | 547 | (def fixed-clock
|
485 | 548 | (-> (java.time.LocalDateTime/of 2022 03 05 13 35 23 0)
|
486 | 549 | (.toInstant java.time.ZoneOffset/UTC)
|
|
0 commit comments