test_greenthreads.py 3.0 KB

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