blob: f60b2bb2af4caf71a44ffb95185f3aec912d4db5 [file] [log] [blame]
Cosmos Nicolaou66afced2014-09-15 22:12:43 -07001package modules
2
3import (
4 "fmt"
5 "io"
6 "path/filepath"
7 "runtime"
8 "testing"
9)
10
11func init() {
Cosmos Nicolaou1e78ccc2014-10-09 08:10:26 -070012 RegisterChild("echos", "[args]*", Echo)
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070013}
14
15func Echo(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
16 if len(args) == 0 {
17 return fmt.Errorf("no args")
18 }
19 for _, a := range args {
20 fmt.Println(a)
21 }
22 return nil
23}
24
25func assertNumHandles(t *testing.T, sh *Shell, n int) {
26 if got, want := len(sh.handles), n; got != want {
Cosmos Nicolaou1e78ccc2014-10-09 08:10:26 -070027 _, file, line, _ := runtime.Caller(1)
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070028 t.Errorf("%s:%d: got %d, want %d", filepath.Base(file), line, got, want)
29 }
30}
31
32func TestState(t *testing.T) {
Cosmos Nicolaou1e78ccc2014-10-09 08:10:26 -070033 sh := NewShell("echos")
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070034 sh.AddSubprocess("echonotregistered", "[args]*")
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070035 sh.AddFunction("echof", Echo, "[args]*")
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070036 assertNumHandles(t, sh, 0)
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070037
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070038 _, _ = sh.Start("echonotregistered") // won't start.
39 hs, _ := sh.Start("echos", "a")
40 hf, _ := sh.Start("echof", "b")
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070041 assertNumHandles(t, sh, 2)
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070042
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070043 for i, h := range []Handle{hs, hf} {
Cosmos Nicolaoubbae3882014-10-02 22:58:19 -070044 if got := h.Shutdown(nil, nil); got != nil {
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070045 t.Errorf("%d: got %q, want %q", i, got, nil)
46 }
47 }
48 assertNumHandles(t, sh, 0)
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070049
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070050 hs, _ = sh.Start("echos", "a", "b")
51 hf, _ = sh.Start("echof", "c")
52 assertNumHandles(t, sh, 2)
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070053
Cosmos Nicolaoubbae3882014-10-02 22:58:19 -070054 sh.Cleanup(nil, nil)
Cosmos Nicolaou66afced2014-09-15 22:12:43 -070055 assertNumHandles(t, sh, 0)
56}