test_greenthreads.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # test_greenthreads.py -- Unittests for eventlet.
  2. # Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
  3. #
  4. # Author: Fabien Boucher <fabien.boucher@enovance.com>
  5. #
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as public by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. import time
  23. from dulwich.object_store import MemoryObjectStore
  24. from dulwich.objects import Blob, Commit, Tree, parse_timezone
  25. from . import TestCase, skipIf
  26. try:
  27. import gevent # noqa: F401
  28. gevent_support = True
  29. except ImportError:
  30. gevent_support = False
  31. if gevent_support:
  32. from dulwich.greenthreads import GreenThreadsMissingObjectFinder
  33. skipmsg = "Gevent library is not installed"
  34. def create_commit(marker=None):
  35. blob = Blob.from_string(b"The blob content " + marker)
  36. tree = Tree()
  37. tree.add(b"thefile " + marker, 0o100644, blob.id)
  38. cmt = Commit()
  39. cmt.tree = tree.id
  40. cmt.author = cmt.committer = b"John Doe <john@doe.net>"
  41. cmt.message = marker
  42. tz = parse_timezone(b"-0200")[0]
  43. cmt.commit_time = cmt.author_time = int(time.time())
  44. cmt.commit_timezone = cmt.author_timezone = tz
  45. return cmt, tree, blob
  46. def init_store(store, count=1):
  47. ret = []
  48. for i in range(count):
  49. objs = create_commit(marker=("%d" % i).encode("ascii"))
  50. for obj in objs:
  51. ret.append(obj)
  52. store.add_object(obj)
  53. return ret
  54. @skipIf(not gevent_support, skipmsg)
  55. class TestGreenThreadsMissingObjectFinder(TestCase):
  56. def setUp(self):
  57. super().setUp()
  58. self.store = MemoryObjectStore()
  59. self.cmt_amount = 10
  60. self.objs = init_store(self.store, self.cmt_amount)
  61. def test_finder(self):
  62. wants = [sha.id for sha in self.objs if isinstance(sha, Commit)]
  63. finder = GreenThreadsMissingObjectFinder(self.store, (), wants)
  64. self.assertEqual(len(finder.sha_done), 0)
  65. self.assertEqual(len(finder.objects_to_send), self.cmt_amount)
  66. finder = GreenThreadsMissingObjectFinder(
  67. self.store, wants[0 : int(self.cmt_amount / 2)], wants
  68. )
  69. # sha_done will contains commit id and sha of blob referred in tree
  70. self.assertEqual(len(finder.sha_done), (self.cmt_amount / 2) * 2)
  71. self.assertEqual(len(finder.objects_to_send), self.cmt_amount / 2)