Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError: Cannot choose from an empty sequence #803

Closed
jawahar273 opened this issue May 26, 2018 · 8 comments
Closed

IndexError: Cannot choose from an empty sequence #803

jawahar273 opened this issue May 26, 2018 · 8 comments

Comments

@jawahar273
Copy link

Description of issue / feature request

  File "/media/jawahar/jon1/new_project/bm/testDev/lib/python3.6/site-packages/locust/core.py", line 268, in run
    self.schedule_task(self.get_next_task())
  File "/media/jawahar/jon1/new_project/bm/testDev/lib/python3.6/site-packages/locust/core.py", line 329, in get_next_task
    return random.choice(self.tasks)
  File "/media/jawahar/jon1/new_project/bm/testDev/lib/python3.6/random.py", line 258, in choice
    raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence

Repeately i am getting this error after Start swarming number of users.

Expected behavior

No Error related to this.

Actual behavior

Traceback showed about

Environment settings (for bug reports)

  • OS:Ubuntu 17
  • Python version: 3.6.5
  • Locust version: 0.8.0/0.8.1

Steps to reproduce (for bug reports)

Run some api and you will get this error

@cgoldberg
Copy link
Member

cgoldberg commented May 26, 2018

IndexError: Cannot choose from an empty sequence

@jawahar273,
That exception gets raised when there are no tasks defined. Can you verify you actually declared some tasks in your locustfile? (either use the @tasks decorator or define a class that inherits from TaskSet)

for Locust devs,
A nice enhancement would be to catch the IndexError in /locust/core.py and raise a RuntimeError instead.. It should display an intuitive message like "No tasks defined in locustfile", and then exit.

@cgoldberg
Copy link
Member

ah.. we should check for this in code.

@jawahar273
Copy link
Author

jawahar273 commented May 27, 2018

import logging

from locust import TaskSet, task

logger = logging.getLogger(__name__)


class UserBehavior(TaskSet):

    def __init__(self, parent):
        super().__init__(parent=parent)

        self.token_key = "Token "

    def base_url(self):

        return "/api/"

    def packages_url(self):

        return self.base_url() + "package/"

    def user_details(self):

        return {"username": "demo", "password": "demobmmb"}

    def on_start(self):
        """ on_start is called
            when a Locust start before
            any task is scheduled
        """
        self.login()

    def login(self):
        # self.token_key = 'Token '
        logger.info("login user")
        response = self.client.post(
            self.base_url() + "rest-auth/login/", data=self.user_details()
        )
        self.token_key = self.token_key + response.text

        logger.debug("response from host" + str(response))
        print("Response status code:", response.status_code)
        print("Response content:", response.text)

    @task(1)
    def currency_details(self):
        currency = self.client.get(
            self.packages_url() + "currency/",
            headers={"authentication": self.token_key},
        )

        if currency.status_code >= 300:

            logger.debug("response from host for currency" + currency.text)

        else:

            logger.info("success in currency", currency.status_code)

This is the actual code.

@cgoldberg
Copy link
Member

what are the items in "Addtion feedback" about?

@jawahar273
Copy link
Author

Leave it.
Did you get the error that I was trying to show.

@cgoldberg
Copy link
Member

Did you get the error that I was trying to show

no

@jawahar273
Copy link
Author

Yes i got the error after removing the @task(1) and if you run the code with @task(1) there is no error in running the locust.

Is this a series issue or not.

@cgoldberg
Copy link
Member

if you write an invalid locustfile and don't declare tasks, expect things to break. As I said previously, the error message could be improved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants