2
0

test_greenthreads.py 3.1 KB

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