blob: 7d7c466880151038670ad8edda71a34f78af4af1 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// 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
Jiri Simsa5293dcb2014-05-10 09:56:38 -07005package rt_test
6
7import (
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -07008 "bufio"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07009 "fmt"
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070010 "io"
11 "os"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070012 "syscall"
13 "testing"
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070014 "time"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070015
Suharsh Sivakumardcc11d72015-05-11 12:19:20 -070016 _ "v.io/x/ref/runtime/factories/generic"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070017 "v.io/x/ref/test"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070018 "v.io/x/ref/test/modules"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070019)
20
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070021func simpleEchoProgram(stdin io.Reader, stdout io.Writer) {
22 fmt.Fprintf(stdout, "ready\n")
23 scanner := bufio.NewScanner(stdin)
24 if scanner.Scan() {
25 fmt.Fprintf(stdout, "%s\n", scanner.Text())
26 }
27 modules.WaitForEOF(stdin)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070028}
29
Todd Wang95873902015-05-22 14:21:30 -070030var withRuntime = modules.Register(func(env *modules.Env, args ...string) error {
Todd Wang60052d82015-05-22 15:00:10 -070031 _, shutdown := test.V23Init()
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080032 defer shutdown()
33
Todd Wang95873902015-05-22 14:21:30 -070034 simpleEchoProgram(env.Stdin, env.Stdout)
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070035 return nil
Todd Wang95873902015-05-22 14:21:30 -070036}, "withRuntime")
Jiri Simsa5293dcb2014-05-10 09:56:38 -070037
Todd Wang95873902015-05-22 14:21:30 -070038var withoutRuntime = modules.Register(func(env *modules.Env, args ...string) error {
39 simpleEchoProgram(env.Stdin, env.Stdout)
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070040 return nil
Todd Wang95873902015-05-22 14:21:30 -070041}, "withoutRuntime")
Jiri Simsa5293dcb2014-05-10 09:56:38 -070042
43func TestWithRuntime(t *testing.T) {
Cosmos Nicolaou9e909842015-03-17 11:58:59 -070044 sh, err := modules.NewShell(nil, nil, testing.Verbose(), t)
Cosmos Nicolaou344cc4a2014-11-26 15:38:43 -080045 if err != nil {
46 t.Fatalf("unexpected error: %s", err)
47 }
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070048 defer sh.Cleanup(os.Stderr, os.Stderr)
Todd Wang95873902015-05-22 14:21:30 -070049 h, err := sh.Start(nil, withRuntime)
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070050 if err != nil {
51 t.Fatalf("unexpected error: %s", err)
52 }
53 defer h.Shutdown(os.Stderr, os.Stderr)
Cosmos Nicolaou9e909842015-03-17 11:58:59 -070054 h.Expect("ready")
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070055 syscall.Kill(h.Pid(), syscall.SIGHUP)
56 h.Stdin().Write([]byte("foo\n"))
Cosmos Nicolaou9e909842015-03-17 11:58:59 -070057 h.Expect("foo")
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070058 h.CloseStdin()
Cosmos Nicolaou9e909842015-03-17 11:58:59 -070059 h.ExpectEOF()
Jiri Simsa5293dcb2014-05-10 09:56:38 -070060}
61
62func TestWithoutRuntime(t *testing.T) {
Cosmos Nicolaou9e909842015-03-17 11:58:59 -070063 sh, err := modules.NewShell(nil, nil, testing.Verbose(), t)
Cosmos Nicolaou344cc4a2014-11-26 15:38:43 -080064 if err != nil {
65 t.Fatalf("unexpected error: %s", err)
66 }
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070067 defer sh.Cleanup(os.Stderr, os.Stderr)
Cosmos Nicolaou9e909842015-03-17 11:58:59 -070068 opts := sh.DefaultStartOpts()
69 opts.ShutdownTimeout = 5 * time.Second
Todd Wang95873902015-05-22 14:21:30 -070070 h, err := sh.StartWithOpts(opts, nil, withoutRuntime)
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070071 if err != nil {
72 t.Fatalf("unexpected error: %s", err)
73 }
74 defer h.Shutdown(os.Stderr, os.Stderr)
Cosmos Nicolaou9e909842015-03-17 11:58:59 -070075 h.Expect("ready")
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070076 syscall.Kill(h.Pid(), syscall.SIGHUP)
Cosmos Nicolaou9e909842015-03-17 11:58:59 -070077 h.ExpectEOF()
Cosmos Nicolaou59496fe2014-10-14 11:21:05 -070078 err = h.Shutdown(os.Stderr, os.Stderr)
79 want := "exit status 2"
80 if err == nil || err.Error() != want {
81 t.Errorf("got %s, want %s", err, want)
82
83 }
Jiri Simsa5293dcb2014-05-10 09:56:38 -070084}