-
Notifications
You must be signed in to change notification settings - Fork 460
/
rpc_client_test.go
118 lines (99 loc) · 2.26 KB
/
rpc_client_test.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugin
import (
"bytes"
"io"
"os"
"sync"
"testing"
"time"
hclog "github.com/hashicorp/go-hclog"
)
func TestClient_App(t *testing.T) {
pluginLogger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Output: os.Stderr,
JSONFormat: true,
})
testPlugin := &testInterfaceImpl{
logger: pluginLogger,
}
client, _ := TestPluginRPCConn(t, map[string]Plugin{
"test": &testInterfacePlugin{Impl: testPlugin},
}, nil)
defer client.Close()
raw, err := client.Dispense("test")
if err != nil {
t.Fatalf("err: %s", err)
}
impl, ok := raw.(testInterface)
if !ok {
t.Fatalf("bad: %#v", raw)
}
result := impl.Double(21)
if result != 42 {
t.Fatalf("bad: %#v", result)
}
}
func TestClient_syncStreams(t *testing.T) {
// Create streams for the server that we can talk to
stdout_r, stdout_w := io.Pipe()
stderr_r, stderr_w := io.Pipe()
client, _ := TestPluginRPCConn(t, map[string]Plugin{}, &TestOptions{
ServerStdout: stdout_r,
ServerStderr: stderr_r,
})
// Start the data copying
var stdout_out, stderr_out safeBuffer
stdout := &safeBuffer{
b: bytes.NewBufferString("stdouttest"),
}
stderr := &safeBuffer{
b: bytes.NewBufferString("stderrtest"),
}
go client.SyncStreams(&stdout_out, &stderr_out)
go io.Copy(stdout_w, stdout)
go io.Copy(stderr_w, stderr)
// Unfortunately I can't think of a better way to make sure all the
// copies above go through so let's just exit.
time.Sleep(100 * time.Millisecond)
// Close everything, and lets test the result
client.Close()
stdout_w.Close()
stderr_w.Close()
if v := stdout_out.String(); v != "stdouttest" {
t.Fatalf("bad: %q", v)
}
if v := stderr_out.String(); v != "stderrtest" {
t.Fatalf("bad: %q", v)
}
}
type safeBuffer struct {
sync.Mutex
b *bytes.Buffer
}
func (s *safeBuffer) Write(p []byte) (n int, err error) {
s.Lock()
defer s.Unlock()
if s.b == nil {
s.b = new(bytes.Buffer)
}
return s.b.Write(p)
}
func (s *safeBuffer) Read(p []byte) (n int, err error) {
s.Lock()
defer s.Unlock()
if s.b == nil {
s.b = new(bytes.Buffer)
}
return s.b.Read(p)
}
func (s *safeBuffer) String() string {
s.Lock()
defer s.Unlock()
if s.b == nil {
s.b = new(bytes.Buffer)
}
return s.b.String()
}