8000 The unit test ```test_shrink``` make no sense · Issue #114 · rust-threadpool/rust-threadpool · GitHub
[go: up one dir, main page]

Skip to content

The unit test test_shrink make no sense #114

@yanghaku

Description

@yanghaku

In the test_shrink, if comment the function call pool.set_num_threads(TEST_TASKS); , the test also can pass. Because after_shrink_size == TEST_TASKS and active threads == TEST_TASKS.

Then I want to try write a test case that can test shrink, but it will test fail sometimes (because the implementation in lib.rs).

The code is as follows, test_no_shrink is for the origin test case I metion above, test_shrink_in_sleep is my custom test case using sleep.

#[cfg(test)]
mod test {
    use std::{
        sync::{Arc, Barrier},
        thread::sleep,
        time::Duration,
    };
    use threadpool::ThreadPool;

    const TEST_TASKS: usize = 4;
    #[test]
    fn test_no_shrink() {
        let test_tasks_begin = TEST_TASKS + 2;

        let pool = ThreadPool::new(test_tasks_begin);
        let b0 = Arc::new(Barrier::new(test_tasks_begin + 1));
        let b1 = Arc::new(Barrier::new(test_tasks_begin + 1));

        for _ in 0..test_tasks_begin {
            let (b0, b1) = (b0.clone(), b1.clone());
            pool.execute(move || {
                b0.wait();
                b1.wait();
            });
        }

        let b2 = Arc::new(Barrier::new(TEST_TASKS + 1));
        let b3 = Arc::new(Barrier::new(TEST_TASKS + 1));

        for _ in 0..TEST_TASKS {
            let (b2, b3) = (b2.clone(), b3.clone());
            pool.execute(move || {
                b2.wait();
                b3.wait();
            });
        }

        b0.wait();

        // no shrink call, it also can test successfully
        // pool.set_num_threads(TEST_TASKS);

        assert_eq!(pool.active_count(), test_tasks_begin);
        b1.wait();

        b2.wait();
        assert_eq!(pool.active_count(), TEST_TASKS);
        b3.wait();
    }

    #[test]
    fn test_shrink_in_sleep() {
        let test_tasks_begin = TEST_TASKS / 2;
        let shrink_tasks = 1;

        let mut pool = ThreadPool::new(test_tasks_begin);

        for _ in 0..TEST_TASKS {
            pool.execute(move || {
                sleep(Duration::from_secs(1));
            })
        }

        sleep(Duration::from_millis(100));
        pool.set_num_threads(shrink_tasks);

        /*
         *  if TEST_TASKS = 4:
         *
         *  time        tasks
         *  [0-1]       task0, task1
         *  [1-2]       task2
         *  [2-3]       task3
         */

        sleep(Duration::from_secs(1));
        assert_eq!(pool.active_count(), 1);
    }
}

This is the test result:

running 2 tests
test test::test_no_shrink ... ok
test test::test_shrink_in_sleep ... FAILED

failures:

---- test::test_shrink_in_sleep stdout ----
thread 'test::test_shrink_in_sleep' panicked at 'assertion failed: `(left == right)`
  left: `2`,
 right: `1`', src/main.rs:77:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    test::test_shrink_in_sleep

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 1.10s

Although I know the thread pool does not need such a precise shrink, I want to ask how to write a test case to test whether the shrink really works and the test case can pass everytime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0