blob: 2247b7a33fe73e2019c796819181029e4f1e9977 [file] [log] [blame]
Cosmos Nicolaou90610bd2014-12-02 22:31:04 -08001package modules_test
2
3import (
4 "fmt"
5 "io"
6 "os"
7
8 "veyron.io/veyron/veyron/lib/modules"
9)
10
11func init() {
12 modules.RegisterChild("echo", "<args>...", echo)
13}
14
15func echo(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
16 for i, a := range args {
17 fmt.Fprintf(stdout, "%d: %s\n", i, a)
18 }
19 return nil
20}
21
22func ExampleDispatch() {
23 if modules.IsModulesProcess() {
24 // Child process. Dispatch will invoke the 'echo' command
25 if err := modules.Dispatch(); err != nil {
26 panic(fmt.Sprintf("unexpected error: %s", err))
27 }
28 return
29 }
30 // Parent process.
31 sh, _ := modules.NewShell(nil)
32 defer sh.Cleanup(nil, nil)
33 h, _ := sh.Start("echo", nil, "a", "b")
34 h.Shutdown(os.Stdout, os.Stderr)
35 // Output:
36 // 0: echo
37 // 1: a
38 // 2: b
39}
40
41func ExampleDispatchAndExit() {
42 // DispatchAndExit will call os.Exit(0) when executed within the child.
43 modules.DispatchAndExit()
44 sh, _ := modules.NewShell(nil)
45 defer sh.Cleanup(nil, nil)
46 h, _ := sh.Start("echo", nil, "c", "d")
47 h.Shutdown(os.Stdout, os.Stderr)
48 // Output:
49 // 0: echo
50 // 1: c
51 // 2: d
52}