gevent_test.bzl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright 2021 The gRPC Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Houses py_grpc_gevent_test.
  16. """
  17. load("@grpc_python_dependencies//:requirements.bzl", "requirement")
  18. _GRPC_LIB = "//src/python/grpcio/grpc:grpcio"
  19. _COPIED_MAIN_SUFFIX = ".gevent.main"
  20. def py_grpc_gevent_test(
  21. name,
  22. srcs,
  23. main = None,
  24. deps = None,
  25. data = None,
  26. **kwargs):
  27. """Runs a Python test with gevent monkeypatched in.
  28. Args:
  29. name: The name of the test.
  30. srcs: The source files.
  31. main: The main file of the test.
  32. deps: The dependencies of the test.
  33. data: The data dependencies of the test.
  34. **kwargs: Any other test arguments.
  35. """
  36. if main == None:
  37. if len(srcs) != 1:
  38. fail("When main is not provided, srcs must be of size 1.")
  39. main = srcs[0]
  40. deps = [] if deps == None else deps
  41. data = [] if data == None else data
  42. lib_name = name + ".gevent.lib"
  43. supplied_python_version = kwargs.pop("python_version", "")
  44. if supplied_python_version and supplied_python_version != "PY3":
  45. fail("py_grpc_gevent_test only supports python_version=PY3")
  46. native.py_library(
  47. name = lib_name,
  48. srcs = srcs,
  49. )
  50. augmented_deps = deps + [
  51. ":{}".format(lib_name),
  52. requirement("gevent"),
  53. ]
  54. if _GRPC_LIB not in augmented_deps:
  55. augmented_deps.append(_GRPC_LIB)
  56. # The main file needs to be in the same package as the test file.
  57. copied_main_name = name + _COPIED_MAIN_SUFFIX
  58. copied_main_filename = copied_main_name + ".py"
  59. native.genrule(
  60. name = copied_main_name,
  61. srcs = ["//bazel:_gevent_test_main.py"],
  62. outs = [copied_main_filename],
  63. cmd = "cp $< $@",
  64. )
  65. # TODO(https://github.com/grpc/grpc/issues/27542): Remove once gevent is deemed non-flaky.
  66. if "flaky" in kwargs:
  67. kwargs.pop("flaky")
  68. native.py_test(
  69. name = name + ".gevent",
  70. args = [name],
  71. deps = augmented_deps,
  72. srcs = [copied_main_filename],
  73. main = copied_main_filename,
  74. python_version = "PY3",
  75. flaky = False,
  76. **kwargs
  77. )