From 2394682a4db7a97830dc10750e59fce0e09347dc Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Nov 21 2018 18:48:36 +0000 Subject: [PATCH 1/2] remove shebang in context module --- diff --git a/koji/context.py b/koji/context.py index 3bcff4e..ed23f22 100755 --- a/koji/context.py +++ b/koji/context.py @@ -1,4 +1,3 @@ -#!/usr/bin/python2 # Copyright (c) 2005-2014 Red Hat, Inc. # # Koji is free software; you can redistribute it and/or From cab5a01dcbd53d18778263b18b41e1a8aa1443c7 Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Nov 22 2018 10:43:31 +0000 Subject: [PATCH 2/2] move tests to test_lib/test_context.py --- diff --git a/koji/context.py b/koji/context.py index ed23f22..274ef33 100755 --- a/koji/context.py +++ b/koji/context.py @@ -25,7 +25,6 @@ from __future__ import absolute_import import six.moves._thread -from six.moves import range import six class _data(object): @@ -79,36 +78,3 @@ class ThreadLocal(object): context = ThreadLocal() - - -if __name__ == '__main__': - - #testing - - #context.foo = 1 - #context.bar = 2 - print(context) - #del context.bar - print(context) - - import random - import time - def test(): - context.foo = random.random() - time.sleep(1.5+random.random()) - context._threadclear() - print(context) - - for x in range(1, 10): - six.moves._thread.start_new_thread(test, ()) - - time.sleep(4) - print('') - print(context) - - context.foo = 1 - context.bar = 2 - print(context.foo, context.bar) - print(context) - context._threadclear() - print(context) diff --git a/tests/test_lib/test_context.py b/tests/test_lib/test_context.py new file mode 100644 index 0000000..92d19c0 --- /dev/null +++ b/tests/test_lib/test_context.py @@ -0,0 +1,41 @@ +from __future__ import absolute_import +import six +import time +import random +try: + import unittest2 as unittest +except ImportError: + import unittest + +from koji.context import context + + +class TestContext(unittest.TestCase): + + def test_context(self): + context.foo = 1 + + def test(): + foo = random.random() + context.foo = foo + time.sleep(0.5 + random.random()) + print(context) + self.assertEqual(context.foo, foo) + context._threadclear() + self.assertFalse(hasattr(context, 'foo')) + + for x in range(1, 10): + six.moves._thread.start_new_thread(test, ()) + + time.sleep(0.5) + for i in range(10): + time.sleep(0.2 + random.random()) + self.assertEqual(context.foo, 1) + + context.foo = 2 + context.bar = 3 + self.assertEqual(context.foo, 2) + self.assertEqual(context.bar, 3) + context._threadclear() + self.assertFalse(hasattr(context, 'foo')) + self.assertFalse(hasattr(context, 'bar'))