|
| 1 | +import signal |
| 2 | +import threading |
1 | 3 | import unittest
|
| 4 | +import sys |
| 5 | + |
| 6 | +if sys.version_info > (3, 0): |
| 7 | + from queue import Queue |
| 8 | +else: |
| 9 | + from Queue import Queue |
2 | 10 |
|
3 | 11 | from django.conf import settings
|
4 | 12 | from django.template import Template, Context
|
@@ -67,6 +75,42 @@ def kids3_1(request):
|
67 | 75 |
|
68 | 76 | self.factory = RequestFactory()
|
69 | 77 |
|
| 78 | + def test_thread_safety_and_checks(self): |
| 79 | + """ |
| 80 | + Ensure our thread safety works, this also ensures our checks work |
| 81 | + """ |
| 82 | + # this shouldn't ever take more than 5 seconds, add a safety in case someting breaks |
| 83 | + signal.alarm(5) |
| 84 | + |
| 85 | + def t1(results): |
| 86 | + "Closure for thread 1" |
| 87 | + request = self.factory.get('/kids2-2/visible') |
| 88 | + items = Menu.process(request, 'test') |
| 89 | + results.put_nowait(len(items[0].children) == 2) |
| 90 | + |
| 91 | + def t2(results): |
| 92 | + "Closure for thread 2" |
| 93 | + request = self.factory.get('/kids2-2/hidden') |
| 94 | + items = Menu.process(request, 'test') |
| 95 | + results.put_nowait(len(items[0].children) == 1) |
| 96 | + |
| 97 | + results = Queue() |
| 98 | + for _ in range(50): |
| 99 | + threads = [ |
| 100 | + threading.Thread(target=t1, args=(results,)), |
| 101 | + threading.Thread(target=t2, args=(results,)) |
| 102 | + ] |
| 103 | + for thread in threads: |
| 104 | + thread.start() |
| 105 | + for thread in threads: |
| 106 | + thread.join() |
| 107 | + |
| 108 | + |
| 109 | + self.assertTrue(all([ |
| 110 | + results.get() |
| 111 | + for _ in range(100) |
| 112 | + ])) |
| 113 | + |
70 | 114 | def test_slug(self):
|
71 | 115 | """
|
72 | 116 | Ensure our slugification works as expected
|
@@ -99,18 +143,6 @@ def test_callable_title(self):
|
99 | 143 | items = Menu.process(request, 'test')
|
100 | 144 | self.assertEqual(items[1].children[1].title, "/parent3-fun")
|
101 | 145 |
|
102 |
| - def test_checks(self): |
103 |
| - """ |
104 |
| - Ensure checks on menus work |
105 |
| - """ |
106 |
| - request = self.factory.get('/kids2-2/visible') |
107 |
| - items = Menu.process(request, 'test') |
108 |
| - self.assertEqual(len(items[0].children), 2) |
109 |
| - |
110 |
| - request = self.factory.get('/kids2-2/hidden') |
111 |
| - items = Menu.process(request, 'test') |
112 |
| - self.assertEqual(len(items[0].children), 1) |
113 |
| - |
114 | 146 | def test_select_parents(self):
|
115 | 147 | """
|
116 | 148 | Ensure the MENU_SELECT_PARENTS setting works
|
|
0 commit comments