]> gitweb.factorcode.org Git - factor.git/blob - basis/threads/threads-tests.factor
742ecaa1f778ae5731c16455f637f06b817e2734
[factor.git] / basis / threads / threads-tests.factor
1 USING: namespaces io tools.test threads kernel
2 concurrency.combinators concurrency.promises locals math
3 words calendar sequences ;
4 IN: threads.tests
5
6 3 "x" set
7 [ 2 "x" set ] "Test" spawn drop
8 [ 2 ] [ yield "x" get ] unit-test
9 [ ] [ [ flush ] "flush test" spawn drop flush ] unit-test
10 [ ] [ [ "Errors, errors" throw ] "error test" spawn drop ] unit-test
11 yield
12
13 [ ] [ 0.3 sleep ] unit-test
14 [ "hey" sleep ] must-fail
15
16 [ 3 ] [ 3 self resume-with "Test suspend" suspend ] unit-test
17
18 [ f ] [ f get-global ] unit-test
19
20 { { 0 3 6 9 12 15 18 21 24 27 } } [
21     10 iota [
22         0 "i" tset
23         [
24             "i" [ yield 3 + ] tchange
25         ] times yield
26         "i" tget
27     ] parallel-map
28 ] unit-test
29
30 :: spawn-namespace-test ( -- ? )
31     <promise> :> p gensym :> g
32     [
33         g "x" set
34         [ "x" get p fulfill ] "B" spawn drop
35     ] with-scope
36     p ?promise g eq? ;
37
38 [ t ] [ spawn-namespace-test ] unit-test
39
40 [ "a" [ 1 1 + ] spawn 100 sleep ] must-fail
41
42 [ ] [ 0.1 seconds sleep ] unit-test
43
44 ! Test thread-local variables
45 <promise> "p" set
46
47 5 "x" tset
48
49 [ 5 ] [ "x" tget ] unit-test
50
51 [ ] [ "x" [ 1 + ] tchange ] unit-test
52
53 [ 6 ] [ "x" tget ] unit-test
54
55 ! Are they truly thread-local?
56 [ "x" tget "p" get fulfill ] in-thread
57
58 [ f ] [ "p" get ?promise ] unit-test