Use XDG_* environment variables even on Windows
[zeroinstall.git] / tests / testschedular.py
blobedd2050398e956e243b0fc3fe26e8e27f72f232a
1 #!/usr/bin/env python
2 import sys
3 from basetest import BaseTest
4 import unittest
6 sys.path.insert(0, '..')
8 from zeroinstall.injector import scheduler
9 from zeroinstall.support import tasks
11 real_spawn = scheduler._spawn_thread
13 class DummyDownload:
14 def __init__(self, url, downloads):
15 self._aborted = tasks.Blocker("abort " + url)
16 self.url = url
17 self.downloads = downloads
19 def _finish(self, status):
20 self.downloads[self.url] = status
22 class TestSchedular(BaseTest):
23 def setUp(self):
24 BaseTest.setUp(self)
26 def tearDown(self):
27 BaseTest.tearDown(self)
28 scheduler._spawn_thread = real_spawn
30 def testQueuing(self):
31 #import logging; logging.getLogger().setLevel(logging.DEBUG)
33 site = scheduler.Site()
35 steps = []
37 downloads = {}
39 @tasks.async
40 def dummy_spawn_thread(step):
41 resume = tasks.Blocker('complete ' + step.url)
42 downloads[step.url] = resume
43 yield resume
44 try:
45 tasks.check(resume)
46 except Exception as ex:
47 step.status = "fail"
48 else:
49 step.status = "ok"
51 scheduler._spawn_thread = dummy_spawn_thread
52 scheduler.MAX_DOWNLOADS_PER_SITE = 2
54 for i in range(4):
55 dl = DummyDownload("http://step/" + str(i), downloads)
57 s = scheduler.DownloadStep()
58 s.url = dl.url
59 s.dl = dl
60 steps.append(site.download(s))
62 @tasks.async
63 def collect():
64 # Let the first two downloads start
65 for x in range(10): yield
66 self.assertEqual(2, len(downloads))
68 # Let one of them complete
69 url, blocker = downloads.items()[0]
70 blocker.trigger()
71 for x in range(10): yield
72 self.assertEqual(3, len(downloads))
74 # Check it was successful
75 self.assertEqual("ok", downloads[url])
76 del downloads[url]
78 # Let the next one fail
79 url, blocker = downloads.items()[0]
80 blocker.trigger(exception = (Exception("test"), None))
81 for x in range(10): yield
82 self.assertEqual(3, len(downloads))
84 # Check it failed
85 self.assertEqual("fail", downloads[url])
86 del downloads[url]
88 # The last two should both be in progress now.
89 # Allow them both to finish.
90 blockers = downloads.values()
91 blockers[0].trigger()
92 blockers[1].trigger()
93 for x in range(10): yield
94 results = downloads.values()
95 self.assertEqual(["ok", "ok"], results)
97 tasks.wait_for_blocker(collect())
99 if __name__ == '__main__':
100 unittest.main()