Forum Discussion

MartinMet's avatar
MartinMet
New Contributor
5 years ago

Difference between Delay() and time.sleep() (python)

Does someone know the qualitative difference between TestCompletes innate 'Delay()' and time.sleep() in Python? I currently have a listener to a connection to ActiveMQ via stomp, and whenever a message arrives the listener will open a file and write the message to this file. Whenever I ran the code nothing was written in the file and when I tried deleting it I got an error stating that TC was still using this file. After some debugging I switched out TC:s innate Delay() to time.sleep() and then everything suddenly works.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    I currently have a listener to a connection to ActiveMQ via stomp

    Are the listener and other test code exist in the same TestComplete's project and are executed concurrently?

    If yes, then note that multithreaded test code is not officially supported by TestComplete. It might work for certain script engines (like Python) but nothing is guaranteed.

    Instead, I would implement a listener in a separate script and execute it in the standalone Python instance (probably, started from TestComplete when needed and terminated when done) and in TestComplete itself leave only the part that checks the content of the file.

     

    • MartinMet's avatar
      MartinMet
      New Contributor

      Ah okay thank you, that makes sense. The listener and the test code are both in the same script, and I guess that Python somehow allowed for multi-threading using the wait() operation. Thanks for the suggestion!

      • alexaminar's avatar
        alexaminar
        Visitor

        Python sleep() will pause for an hour, day or whatever if given the proper value. It does not allow other processes take place (in same script) however. A better way is to use an event which will create an event on timeout.

         

  • Use the aqUtils. Delay method to delay the script execution for a period of time. You may want to delay the script execution when the application under test is performing some time-consuming operations (for example, when it is loading a database or connecting to a network computer).

    The sleep() function suspends (waits) execution of the current thread for a given number of seconds.

     

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqutils/delay.html#:~:text=Use%20the%20aqUtils.,connecting%20to%20a%20network%20computer).

     

    https://support.smartbear.com/testcomplete/docs/app-objects/common-tasks/waiting-process-or-window-activation.html#_ga=2.111164748.959454532.1598443439-581598863.1589353078

    • MartinMet's avatar
      MartinMet
      New Contributor

      abishekhari007 Why would the aqUtils.Delay() cause a deadlock in writing to the file whereas time.sleep() does not then? Is it because the writing is supposed to happen during the delay and aqUtils.Delay() just pauses the entire script execution, including the interrupting class, whereas time.sleep() allows for it to run?