From 94a3a51db76f45f8941d8a6cb649da0624fde67b Mon Sep 17 00:00:00 2001
From: chenjiayi <chenjiayi22@huawei.com>
Date: Thu, 16 Nov 2023 11:16:12 +0800
Subject: [PATCH 066/103] test(devmaster): optimize UT for testing framework
 and add some new cases

---
 exts/devmaster/src/bin/devctl/daemon/mod.rs   |  7 ++
 exts/devmaster/src/lib/framework/job_queue.rs | 85 ++++++++++++-------
 .../src/lib/framework/worker_manager.rs       |  1 +
 3 files changed, 64 insertions(+), 29 deletions(-)

diff --git a/exts/devmaster/src/bin/devctl/daemon/mod.rs b/exts/devmaster/src/bin/devctl/daemon/mod.rs
index 3e4bd96d..53931650 100644
--- a/exts/devmaster/src/bin/devctl/daemon/mod.rs
+++ b/exts/devmaster/src/bin/devctl/daemon/mod.rs
@@ -71,6 +71,13 @@ mod test {
             std::thread::sleep(std::time::Duration::from_secs(1));
 
             let dev = Device::from_subsystem_sysname("net", "lo").unwrap();
+
+            /* Trigger more than the number of workers. */
+            dev.trigger(DeviceAction::Change).unwrap();
+            dev.trigger(DeviceAction::Change).unwrap();
+            dev.trigger(DeviceAction::Change).unwrap();
+            dev.trigger(DeviceAction::Change).unwrap();
+            dev.trigger(DeviceAction::Change).unwrap();
             dev.trigger(DeviceAction::Change).unwrap();
 
             /* Sleep more than 3 seconds to wait for the workers being recycled. */
diff --git a/exts/devmaster/src/lib/framework/job_queue.rs b/exts/devmaster/src/lib/framework/job_queue.rs
index 7c6d6a8d..e488f480 100644
--- a/exts/devmaster/src/lib/framework/job_queue.rs
+++ b/exts/devmaster/src/lib/framework/job_queue.rs
@@ -22,7 +22,6 @@ use std::{
     cell::RefCell,
     cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
     collections::VecDeque,
-    fmt::{self, Display},
     rc::{Rc, Weak},
 };
 
@@ -37,20 +36,8 @@ pub enum JobState {
     Running,
 }
 
-impl Display for JobState {
-    ///
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let state = match self {
-            JobState::Undef => "Undef",
-            JobState::Queued => "Queued",
-            JobState::Running => "Running",
-        };
-
-        write!(f, "{}", state)
-    }
-}
-
 /// device job
+#[derive(Debug)]
 pub struct DeviceJob {
     /// internal device
     pub device: Device,
@@ -168,8 +155,6 @@ impl JobQueue {
             gc.close_killer(e);
         }
 
-        // self.job_queue_show_state();
-
         for job in self.jobs.borrow().iter() {
             match job.get_state() {
                 JobState::Queued => {}
@@ -241,19 +226,6 @@ impl JobQueue {
         log::debug!("Job Queue: insert job {}", seqnum);
     }
 
-    // /// cleanup the job queue, if match_state is Undef, cleanup all jobs, otherwise just retain the unmatched jobs
-    // pub(crate) fn job_queue_cleanup(&self, match_state: JobState) {
-    //     self.jobs.borrow_mut().retain_mut(|job| {
-    //         if match_state != JobState::Undef && match_state != job.get_state() {
-    //             return true;
-    //         }
-
-    //         false
-    //     });
-
-    //     log::debug!("Job Queue: cleanup");
-    // }
-
     /// free a job from job queue
     pub(crate) fn job_free(&self, job: &Rc<DeviceJob>) {
         job.job_free();
@@ -289,3 +261,58 @@ impl JobQueue {
         self.jobs.borrow().is_empty()
     }
 }
+
+#[cfg(test)]
+mod test {
+    use super::*;
+    use std::sync::{Arc, RwLock};
+
+    #[test]
+    fn test_job_cmp() {
+        let j1 = DeviceJob::new(
+            Device::from_subsystem_sysname("net", "lo").unwrap(),
+            JobState::Queued,
+            1000,
+        );
+
+        let j2 = DeviceJob::new(
+            Device::from_subsystem_sysname("net", "lo").unwrap(),
+            JobState::Queued,
+            1000,
+        );
+
+        assert_eq!(j1, j2);
+    }
+
+    #[test]
+    fn test_job_queue() {
+        let events = Rc::new(Events::new().unwrap());
+        let cache = Cache::new(vec![], vec![]);
+        let devmaster = Rc::new(RefCell::new(Devmaster {
+            events,
+            worker_manager: None,
+            control_manager: None,
+            monitor: None,
+            job_queue: None,
+            gc: None,
+            cache: Arc::new(RwLock::new(cache)),
+        }));
+
+        let job_queue = JobQueue::new(devmaster);
+
+        /* Test start job when the queue is empty. */
+        job_queue.job_queue_start(None);
+
+        let dev = Device::from_subsystem_sysname("net", "lo").unwrap();
+        job_queue.job_queue_insert(dev);
+
+        /* Test insert dulplicate jobs with the same seqnum. */
+        let dev = Device::from_subsystem_sysname("net", "lo").unwrap();
+        dev.set_seqnum_from_string("1000").unwrap();
+        job_queue.job_queue_insert(dev);
+
+        let dev = Device::from_subsystem_sysname("net", "lo").unwrap();
+        dev.set_seqnum_from_string("1000").unwrap();
+        job_queue.job_queue_insert(dev);
+    }
+}
diff --git a/exts/devmaster/src/lib/framework/worker_manager.rs b/exts/devmaster/src/lib/framework/worker_manager.rs
index 870f6779..bddc9deb 100644
--- a/exts/devmaster/src/lib/framework/worker_manager.rs
+++ b/exts/devmaster/src/lib/framework/worker_manager.rs
@@ -64,6 +64,7 @@ pub struct WorkerManager {
 }
 
 /// worker
+#[derive(Debug)]
 pub struct Worker {
     /// worker unique id
     id: u32,
-- 
2.33.0

