RegressionTests__PipeStreamTest.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 27 Mar 2014 17:26:13 +0100
changeset 1109 ad8866ade273
parent 1082 a46d9682d459
child 1110 a38d8f5208fd
permissions -rw-r--r--
class: RegressionTests::PipeStreamTest added: #testPipeWriteRead

"{ Package: 'exept:regression' }"

"{ NameSpace: RegressionTests }"

TestCase subclass:#PipeStreamTest
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'tests-Regression-Streams'
!


!PipeStreamTest methodsFor:'tests'!

testPipeWriteRead
    | makeReader makeWriter count threads ok blocker |

    count := 1000.
    threads := 200.
    ok := true.
    blocker := Semaphore new: threads negated + 1.
    makeReader := [ :stream | 
        [
            | c |
            c := 0.
            [ stream atEnd ] whileFalse:[ 
                stream next.
                c := c + 1.
            ].
            ok := ok and:[ count == c ].
            blocker signal.
        ].
    ].

    makeWriter := [ :stream | 
        [ 
            [ count timesRepeat:[stream nextPut: $P] ] on: Error do:[ 
                ok := false 
            ]
        ]
    ].

    threads timesRepeat:[
       | pipe rs ws|

        pipe := NonPositionableExternalStream makePipe.
        rs := pipe at:1.
        ws := pipe at:2.     

        (makeReader value: rs) fork.
        (makeWriter value: ws) fork.
    ].

    blocker wait.
    self assert: ok.

    "Created: / 27-03-2014 / 16:21:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

testReadCheckPipe4
    "readCheck a pipe.
     Do this and interrupt the reading thread heavily"

    | s p count nLoop|

    nLoop := 1000.

    "/ self createTestFile.

    s := PipeStream readingFrom:'sleep 5'.

    p := [
        s readWait.
        'readWait finished' printCR.
    ] forkAt:7.

    count := 0.
    [p isDead] whileFalse:[
        Delay waitForMilliseconds:5.
        p interruptWith:[count := count + 1].
    ].
    ('readWait interrupted <1p> times' expandMacrosWith:count) printCR.

    s close.

    "
     self new testReadCheckPipe4
    "

    "Modified: / 12.8.1998 / 13:42:13 / cg"
!

testReadCheckPipe5
    "readCheck a pipe.
     Do this and interrupt the reading thread heavily"

    | s p count nLoop|

    nLoop := 1000.

    "/ self createTestFile.

    s := PipeStream readingFrom:'sleep 5; echo hello'.

    p := [
        s readWait.
        'readWait finished' printCR.
    ] forkAt:7.

    count := 0.
    [p isDead] whileFalse:[
        Delay waitForMilliseconds:5.
        p interruptWith:[count := count + 1].
    ].
    ('readWait interrupted <1p> times' expandMacrosWith:count) printCR.

    s close.

    "
     self new testReadCheckPipe5
    "

    "Modified: / 12.8.1998 / 13:42:13 / cg"
!

testReadPipe6
    "read a pipe.
     Do this and interrupt the reading thread heavily"

    | s p count nLoop|

    nLoop := 1000.

    "/ self createTestFile.

    s := PipeStream readingFrom:'sleep 5'.

    p := [
        'read: ' print. s nextLine printCR.
    ] forkAt:7.

    count := 0.
    [p isDead] whileFalse:[
        Delay waitForMilliseconds:5.
        p interruptWith:[count := count + 1].
    ].
    ('read interrupted <1p> times' expandMacrosWith:count) printCR.

    s close.

    "
     self new testReadPipe6
    "

    "Modified: / 12.8.1998 / 13:42:13 / cg"
!

testReadPipe7
    "read a pipe.
     Do this and interrupt the reading thread heavily"

    |sz s p count nLoop|

    nLoop := 1000.

    "/ self createTestFile.

    s := PipeStream readingFrom:'sleep 5; echo hello'.

    p := [
        'read: ' print. s nextLine printCR.
    ] forkAt:7.

    count := 0.
    [p isDead] whileFalse:[
        Delay waitForMilliseconds:5.
        p interruptWith:[count := count + 1].
    ].
    ('read interrupted <1p> times' expandMacrosWith:count) printCR.

    s close.

    "
     self test7
    "

    "Modified: / 12.8.1998 / 13:42:13 / cg"
! !

!PipeStreamTest class methodsFor:'documentation'!

version
    ^ '$Header$'
! !