-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_test.go
More file actions
159 lines (121 loc) · 3.51 KB
/
start_test.go
File metadata and controls
159 lines (121 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//go:build !windows
// +build !windows
package rcmd_test
import (
"context"
"testing"
"github.com/metrumresearchgroup/wrapt"
"github.com/metrumresearchgroup/rcmd/v2"
)
func Test_example(tt *testing.T) {
t := wrapt.WrapT(tt)
cmd, err := rcmd.New(context.Background(), "", "--quiet", "-e", "2+2")
t.R.NoError(err)
co, err := cmd.CombinedOutput()
t.R.NoError(err)
t.R.Equal("> 2+2\n[1] 4\n> \n> \n", string(co))
}
/*
func Test_RunR_example(tt *testing.T) {
t := wrapt.WrapT(tt)
rs, err := NewRSettings("R")
t.A.NoError(err)
p, n, err := rs.RunR(context.Background(), New(WithPrefix("foo")), "", "-e", "{2+2}")
t.A.NoError(err)
t.A.Equal(0, n)
_, err = io.ReadAll(p.Stdout)
t.A.NoError(err)
}
func Test_startR_example2(tt *testing.T) {
t := wrapt.WrapT(tt)
rs, err := NewRSettings("R")
t.A.NoError(err)
i, err := rs.StartR(context.Background(), New(), "", "-e", "2+2", "slave")
t.A.NoError(err)
defer fmt.Println(i.Wait())
}
func Test_runR_expression(tt *testing.T) {
t := wrapt.WrapT(tt)
rs, err := NewRSettings("R")
t.A.NoError(err)
ps, err := rs.RunRWithOutput(context.Background(), New(), "", "-e", "2+2", "--slave")
t.A.NoError(err)
lines, err := rp.ScanLines(ps)
t.A.NoError(err)
fmt.Println(strings.Join(lines, "\n"))
}
func Test_runRWithOutput_example(tt *testing.T) {
t := wrapt.WrapT(tt)
rs, err := NewRSettings("R")
t.A.NoError(err)
bs, err := rs.RunRWithOutput(context.Background(), New(), "", "-e", "2+2", "--slave", "--interactive")
t.A.NoError(err)
fmt.Println(string(bs))
fmt.Println("----with prefix----")
rs, err = NewRSettings("R")
t.A.NoError(err)
bs, err = rs.RunRWithOutput(context.Background(), New(), "", "-e", "2+2", "--slave", "--interactive")
t.A.NoError(err)
fmt.Println(string(bs))
}
func Test_runR_exampleTimeout(tt *testing.T) {
t := wrapt.WrapT(tt)
rs, err := NewRSettings("R")
t.A.NoError(err)
ctx, ccl := context.WithTimeout(context.Background(), 1*time.Second)
defer ccl()
_, res, err := rs.RunR(ctx, New(), "", "-e", "Sys.sleep(1.5); 2+2", "--slave", "--interactive")
t.A.Error(err)
fmt.Println(res)
}
func Test_runR_exampleCancel(tt *testing.T) {
t := wrapt.WrapT(tt)
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
// fire off 3 go routines where the 2nd fails, and all existing should stop processing
wg.Add(3)
go func() {
defer wg.Done()
rs, err := NewRSettings("R")
t.A.NoError(err)
i, err := rs.StartR(ctx, New(), "", "-e", "2+2", "--slave", "--interactive")
t.A.NoError(err)
defer fmt.Println(i.Wait())
res, err := io.ReadAll(i.Pipes().Stdout)
t.A.NoError(err)
fmt.Println("go routine 1: ", res)
}()
go func() {
defer wg.Done()
rs, err := NewRSettings("R")
t.A.NoError(err)
i, err := rs.StartR(ctx, New(), "", "-e", "Sys.sleep(0.5); stop('failed')", "--slave", "--interactive")
t.A.NoError(err)
defer fmt.Println(i.Wait())
if err != nil {
log.Error("goroutine 2 error:", err)
log.Warn("cancelling ongoing work...")
cancel()
}
res, err := io.ReadAll(i.Pipes().Stdout)
t.A.NoError(err)
fmt.Println("go routine 2 res: ", res)
}()
go func() {
defer wg.Done()
rs, err := NewRSettings("R")
t.A.NoError(err)
i, err := rs.StartR(ctx, New(), "", "-e", "Sys.sleep(1); 2+2", "--slave", "--interactive")
t.A.NoError(err)
defer fmt.Println(i.Wait())
if err != nil {
log.Error("goroutine 3 error:", err)
}
res, err := io.ReadAll(i.Pipes().Stdout)
t.A.NoError(err)
fmt.Println("go routine 3 res: ", res)
}()
wg.Wait()
fmt.Println("completed everything....")
}
*/