1# test concurrent interning of strings
2#
3# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
4
5try:
6    import utime as time
7except ImportError:
8    import time
9import _thread
10
11# function to check the interned string
12def check(s, val):
13    assert type(s) == str
14    assert int(s) == val
15
16
17# main thread function
18def th(base, n):
19    for i in range(n):
20        # this will intern the string and check it
21        exec("check('%u', %u)" % (base + i, base + i))
22
23    with lock:
24        global n_finished
25        n_finished += 1
26
27
28lock = _thread.allocate_lock()
29n_thread = 4
30n_finished = 0
31n_qstr_per_thread = 100  # make 1000 for a more stressful test (uses more heap)
32
33# spawn threads
34for i in range(n_thread):
35    _thread.start_new_thread(th, (i * n_qstr_per_thread, n_qstr_per_thread))
36
37# wait for threads to finish
38while n_finished < n_thread:
39    time.sleep(1)
40
41print("pass")
42