Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 5 | package modules_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "os" |
| 11 | |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 12 | "v.io/x/ref/test" |
| 13 | "v.io/x/ref/test/modules" |
Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | func init() { |
| 17 | modules.RegisterChild("echo", "<args>...", echo) |
| 18 | } |
| 19 | |
| 20 | func echo(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
| 21 | for i, a := range args { |
| 22 | fmt.Fprintf(stdout, "%d: %s\n", i, a) |
| 23 | } |
| 24 | return nil |
| 25 | } |
| 26 | |
| 27 | func ExampleDispatch() { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 28 | ctx, shutdown := test.InitForTest() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 29 | defer shutdown() |
Cosmos Nicolaou | 42a1736 | 2015-03-10 16:40:18 -0700 | [diff] [blame] | 30 | if modules.IsModulesChildProcess() { |
Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 31 | // Child process. Dispatch will invoke the 'echo' command |
| 32 | if err := modules.Dispatch(); err != nil { |
| 33 | panic(fmt.Sprintf("unexpected error: %s", err)) |
| 34 | } |
| 35 | return |
| 36 | } |
| 37 | // Parent process. |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 38 | sh, _ := modules.NewShell(ctx, nil, false, nil) |
Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 39 | defer sh.Cleanup(nil, nil) |
| 40 | h, _ := sh.Start("echo", nil, "a", "b") |
| 41 | h.Shutdown(os.Stdout, os.Stderr) |
| 42 | // Output: |
Suharsh Sivakumar | 9d17e4a | 2015-02-02 22:42:16 -0800 | [diff] [blame] | 43 | // 0: a |
| 44 | // 1: b |
Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func ExampleDispatchAndExit() { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 48 | ctx, shutdown := test.InitForTest() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 49 | defer shutdown() |
Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 50 | // DispatchAndExit will call os.Exit(0) when executed within the child. |
| 51 | modules.DispatchAndExit() |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 52 | sh, _ := modules.NewShell(ctx, nil, false, nil) |
Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 53 | defer sh.Cleanup(nil, nil) |
| 54 | h, _ := sh.Start("echo", nil, "c", "d") |
| 55 | h.Shutdown(os.Stdout, os.Stderr) |
| 56 | // Output: |
Suharsh Sivakumar | 9d17e4a | 2015-02-02 22:42:16 -0800 | [diff] [blame] | 57 | // 0: c |
| 58 | // 1: d |
Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 59 | } |