Repository: tokio-rs/tokio
File: tokio/src/sync/semaphore.rs (Rust)

Code before:
/// #        }
/// #    }
/// #    pub fn insert(&mut self, key: &str, value: i32) {
/// #        self.map.insert(key.to_string(), value);
/// #    }
/// #    pub fn update(&mut self, key: &str, value: i32) {
/// #        self.map
/// #            .entry(key.to_string())
/// #            .and_modify(|origin| *origin = value);
/// #    }
/// #    pub fn delete(&mut self, key: &str) {
/// #        self.map.remove(key);
/// #    }
/// #    pub fn get(&mut self, key: &str) -> i32 {
/// #        *self.map.get(key).unwrap()
/// #    }
/// # }
/// // Initialize a static semaphore with only one permit, which is used to
/// // prevent test_insert and test_update from running in parallel.
/// static PERMIT: Semaphore = Semaphore::const_new(1);
///
/// // Initialize the database that will be used by the subsequent tests.
/// lazy_static! {
///     static ref DB: Mutex<Database> = Mutex::new(Database::setup());
/// }
///
/// async fn test_insert() {
///
///     // Acquire permit before proceeding. Since the semaphore has only one permit,
///     // the test will wait if the permit is already acquired by other tests.
///     let permit = PERMIT.acquire().await.unwrap();
///
///     // Do the actual test stuff with database
///
///     // Insert a key-value pair to database
///     let (key, value) = ("name", 0);
///     DB.lock().await.insert(key, value);
///
///     // Verify if the value has been inserted correctly.
///     assert_eq!(DB.lock().await.get(key), value);
///
///     // Undo the insertation, make database status unchanged.
///     DB.lock().await.delete(key);
///
///     // Drop permit, make possible waiting test to run.
///     drop(permit);
/// }
///
/// async fn test_update() {
///
///     // Acquire permit before proceeding. Since the semaphore has only one permit,

Review comment (suggestion):
Since `Mutex::new` is const, you don't need `lazy_static!` for this.
```suggestion
/// static DB: Mutex<Database> = Mutex::new(Database::setup());
```

Code after:
/// #        }
/// #    }
/// #    pub async fn insert(&self, key: &str, value: i32) {
/// #        self.map.lock().await.insert(key.to_string(), value);
/// #    }
/// #    pub async fn update(&self, key: &str, value: i32) {
/// #        self.map.lock().await
/// #            .entry(key.to_string())
/// #            .and_modify(|origin| *origin = value);
/// #    }
/// #    pub async fn delete(&self, key: &str) {
/// #        self.map.lock().await.remove(key);
/// #    }
/// #    pub async fn get(&self, key: &str) -> i32 {
/// #        *self.map.lock().await.get(key).unwrap()
/// #    }
/// # }
///
/// // Initialize a static semaphore with only one permit, which is used to
/// // prevent test_insert and test_update from running in parallel.
/// static PERMIT: Semaphore = Semaphore::const_new(1);
///
/// // Initialize the database that will be used by the subsequent tests.
/// static DB: Database = Database::setup();
///
/// #[tokio::test]
/// # async fn fake_test() {}
/// async fn test_insert() {
///     // Acquire permit before proceeding. Since the semaphore has only one permit,
///     // the test will wait if the permit is already acquired by other tests.
///     let permit = PERMIT.acquire().await.unwrap();
///
///     // Do the actual test stuff with database
///
///     // Insert a key-value pair to database
///     let (key, value) = ("name", 0);
///     DB.insert(key, value).await;
///
///     // Verify that the value has been inserted correctly.
///     assert_eq!(DB.get(key).await, value);
///
///     // Undo the insertion, so the database is empty at the end of the test.
///     DB.delete(key).await;
///
///     // Drop permit. This allows the other test to start running.
///     drop(permit);
/// }
///
/// #[tokio::test]
/// # async fn fake_test() {}
/// async fn test_update() {