summaryrefslogtreecommitdiffstats
path: root/goctl_test.go
blob: 2c011b1b8bf6448fdf365337a6c776439d3dbc6b (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package goctl

import (
	"io/ioutil"
	"log"
	"net"
	"os"
	"reflect"
	"testing"
)

type testHandler struct {
	name string
	fn   func(*Goctl, []string) string
}

func makeHandler(name string, fn func(_ *Goctl, _ []string) string) testHandler {
	return testHandler{name: name, fn: fn}
}

func (th testHandler) Name() string {
	return th.name
}

func (th testHandler) Help() string {
	return ""
}

func (th testHandler) Run(gc *Goctl, args []string) string {
	return th.fn(gc, args)
}

var sockpath string

func init() {
	f, err := ioutil.TempFile("", "goctl-test")
	if err != nil {
		log.Fatalf("Couldn't create temporary file: %s.", err)
	}
	sockpath = f.Name()
	if err := os.Remove(sockpath); err != nil {
		log.Fatalf("Couldn't delete temporary file '%s': %s.", sockpath, err)
	}
}

func start(t testing.TB) *Goctl {
	gc := NewGoctl(sockpath)
	if err := gc.Start(); err != nil {
		t.Fatalf("Couldn't start: %s.", err)
	}
	return &gc
}

func dial(t testing.TB) net.Conn {
	c, err := net.Dial("unix", sockpath)
	if err != nil {
		t.Fatalf("Couldn't open %s: %s.", sockpath, err)
	}
	return c
}

func TestAlreadyRunning(t *testing.T) {
	gc := start(t)
	defer gc.Stop()

	bar := NewGoctl(sockpath)
	if bar.Start() == nil {
		t.Errorf("Started bot when already running.")
	}
	defer bar.Stop()
}

func TestSimulCommands(t *testing.T) {
	gc := start(t)
	defer gc.Stop()

	c0 := dial(t)
	defer c0.Close()

	c1 := dial(t)
	defer c1.Close()

	Write(c0, []byte("ping"))
	Write(c1, []byte("ping"))

	if buf, err := Read(c0); err != nil {
		t.Errorf("Couldn't read from first connection: %s.", err)
	} else if string(buf) != "pong" {
		t.Fatalf("Sending ping on first connection: got '%s', expected 'pong'.", string(buf))
	}
	if buf, err := Read(c1); err != nil {
		t.Fatalf("Couldn't read from second connection: %s.", err)
	} else if string(buf) != "pong" {
		t.Fatalf("Sending ping on second connection: got '%s', expected 'pong'.", string(buf))
	}
}

func TestAddHandlers(t *testing.T) {
	gc := start(t)
	defer gc.Stop()

	gc.AddHandlers(
		makeHandler("foo", func(_ *Goctl, args []string) string {
			if !reflect.DeepEqual(args, []string{"bar", "baz"}) {
				t.Errorf("Got %v, expected ['bar', 'baz']", args)
			}
			return ""
		}),
		makeHandler("bar", func(_ *Goctl, args []string) string {
			if !reflect.DeepEqual(args, []string{"baz", "pham"}) {
				t.Errorf("Got %v, expected ['baz', 'pham']", args)
			}
			return "wauug"
		}),
	)

	c := dial(t)
	defer c.Close()

	Write(c, []byte("foo\u0000bar\u0000baz"))
	Write(c, []byte("bar\u0000baz\u0000pham"))

	if buf, err := Read(c); err != nil {
		t.Errorf("Couldn't read from connection: %s.", err)
	} else if string(buf) != "" {
		t.Errorf("Got: %s, expected ''", string(buf))
	}

	if buf, err := Read(c); err != nil {
		t.Errorf("Couldn't read from connection: %s.", err)
	} else if string(buf) != "wauug" {
		t.Errorf("Got: %s, expected 'wauug'", string(buf))
	}
}

func TestCannotOverrideExtantHandlers(t *testing.T) {
	gc := start(t)
	defer gc.Stop()

	err := gc.AddHandlers(makeHandler("ping", func(_ *Goctl, args []string) string {
		return "gnip"
	}))
	if err != HandlerExists {
		t.Error("Was able to override built-in ping handler.")
	}
	err = gc.AddHandlers(
		makeHandler("foo", func(_ *Goctl, args []string) string { return "foo" }),
		makeHandler("foo", func(_ *Goctl, args []string) string { return "foo" }),
	)
	if err != HandlerExists {
		t.Error("Was able to assign two handlers for 'foo'.")
	}
}

func BenchmarkStartStop(b *testing.B) {
	gc := NewGoctl(sockpath)
	for i := 0; i < b.N; i++ {
		gc.Start()
		gc.Stop()
	}
}

func BenchmarkPing(b *testing.B) {
	gc := start(b)
	defer gc.Stop()

	c := dial(b)
	defer c.Close()

	for i := 0; i < b.N; i++ {
		Write(c, []byte("ping"))
		Read(c)
	}
}