This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
72 lines (60 loc) · 1.99 KB
/
main_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
package main
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/b4ckspace/spacestatus/server"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/google/go-cmp/cmp"
log "github.com/sirupsen/logrus"
)
func TestMqtt(t *testing.T) {
s, err := server.NewServer()
if err != nil {
log.Fatal(err)
}
m := mqtt.NewClient(&mqtt.ClientOptions{
Servers: []*url.URL{s.MqttURL},
ClientID: "go-mqtt-spacestatus-test",
AutoReconnect: true,
OnConnect: func(c mqtt.Client) { log.Info("connected") },
OnConnectionLost: func(c mqtt.Client, err error) { log.Errorf("connection lost: %v", err) },
})
token := m.Connect()
log.Infof("%+v", token)
go main()
<-time.After(1 * time.Second)
_ = m.Publish("sensor/space/member/names", 0, false, "a, b, c, d")
_ = m.Publish("sensor/space/member/present", 0, false, "4")
_ = m.Publish("sensor/space/member/count", 0, false, "30")
_ = m.Publish("sensor/temperature/hackcenter/shelf", 0, false, "21.3")
_ = m.Publish("sensor/power/main/L1", 0, false, "123")
_ = m.Publish("sensor/power/main/L2", 0, false, "234")
_ = m.Publish("sensor/power/main/L3", 0, false, "345")
_ = m.Publish("sensor/power/main/total", 0, false, "1234")
_ = m.Publish("sensor/space/status", 0, false, "closed")
_ = m.Publish("sensor/space/member/deviceCount", 0, false, "77")
_ = m.Publish("sensor/radiation/cpm", 0, false, "42")
_ = m.Publish("sensor/radiation/uSv", 0, false, "0.23")
<-time.After(1 * time.Second)
resp, err := http.Get("http://localhost:8080/")
if err != nil {
t.Errorf("Unable to query API: %v", err)
}
have, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Unable to load body: %v", err)
}
want, err := ioutil.ReadFile("testdata/status.json")
if err != nil {
t.Errorf("Unable to load want: %v", err)
}
wants := strings.Split(string(want), "\n")
haves := strings.Split(string(have), "\n")
if diff := cmp.Diff(wants, haves); diff != "" {
t.Errorf("Invalid output. \n%s", diff)
}
}