Question

I have written a custom test harness in Python (existing stuff was not a good fit due to lots of custom logic). Windows task scheduler kicks it off once per hour every day. As my tests now take more than 2 hours to run and are growing, I am running into problems. Right now I just check the system time and do nothing unless hour % 3 == 0, but I do not like that. I have a text file that contains:

# This is a comment
LatestTestedBuild = 25100

# Blank lines are skipped too
LatestTestRunStartedDate = 2011_03_26_00:01:21

# This indicates that it has not finished yet.
LatestTestRunFinishDate = 

Sometimes, when I kick off a test manually, it can happen at any time, including 12:59:59.99 I want to remove race conditions as much as possible. I would rather put some extra effort once and not worry about practical probability of something happening. So, I think locking a this text file atomically is the best approach.

I am using Python 2.7, Windows Server 2008R2 Pro and Windows 7 Pro. I prefer not to install extra libraries (Python has not been "sold" to my co-workers yet, but I could copy over a file locally that implements it all, granted that the license permits it).

So, please suggest a good, bullet-proof way to solve this.

Was it helpful?

Solution

When you start running a test make a file called __LOCK__ or something. Delete it when you finish, using a try...finally block to ensure that it always gets cleared up. Don't run the test if the file exists. If the computer crashes or similar, delete the file by hand. I doubt you need more cleverness than that.

Are you sure you need 2 hours of tests?! I think 2 minutes is a more reasonable amount of time to spend, though I guess if you are running some complicated numerics you might need more.

example code:

import os
if os.path.exists("__LOCK__"):
    raise RuntimeError("Already running.") # or whatever
try:
    open("__LOCK__", "w").write("Put some info here if you want.")
finally:
    if os.path.exists("__LOCK__"):
        os.unlink("__LOCK__")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top