1 # test_greenthreads.py -- Unittests for eventlet.
2 # Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
4 # Author: Fabien Boucher <fabien.boucher@enovance.com>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
9 # of the License or (at your option) any later version of
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 from dulwich.tests import (
28 from dulwich.object_store import (
32 from dulwich.objects import (
43 gevent_support = False
46 from dulwich.greenthreads import (
47 GreenThreadsObjectStoreIterator,
48 GreenThreadsMissingObjectFinder,
51 skipmsg = "Gevent library is not installed"
53 def create_commit(marker=None):
54 blob = Blob.from_string('The blob content %s' % marker)
56 tree.add("thefile %s" % marker, 0o100644, blob.id)
59 cmt.author = cmt.committer = "John Doe <john@doe.net>"
60 cmt.message = "%s" % marker
61 tz = parse_timezone('-0200')[0]
62 cmt.commit_time = cmt.author_time = int(time.time())
63 cmt.commit_timezone = cmt.author_timezone = tz
64 return cmt, tree, blob
67 def init_store(store, count=1):
69 for i in range(0, count):
70 objs = create_commit(marker=i)
77 @skipIf(not gevent_support, skipmsg)
78 class TestGreenThreadsObjectStoreIterator(TestCase):
81 super(TestGreenThreadsObjectStoreIterator, self).setUp()
82 self.store = MemoryObjectStore()
84 self.objs = init_store(self.store, self.cmt_amount)
87 wants = [sha.id for sha in self.objs if isinstance(sha, Commit)]
88 finder = MissingObjectFinder(self.store, (), wants)
89 iterator = GreenThreadsObjectStoreIterator(self.store,
90 iter(finder.next, None),
92 # One commit refers one tree and one blob
93 self.assertEqual(len(iterator), self.cmt_amount * 3)
94 haves = wants[0:self.cmt_amount-1]
95 finder = MissingObjectFinder(self.store, haves, wants)
96 iterator = GreenThreadsObjectStoreIterator(self.store,
97 iter(finder.next, None),
99 self.assertEqual(len(iterator), 3)
102 wants = [sha.id for sha in self.objs if isinstance(sha, Commit)]
103 finder = MissingObjectFinder(self.store, (), wants)
104 iterator = GreenThreadsObjectStoreIterator(self.store,
105 iter(finder.next, None),
108 for sha, path in iterator:
109 self.assertIn(sha, self.objs)
111 self.assertEqual(len(objs), len(self.objs))
114 @skipIf(not gevent_support, skipmsg)
115 class TestGreenThreadsMissingObjectFinder(TestCase):
118 super(TestGreenThreadsMissingObjectFinder, self).setUp()
119 self.store = MemoryObjectStore()
121 self.objs = init_store(self.store, self.cmt_amount)
123 def test_finder(self):
124 wants = [sha.id for sha in self.objs if isinstance(sha, Commit)]
125 finder = GreenThreadsMissingObjectFinder(self.store, (), wants)
126 self.assertEqual(len(finder.sha_done), 0)
127 self.assertEqual(len(finder.objects_to_send), self.cmt_amount)
129 finder = GreenThreadsMissingObjectFinder(self.store,
130 wants[0:self.cmt_amount/2],
132 # sha_done will contains commit id and sha of blob refered in tree
133 self.assertEqual(len(finder.sha_done), (self.cmt_amount/2)*2)
134 self.assertEqual(len(finder.objects_to_send), self.cmt_amount/2)