generated from streamingriver/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_test.go
83 lines (71 loc) · 1.84 KB
/
app_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
package main
import (
"bytes"
"log"
"os"
"strings"
"testing"
"time"
)
func TestApp(t *testing.T) {
app := new(App)
os.Setenv("SC_NATS_URL", "nats-server")
os.Setenv("SC_NATS_TOPIC", "nats-topic")
os.Setenv("SC_PARSER_TYPE", "cache")
os.Setenv("SC_CONFIG_PATH", "etc")
os.Setenv("SC_CONFIG_EXT", ".conf")
app.init()
if app.natsTopic != "nats-topic" {
t.Errorf("Wrong nats topic %s", app.natsTopic)
}
if app.natsURL != "nats-server" {
t.Errorf("Wrong nats server %s", app.natsURL)
}
if app.parserType != "cache" {
t.Errorf("Wrong parser type %s", app.parserType)
}
if app.path != "etc" {
t.Errorf("Wrong parser type %s", app.path)
}
if app.ext != ".conf" {
t.Errorf("Wrong parser type %s", app.ext)
}
}
type LogWriter struct {
t *testing.T
buff *bytes.Buffer
}
func (mw LogWriter) Write(b []byte) (int, error) {
mw.buff.Write(b)
return len(b), nil
}
func TestConnectToNatsShouldFail(t *testing.T) {
os.Setenv("SC_NATS_URL", "127.0.0.1:4567")
os.Setenv("SC_NATS_TOPIC", "nats-topic")
os.Setenv("SC_PARSER_TYPE", "cache")
writer := &LogWriter{t, bytes.NewBuffer(nil)}
app := new(App)
app.init()
app.testing = true
app.logger = log.New(writer, "", 0)
app.ConnectToNats()
time.Sleep(time.Second)
if strings.Trim(writer.buff.String(), "\n") != "nats: no servers available for connection" {
t.Errorf("ConnectToNats %s", writer.buff.Bytes())
}
}
func TestNatsSubscribeShouldFail(t *testing.T) {
os.Setenv("SC_NATS_URL", "127.0.0.1:4567")
os.Setenv("SC_NATS_TOPIC", "nats-topic")
os.Setenv("SC_PARSER_TYPE", "cache")
writer := &LogWriter{t, bytes.NewBuffer(nil)}
app := new(App)
app.testing = true
app.init()
app.logger = log.New(writer, "", 0)
app.Subscribe()
time.Sleep(time.Second)
if strings.Trim(writer.buff.String(), "\n") != "nats: invalid connection" {
t.Errorf("Subscribe %s", writer.buff.Bytes())
}
}