laburnum 1.17.1

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
Documentation
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

use {
  super::common::*,
  crate::scheduler::lanes::DEFAULT_LANE,
  macro_rules_attribute::apply,
  std::{
    sync::{
      Arc,
      atomic::{
        AtomicBool,
        Ordering,
      },
    },
    time::Duration,
  },
};

/// Verifies that the scheduler correctly spawns worker threads when requested,
/// and that workers are not spawned prematurely.
#[apply(smol_macros::test!)]
#[test_log::test]
async fn test_scheduler_starts_workers() {
  let (scheduler, _conn) = test_scheduler();

  {
    let workers = scheduler.worker_threads.read();
    assert_eq!(workers.len(), 0, "Workers should not be spawned yet");
  }

  scheduler.spawn_workers();

  {
    let workers = scheduler.worker_threads.read();
    assert!(
      !workers.is_empty(),
      "Workers should be spawned after spawn_workers()"
    );
  }
}

#[apply(smol_macros::test!)]
#[test_log::test]
async fn test_queue_method() {
  let (scheduler, _conn) = test_scheduler();
  let task_executed = Arc::new(AtomicBool::new(false));
  let exec = task_executed.clone();

  scheduler.queue(
    move |_ctx| {
      let executed = exec.clone();
      async move {
        executed.store(true, Ordering::SeqCst);
        None
      }
    },
    DEFAULT_LANE,
  );

  scheduler.spawn_workers();

  assert!(
    wait_for(
      || task_executed.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Task queued via queue() method did not execute"
  );
}