简体   繁体   English

如果测试先决条件失败,完全退出 locust 进程的最干净的方法是什么?

[英]What is the cleanest to completely exit a locust process if test prerequisites fail?

I have an 'on_test_start' hook that tests for the existence of some environmental variables before the test starts:我有一个“on_test_start”钩子,可以在测试开始之前测试一些环境变量的存在:

@events.test_start.add_listener
def on_test_start(**_kwargs):
    env_vars_present = check_vars.check_existence_of_env_vars(["VAR1", "VAR2"])
    if not env_vars_present:
        sys.exit()

This works, but I don't like using sys.exit() which is a rather dirty way to quit a process.这行得通,但我不喜欢使用sys.exit()这是退出进程的一种相当肮脏的方式。 Is there a better alternative?有更好的选择吗? I've tried raising an exception, but this doesn't stop the runners.我试过提出一个例外,但这并不能阻止跑步者。 I've also tried environment.runner.quit() .我也试过environment.runner.quit() This quits the runners, but locust process continues until run-time limit is reached.这会退出跑步者,但蝗虫进程会继续,直到达到运行时间限制。 What's the cleanest way to exit the process if some prerequisite checks fail?如果某些先决条件检查失败,退出该过程的最干净的方法是什么? Thanks!谢谢!

You have the right idea with environment.runner.quit() you just need to have that happen on the master, not the workers.你对environment.runner.quit()有正确的想法,你只需要在主人身上发生,而不是工人。 You need the workers to communicate the failure to the master and then the master can can quit, which will stop the master and all the workers.您需要工人将故障传达给主人,然后主人可以退出,这将停止主人和所有工人。

You could either use other Event Hooks like request with a custom Name you could check a failure for, report_to_master to add in some extra data to the report payload sent to the master that you could check for its existence and quit on.您可以使用其他事件挂钩,例如带有自定义名称的请求,您可以检查失败, report_to_master将一些额外的数据添加到发送给主服务器的报告有效负载中,您可以检查其是否存在并退出。 Both of those you'd use worker_report on the master to trigger your check and quit.您将在 master 上使用 worker_report 来触发您的检查并退出的这两个。 Doing it this way could help you get these steps in any test output data so you could clearly see what the problem was without doing much custom reporting work.这样做可以帮助您在任何测试 output 数据中获得这些步骤,这样您就可以清楚地看到问题所在,而无需进行大量自定义报告工作。

Along the same lines but maybe slightly cleaner and more direct would be to use the communication between nodes to have the workers send a message directly to the master and have the master quit when that message was received.沿着相同的思路,但可能稍微更简洁和更直接的是使用节点之间的通信让工作人员直接向主服务器发送消息,并在收到该消息时让主服务器退出。 Code would look something like this:代码看起来像这样:

@events.init.add_listener
def quit_env_failure_message(environment, **_kwargs):
    global ENV
    ENV
    if isinstance(environment.runner, MasterRunner):
        environment.runner.register_message('env_failure', quit_on_env_failure)


def quit_on_env_failure(msg, **kwargs):
    ENV.runner.quit()


@events.test_start.add_listener
def on_test_start(**_kwargs):
    env_vars_present = check_vars.check_existence_of_env_vars(["VAR1", "VAR2"])
    if not env_vars_present:
        environment.runner.send_message('env_failure')

Doing it this way, you may need to do extra work to get whatever kind of tests result reporting you'd want.这样做,您可能需要做额外的工作才能获得您想要的任何类型的测试结果报告。 That would most likely go in quit_on_env_failure() .那很可能是quit_on_env_failure()中的 go 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM