| 137 | |
| 138 | |
| 139 | def test_threadsafe_singleton(): |
| 140 | class ShouldBeSingle(six.with_metaclass(func.Singleton, object)): |
| 141 | def __new__(cls, *args, **kwargs): |
| 142 | time.sleep(0.1) |
| 143 | return super(ShouldBeSingle, cls).__new__(cls, *args, **kwargs) |
| 144 | |
| 145 | threads_count = 100 |
| 146 | threads = [None] * threads_count |
| 147 | results = [None] * threads_count |
| 148 | |
| 149 | def class_factory(results, i): |
| 150 | time.sleep(0.1) |
| 151 | results[i] = ShouldBeSingle() |
| 152 | |
| 153 | for i in range(threads_count): |
| 154 | threads[i] = threading.Thread(target=class_factory, args=(results, i)) |
| 155 | |
| 156 | for i in range(threads_count): |
| 157 | threads[i].start() |
| 158 | |
| 159 | for i in range(threads_count): |
| 160 | threads[i].join() |
| 161 | |
| 162 | assert len(set(results)) == 1 |
| 163 | |
| 164 | |
| 165 | def test_memoize_thread_local(): |