test_greenthreads.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.tests import (
  24. skipIf,
  25. TestCase,
  26. )
  27. from dulwich.object_store import (
  28. MemoryObjectStore,
  29. )
  30. from dulwich.objects import (
  31. Commit,
  32. Blob,
  33. Tree,
  34. parse_timezone,
  35. )
  36. try:
  37. import gevent # noqa: F401
  38. gevent_support = True
  39. except ImportError:
  40. gevent_support = False
  41. if gevent_support:
  42. from dulwich.greenthreads import (
  43. GreenThreadsMissingObjectFinder,
  44. )
  45. skipmsg = "Gevent library is not installed"
  46. def create_commit(marker=None):
  47. blob = Blob.from_string(b"The blob content " + marker)
  48. tree = Tree()
  49. tree.add(b"thefile " + marker, 0o100644, blob.id)
  50. cmt = Commit()
  51. cmt.tree = tree.id
  52. cmt.author = cmt.committer = b"John Doe <john@doe.net>"
  53. cmt.message = marker
  54. tz = parse_timezone(b"-0200")[0]
  55. cmt.commit_time = cmt.author_time = int(time.time())
  56. cmt.commit_timezone = cmt.author_timezone = tz
  57. return cmt, tree, blob
  58. def init_store(store, count=1):
  59. ret = []
  60. for i in range(0, count):
  61. objs = create_commit(marker=("%d" % i).encode("ascii"))
  62. for obj in objs:
  63. ret.append(obj)
  64. store.add_object(obj)
  65. return ret
  66. @skipIf(not gevent_support, skipmsg)
  67. class TestGreenThreadsMissingObjectFinder(TestCase):
  68. def setUp(self):
  69. super().setUp()
  70. self.store = MemoryObjectStore()
  71. self.cmt_amount = 10
  72. self.objs = init_store(self.store, self.cmt_amount)
  73. def test_finder(self):
  74. wants = [sha.id for sha in self.objs if isinstance(sha, Commit)]
  75. finder = GreenThreadsMissingObjectFinder(self.store, (), wants)
  76. self.assertEqual(len(finder.sha_done), 0)
  77. self.assertEqual(len(finder.objects_to_send), self.cmt_amount)
  78. finder = GreenThreadsMissingObjectFinder(
  79. self.store, wants[0 : int(self.cmt_amount / 2)], wants
  80. )
  81. # sha_done will contains commit id and sha of blob referred in tree
  82. self.assertEqual(len(finder.sha_done), (self.cmt_amount / 2) * 2)
  83. self.assertEqual(len(finder.objects_to_send), self.cmt_amount / 2)