test_command.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from hystrix.command import Command
  2. import pytest
  3. class HelloCommand(Command):
  4. def run(self):
  5. return 'Hello Run'
  6. class FallbackCommand(Command):
  7. def run(self):
  8. raise RuntimeError('This command always fails')
  9. def fallback(self):
  10. return 'Hello Fallback'
  11. class CacheCommand(Command):
  12. def run(self):
  13. raise RuntimeError('This command always fails')
  14. def fallback(self):
  15. raise RuntimeError('This command always fails')
  16. def cache(self):
  17. return 'Hello Cache'
  18. def test_not_implemented_error():
  19. class NotImplementedCommand(Command):
  20. pass
  21. command = NotImplementedCommand()
  22. with pytest.raises(RuntimeError):
  23. command.run()
  24. with pytest.raises(RuntimeError):
  25. command.fallback()
  26. with pytest.raises(RuntimeError):
  27. command.cache()
  28. def test_default_groupname():
  29. class RunCommand(Command):
  30. pass
  31. command = RunCommand()
  32. assert command.group_key == 'RunCommandGroup'
  33. def test_manual_groupname():
  34. class RunCommand(Command):
  35. group_key = 'MyRunGroup'
  36. pass
  37. command = RunCommand()
  38. assert command.group_key == 'MyRunGroup'
  39. def test_command_hello_synchronous():
  40. command = HelloCommand()
  41. result = command.execute()
  42. assert 'Hello Run' == result
  43. def test_command_hello_asynchronous():
  44. command = HelloCommand()
  45. future = command.queue()
  46. assert 'Hello Run' == future.result()
  47. def test_command_hello_callback():
  48. command = HelloCommand()
  49. future = command.observe()
  50. assert 'Hello Run' == future.result()
  51. def test_command_hello_fallback_synchronous():
  52. command = FallbackCommand()
  53. result = command.execute()
  54. assert 'Hello Fallback' == result
  55. def test_command_hello_fallback_asynchronous():
  56. command = FallbackCommand()
  57. future = command.queue()
  58. assert 'Hello Fallback' == future.result()
  59. def test_command_hello_fallback_callback():
  60. command = FallbackCommand()
  61. future = command.observe()
  62. assert 'Hello Fallback' == future.result()
  63. def test_command_hello_cache_synchronous():
  64. command = CacheCommand()
  65. result = command.execute()
  66. assert 'Hello Cache' == result
  67. def test_command_hello_cache_asynchronous():
  68. command = CacheCommand()
  69. future = command.queue()
  70. assert 'Hello Cache' == future.result()
  71. def test_command_hello_cache_callback():
  72. command = CacheCommand()
  73. future = command.observe()
  74. assert 'Hello Cache' == future.result()