Added non-blocking message passing.
Added non-blocking message passing.

package org.example.shared.concurrency

import groovyx.gpars.actor.Actors
import org.example.shared.BaseUnitTest

import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

/**
 * Unit test of actor-based concurrency object.
 */
class ExampleActorUnitTest extends BaseUnitTest {

    def 'exercise one-way message passing'() {
        given: 'a couple of subjects under test'
        def depp = new HollywoodActor( name: 'Johnny Depp' )
        def hanks = new HollywoodActor( name: 'Tom Hanks' )

        and: 'the subjects are activated'
        [depp,hanks]*.start()

        when: 'messages are sent'
        depp.send( 'Sparrow')
        depp.send( 'Wonka')
        depp.send( 'Scissorhands')
        depp.send( 'Ceaser')

        hanks.send( 'Lovell' )

        and: 'we wait for a second'
        [depp,hanks]*.join( 1, TimeUnit.SECONDS )

        then: 'messages are printed to the screen'
        true
    }

    def 'exercise non-blocking multi-party message passing'() {

        given: 'a subject subject under test'
        def fortuneTeller = Actors.actor {
            loop {
                react { name ->
                    sender.send( "${name}, you have a very bright future!" )
                }
            }
        }

        when: 'multiple non-blocking messages are sent'
        def latch = new CountDownLatch( 2 )
        fortuneTeller.sendAndContinue( 'Bob' ) { println it; latch.countDown() }
        fortuneTeller.sendAndContinue( 'Fred' ) { println it; latch.countDown() }

        then: 'messages are printed to the screen'
        if ( !latch.await( 1, TimeUnit.SECONDS ) ) {
            println 'Fortune teller did not response in time'
        }
        else {
            println 'Bob and Fred are happy campers.'
        }
    }
}

package org.example.shared.concurrency

import org.example.shared.BaseUnitTest

import java.util.concurrent.TimeUnit

/**
 * Unit test of actor-based concurrency object.
 */
class ExampleActorUnitTest extends BaseUnitTest {

    def 'exercise message passing'() {
        given: 'a couple of subjects under test'
        def depp = new HollywoodActor( name: 'Johnny Depp' )
        def hanks = new HollywoodActor( name: 'Tom Hanks' )

        and: 'the subjects are activated'
        [depp,hanks]*.start()

        when: 'messages are sent'
        depp.send( 'Sparrow')
        depp.send( 'Wonka')
        depp.send( 'Scissorhands')
        depp.send( 'Ceaser')

        hanks.send( 'Lovell' )

        and: 'we wait for a second'
        [depp,hanks]*.join( 1, TimeUnit.SECONDS )

        then: 'messages are printed to the screen'
        true
    }

}
