Skip to content

Commit

Permalink
Merge pull request locustio#32 from pancaprima/prima/fix_reread_confi…
Browse files Browse the repository at this point in the history
…guration

Prima fix re-read configuration if defined in task
  • Loading branch information
pancaprima authored Nov 29, 2017
2 parents e017da6 + 681d22d commit bba14e6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
61 changes: 43 additions & 18 deletions locust/configuration.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
import os, json, logging

logger = logging.getLogger(__name__)
config_path = '/tests/settings/config.json'

def read_file():
"""
Will read the file and return it as a string with tree view.
"""
try:
with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + '/tests/settings/config.json', "r") as data_file:
datas = data_file.read()
with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + config_path, "r") as data_file:
data = data_file.read()
data_file.close()
return datas
except Exception as err:
logger.info(err)
return "{}"
data = "{}"
return data

def write_file(stringJSON):
def write_file(string_json):
"""
The `string_json` will overwrite existing configuration.
If the previous configuration doesn't exist, then it will create the file.
"""
status, message = None, None
try:
with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + '/tests/settings/config.json', "w") as data_file:
datas = data_file.write(stringJSON)
return True, 'JSON saved'
with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + config_path, "w") as data_file:
data_file.write(string_json)
status = True
message = 'Configuration has been saved'
except Exception as err:
logger.info(err)
return False, err
status = False
message = "Can't save the configuration :" + err
return status, message

class ClientConfiguration:
"""
This class is a handler for data configuration with JSON data structure.
"""

config_data = None

def read_json(self):
"""
Will get the data of configuration as JSON.
It reads configuration file once.
"""
if self.config_data is None:
try:
with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + config_path, "r") as data_file:
self.config_data = json.load(data_file)
data_file.close()
except Exception as err:
logger.info(err)
self.config_data = json.load({})
return self.config_data

def read_JSON():
try:
with open((os.environ['PYTHONPATH'].split(os.pathsep))[-1] + '/tests/settings/config.json', "r") as data_file:
datas = json.load(data_file)
data_file.close()
return datas
except Exception as err:
logger.info(err)
return "{}"
11 changes: 8 additions & 3 deletions locust/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import logging

from .clients import HttpSession
from . import events, configuration
from . import events
from .configuration import ClientConfiguration

from .exception import LocustError, InterruptTaskSet, RescheduleTask, RescheduleTaskImmediately, StopLocust

Expand Down Expand Up @@ -223,6 +224,9 @@ class ForumPage(TaskSet):
instantiated. Useful for nested TaskSet classes.
"""

config = None
"""Will refer to the ClientConfiguration class instance when the TaskSet has been instantiated"""

def __init__(self, parent):
self._task_queue = []
self._time_start = time()
Expand All @@ -235,7 +239,8 @@ def __init__(self, parent):
raise LocustError("TaskSet should be called with Locust instance or TaskSet instance as first argument")

self.parent = parent

self.config = ClientConfiguration()

# if this class doesn't have a min_wait or max_wait defined, copy it from Locust
if not self.min_wait:
self.min_wait = self.locust.min_wait
Expand Down Expand Up @@ -358,5 +363,5 @@ def configuration(self):
"""
Reference to configuration.py
"""
return configuration.read_JSON()
return self.config.read_json()

1 change: 0 additions & 1 deletion locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
app.root_path = os.path.dirname(os.path.abspath(__file__))
_ramp = False
greenlet_spawner = None
load_config=""

@app.route('/')
def index():
Expand Down

0 comments on commit bba14e6

Please sign in to comment.