Skip to content

Commit

Permalink
add call router example
Browse files Browse the repository at this point in the history
  • Loading branch information
name5566 committed Dec 11, 2014
1 parent 63299bb commit 11c2a46
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions util/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util_test
import (
"fmt"
"github.com/name5566/leaf/util"
"sync"
)

func ExampleMap() {
Expand All @@ -26,3 +27,40 @@ func ExampleMap() {
// <nil>
// 3
}

func ExampleCallRouter() {
r := util.NewCallRouter(10)
var wg sync.WaitGroup

// goroutine 1
wg.Add(1)
go func() {
// def
r.Def("add", func(args []interface{}) interface{} {
num1 := args[0].(int)
num2 := args[1].(int)
return num1 + num2
})

// route
ci := <-r.Chan()
r.Route(ci)

wg.Done()
}()

// goroutine 2
wg.Add(1)
go func() {
// call
c := r.Call1("add", 1, 2)
fmt.Println(<-c)

wg.Done()
}()

wg.Wait()

// Output:
// 3
}

0 comments on commit 11c2a46

Please sign in to comment.