```
--- a/libvirt/tests/src/virsh_cmd/domain/virsh_domfstrim.py
+++ b/libvirt/tests/src/virsh_cmd/domain/virsh_domfstrim.py
@@ -3,7 +3,7 @@ import os
 import time
 import commands
 from autotest.client.shared import error
-from virttest import virsh
+from virttest import virsh, utils_misc
 from virttest.libvirt_xml import vm_xml
 from virttest.libvirt_xml.devices.disk import Disk
 from virttest.libvirt_xml.devices.controller import Controller
@@ -117,47 +117,47 @@ def run_virsh_domfstrim(test, params, env):
                                  cmd_result.stderr)
         map_cmd = "cat /sys/bus/pseudo/drivers/scsi_debug/map"
         ori_diskmap = commands.getoutput(map_cmd).strip('\n\x00')
-        ori_limit = ori_diskmap.split(",")[-1].split("-")[-1]
+        ori_limit = int(ori_diskmap.split(",")[-1].split("-")[-1])
         logging.debug("origin disk map is %s", ori_diskmap)
 
         # Write date in disk
         dd_cmd = "dd if=/dev/zero of=/home/test/file bs=1048576 count=5"
         guest_session.cmd(dd_cmd)
-        time.sleep(10)
+        def _full_mapped():
+            full_diskmap = commands.getoutput(map_cmd).strip('\n\x00')
+            full_limit = int(full_diskmap.split(",")[-1].split("-")[-1])
+            logging.debug("Limit in map after dd command is %s.", full_limit)
+            return (ori_limit < full_limit)
+        if not utils_misc.wait_for(_full_mapped, timeout=30):
+            raise error.TestError("Scsi map is not updated after dd command.")
 
         full_diskmap = commands.getoutput(map_cmd).strip('\n\x00')
-        full_limit = full_diskmap.split(",")[-1].split("-")[-1]
-        logging.debug("disk map after write data is %s", full_diskmap)
+        full_limit = int(full_diskmap.split(",")[-1].split("-")[-1])
 
         # Remove disk content in guest
         guest_session.cmd("rm -rf /home/test/*")
-        time.sleep(10)
+        def _trim_completed():
+            cmd_result = virsh.domfstrim(vm_name, options)
+            if cmd_result.exit_status != 0:
+                if status_error == "no":
+                    raise error.TestFail("Fail to do virsh domfstrim, error %s" %
+                                         cmd_result.stderr)
+                else:
+                    logging.info("Fail to do virsh domfstrim as expected: %s",
+                                 cmd_result.stderr)
+                    return True
+            empty_diskmap = commands.getoutput(map_cmd).strip('\n\x00')
+            empty_limit = int(empty_diskmap.split(",")[-1].split("-")[-1])
+            logging.debug("Limit of map after remove is %s.", empty_limit)
 
-        cmd_result = virsh.domfstrim(vm_name, options)
-        if cmd_result.exit_status != 0:
-            if status_error == "no":
-                raise error.TestFail("Fail to do virsh domfstrim, error %s" %
-                                     cmd_result.stderr)
+            if is_fulltrim == "yes":
+                return empty_limit == ori_limit
             else:
-                logging.info("Fail to do virsh domfstrim as expected: %s",
-                             cmd_result.stderr)
-                return
+                return empty_limit <= full_limit
 
-        empty_diskmap = commands.getoutput(map_cmd).strip('\n\x00')
-        empty_limit = empty_diskmap.split(",")[-1].split("-")[-1]
-        logging.debug("disk map after fstrim is %s", empty_diskmap)
+        if not utils_misc.wait_for(_trim_completed, timeout=30):
+            raise error.TestFail("error message.")
 
-        if is_fulltrim == "yes":
-            if empty_limit == ori_limit:
-                logging.info("Success to do fstrim")
-            else:
-                raise error.TestFail("Fail to do fstrim, map is %s" %
-                                     empty_diskmap)
-        else:
-            if int(ori_limit) < int(empty_limit) <= int(full_limit):
-                logging.info("success to do fstrim partly")
-            else:
-                raise error.TestFail("Fail to do fstrim partly")
     finally:
         # Do domain recovery
         xml_backup.sync()
```

Just FYI, this is a modification based on your code, you can copy it and ```git apply``` it. 
Wish it is helpful to you!!

BTW, variables such as is_fulltrim we can get it as a bool type by ```is_fulltrim = ("yes" == params.get("XXX", "no"))```