-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathversion_message_test.go
More file actions
80 lines (67 loc) · 1.78 KB
/
version_message_test.go
File metadata and controls
80 lines (67 loc) · 1.78 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
// Tests error handling and support of various version ('V') messages.
//
// Also serves as an example on how to use indirections provided by
// pg_logplexcollector to do testing.
package main
import (
"bytes"
"errors"
"testing"
"github.com/deafbybeheading/femebe/buf"
"github.com/deafbybeheading/femebe/core"
)
var versionCheckTests = []struct {
Version string
Ok bool
}{
{"PG-7.4.15/1", false},
{"PG-7.4.15/logfebe-1", false},
{"PG-9.2.2/logfebe-1", true},
{"PG-9.2alpha1/logfebe-1", true},
{"PG-9.3.0/logfebe-1", true},
{"PG-9.3beta2/logfebe-1", true},
{"PG-9.4.0/logfebe-1", true},
{"PG-9.4devel/logfebe-1", true},
{"PG7.4.15/1", false},
}
func TestVersionCheck(t *testing.T) {
for i, tt := range versionCheckTests {
msgInit := func(dst *core.Message, exit exitFn) {
b := bytes.Buffer{}
buf.WriteCString(&b, tt.Version)
dst.InitFromBytes('V', b.Bytes())
}
ok := true
onBadVersion := func(args ...interface{}) {
ok = false
}
processVerMsg(msgInit, onBadVersion)
if ok != tt.Ok {
t.Errorf("%d: Ver Message well formed: %v; want %v",
i, ok, tt.Ok)
}
}
}
func TestVersionMsgInitErr(t *testing.T) {
theErr := errors.New("An error; e.g. network difficulties")
msgInit := func(dst *core.Message, exit exitFn) {
exit(theErr)
}
sentinel := &msgInit
exit := func(args ...interface{}) {
// Since the error instance raised is injected, test that it
// is precisely the error propagated to the exitFn.
if args[0] != theErr {
t.Fatal("Error propagated, but not the expected one")
}
panic(sentinel)
}
defer func() {
if r := recover(); r != nil && r != sentinel {
t.Fatal("Paniced, but not with the sentinel value")
}
// Success
}()
processVerMsg(msgInit, exit)
t.Fatal("Message should call exit, aborting execution before this")
}