factor/extra/concurrency/messaging/messaging-tests.factor

93 lines
2.3 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2005 Chris Double. All Rights Reserved.
! See http://factorcode.org/license.txt for BSD license.
!
2008-02-18 08:30:16 -05:00
USING: kernel threads vectors arrays sequences
2007-09-20 18:09:08 -04:00
namespaces tools.test continuations dlists strings math words
match quotations concurrency.messaging ;
2007-09-20 18:09:08 -04:00
IN: temporary
[ ] [ mailbox mailbox-data dlist-delete-all ] unit-test
2008-02-06 20:23:39 -05:00
2007-09-20 18:09:08 -04:00
[ V{ 1 2 3 } ] [
0 <vector>
<mailbox>
[ mailbox-get swap push ] in-thread
[ mailbox-get swap push ] in-thread
[ mailbox-get swap push ] in-thread
1 over mailbox-put
2 over mailbox-put
3 swap mailbox-put
2007-09-20 18:09:08 -04:00
] unit-test
[ V{ 1 2 3 } ] [
0 <vector>
<mailbox>
[ [ integer? ] swap mailbox-get? swap push ] in-thread
[ [ integer? ] swap mailbox-get? swap push ] in-thread
[ [ integer? ] swap mailbox-get? swap push ] in-thread
1 over mailbox-put
2 over mailbox-put
3 swap mailbox-put
2007-09-20 18:09:08 -04:00
] unit-test
[ V{ 1 "junk" 3 "junk2" } [ 456 ] ] [
0 <vector>
<mailbox>
[ [ integer? ] swap mailbox-get? swap push ] in-thread
[ [ integer? ] swap mailbox-get? swap push ] in-thread
[ [ string? ] swap mailbox-get? swap push ] in-thread
[ [ string? ] swap mailbox-get? swap push ] in-thread
1 over mailbox-put
"junk" over mailbox-put
[ 456 ] over mailbox-put
3 over mailbox-put
"junk2" over mailbox-put
mailbox-get
2007-09-20 18:09:08 -04:00
] unit-test
[ "received" ] [
[
receive "received" swap reply-synchronous
] "Synchronous test" spawn
"sent" swap send-synchronous
2007-09-20 18:09:08 -04:00
] unit-test
[ 1 3 2 ] [
1 self send
2 self send
3 self send
receive
[ 2 mod 0 = not ] receive-if
receive
2007-09-20 18:09:08 -04:00
] unit-test
[
[
"crash" throw
] "Linked test" spawn-linked drop
receive
] [ linked-error "crash" = ] must-fail-with
2007-09-20 18:09:08 -04:00
MATCH-VARS: ?from ?to ?value ;
2007-09-20 18:09:08 -04:00
SYMBOL: increment
SYMBOL: decrement
SYMBOL: value
SYMBOL: exit
2007-09-20 18:09:08 -04:00
: counter ( value -- value ? )
receive {
{ { increment ?value } [ ?value + t ] }
{ { decrement ?value } [ ?value - t ] }
{ { value ?from } [ dup ?from send t ] }
{ exit [ f ] }
} match-cond ;
2007-09-20 18:09:08 -04:00
[ -5 ] [
[ 0 [ counter ] [ ] [ ] while ] "Counter" spawn "counter" set
{ increment 10 } "counter" get send
{ decrement 15 } "counter" get send
[ value , self , ] { } make "counter" get send
receive
exit "counter" get send
2008-02-08 22:47:35 -05:00
] unit-test