Skip to content

Instantly share code, notes, and snippets.

@elzibubble
Last active March 18, 2016 15:26
Show Gist options
  • Save elzibubble/166ae0dbcae534781871 to your computer and use it in GitHub Desktop.
Save elzibubble/166ae0dbcae534781871 to your computer and use it in GitHub Desktop.
testing for eventlet issues while reconfiguring logging
--- Init logging
--- Spawn
*** [0, 0] [0, 0]
--- Reloading - set EV
*** [0, 0] [0, 0]
*** [0, 0] [0, 0]
fail :(
*** [0, 0] [0, 0]
2016-03-18 15:26:51.697 27138 CRITICAL logxp [-] AssertionError
2016-03-18 15:26:51.697 27138 ERROR logxp Traceback (most recent call last):
2016-03-18 15:26:51.697 27138 ERROR logxp File "logxp/stress.py", line 97, in <module>
2016-03-18 15:26:51.697 27138 ERROR logxp main()
2016-03-18 15:26:51.697 27138 ERROR logxp File "logxp/stress.py", line 91, in main
2016-03-18 15:26:51.697 27138 ERROR logxp assert n_function[1] == THREADS
2016-03-18 15:26:51.697 27138 ERROR logxp AssertionError
2016-03-18 15:26:51.697 27138 ERROR logxp
[loggers]
keys: root,stress
[handlers]
keys: screen,stress
[formatters]
keys: context
[logger_root]
qualname: root
handlers: screen
level: NOTSET
[logger_stress]
qualname: stress
handlers: stress
propagate: 0
[handler_stress]
class: stress.StressHandler
args: ''
formatter: context
[handler_screen]
class=StreamHandler
formatter=context
level=INFO
args=(sys.stdout,)
# datefmt must be set otherwise you end up with too many (msecs) fields
[formatter_context]
class: oslo_log.formatters.ContextFormatter
args: (datefmt=datefmt)
format: %(isotime)s %(process)d %(levelname)s %(name)s %(message)s
datefmt: %Y-%m-%d %H:%M:%S
import eventlet
eventlet.monkey_patch()
import os
import logging
import time
import threading
import traceback
import oslo_config.cfg as cfg
from oslo_log import log as log
class Counter(object):
def __init__(self):
self.n = 0
def __str__(self):
return str(self.n)
def __repr__(self):
return repr(self.n)
def __eq__(self, x):
return self.n == x
def __iadd__(self, x):
self.n += x
return self
def is_set(self):
return bool(self.n)
def set(self):
self.n = 1
def clear(self):
self.n = 0
EV = threading.Event()
# EV = Counter()
THREADS = 10
def count(pair):
if not EV.is_set():
pair[0] += 1
else:
pair[1] += 1
if pair[0] == THREADS:
print "fail :("
class StressHandler(logging.Handler):
counts = [Counter(), Counter()]
def __init__(self, level=logging.NOTSET):
super(StressHandler, self).__init__(level)
def emit(self, record):
count(StressHandler.counts)
def main():
os.system('clear')
CONF = cfg.CONF
log.register_options(CONF)
CONF()
print "--- Init logging"
CONF.log_config_append = "stress.ini"
log.setup(CONF, 'logxp')
root = log.getLogger("")
stress = log.getLogger("stress")
print "\n--- Spawn"
n_function = [Counter(), Counter()]
for i in range(THREADS):
# eventlet.spawn(count, n_function)
eventlet.spawn(stress.error, '')
print "*** %s %s" % (n_function, StressHandler.counts)
print "\n--- Reloading - set EV"
EV.set()
print "*** %s %s" % (n_function, StressHandler.counts)
# logging.config.fileConfig('stress.ini', disable_existing_loggers=False)
print "*** %s %s" % (n_function, StressHandler.counts)
time.sleep(0)
print "*** %s %s" % (n_function, StressHandler.counts)
assert n_function[0] == 0
assert n_function[1] == THREADS
assert StressHandler.counts[0] == 0
assert StressHandler.counts[1] == THREADS
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment