forked from filinvadim/go-cloud-pubsub-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_test.go
200 lines (168 loc) · 4.79 KB
/
mqtt_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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2019 The Go Cloud Development Kit Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mqttpubsub //nolint:testpackage
import (
"context"
"fmt"
"net"
"strconv"
"testing"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"gocloud.dev/pubsub"
"gocloud.dev/pubsub/driver"
"gocloud.dev/pubsub/drivertest"
)
const (
publicTestHost = "test.mosquitto.org" // officially for testing
localTestHost = "localhost"
testPort = 1883
)
type harness struct {
Subscriber
Publisher
}
func newSubPub(_ context.Context) (*harness, error) {
url := fmt.Sprintf("%s:%d", localTestHost, testPort)
if !isMQTTListens() {
url = fmt.Sprintf("%s:%d", publicTestHost, testPort)
}
sub, err := defaultSubClient(url)
if err != nil {
return nil, err
}
pub, err := defaultPubClient(url)
if err != nil {
return nil, err
}
return &harness{sub, pub}, nil
}
func newHarness(ctx context.Context, _ *testing.T) (drivertest.Harness, error) {
return newSubPub(ctx)
}
func (h *harness) CreateTopic(_ context.Context, testName string) (driver.Topic, func(), error) {
cleanup := func() {}
dt, err := openTopic(h.Publisher, testName)
if err != nil {
return nil, nil, err
}
return dt, cleanup, nil
}
func (h *harness) MakeNonexistentTopic(_ context.Context) (driver.Topic, error) {
// A nil *topic behaves like a nonexistent topic.
return (*topic)(nil), nil
}
func (h *harness) CreateSubscription(_ context.Context, _ driver.Topic, testName string) (driver.Subscription, func(), error) {
ds, err := openSubscription(h.Subscriber, testName, &SubscriptionOptions{WaitTime: 10 * time.Millisecond})
if err != nil {
return nil, nil, err
}
cleanup := func() {
var sub Subscriber
if ds.As(&sub) {
_ = h.Subscriber.Unsubscribe(testName)
}
}
return ds, cleanup, nil
}
func (h *harness) MakeNonexistentSubscription(_ context.Context) (driver.Subscription, func(), error) {
return (*subscription)(nil), func() {}, nil
}
func (h *harness) Close() {
_ = h.Publisher.Stop()
_ = h.Subscriber.Close()
}
func (*harness) MaxBatchSizes() (int, int) { return 0, 0 }
// supported from MQTT v5.0. Not supported by "github.com/eclipse/paho.mqtt.golang" driver.
func (*harness) SupportsMultipleSubscriptions() bool { return false }
type mqttAsTest struct{}
func (mqttAsTest) Name() string {
return "mqtt test"
}
func (mqttAsTest) TopicCheck(topic *pubsub.Topic) error {
var pub *Publisher
if topic.As(&pub) {
return fmt.Errorf("cast succeeded for %T, want failure", &pub)
}
var pub2 Publisher
if !topic.As(&pub2) {
return fmt.Errorf("cast failed for %T", &pub2)
}
return nil
}
func (mqttAsTest) SubscriptionCheck(sub *pubsub.Subscription) error {
var sub1 *Subscriber
if sub.As(&sub1) {
return fmt.Errorf("cast succeeded for %T, want failure", &sub1)
}
var sub2 Subscriber
if !sub.As(&sub2) {
return fmt.Errorf("cast failed for %T", &sub2)
}
return nil
}
func (mqttAsTest) TopicErrorCheck(t *pubsub.Topic, err error) error {
var dummy string
if t.ErrorAs(err, &dummy) {
return fmt.Errorf("cast succeeded for %T, want failure", &dummy)
}
return nil
}
func (mqttAsTest) SubscriptionErrorCheck(s *pubsub.Subscription, err error) error {
var dummy string
if s.ErrorAs(err, &dummy) {
return fmt.Errorf("cast succeeded for %T, want failure", &dummy)
}
return nil
}
func (mqttAsTest) MessageCheck(m *pubsub.Message) error {
var pm *mqtt.Message
if m.As(&pm) {
return fmt.Errorf("cast succeeded for %T, want failure", &pm)
}
var ppm mqtt.Message
if !m.As(&ppm) {
return fmt.Errorf("cast failed for %T", &ppm)
}
return nil
}
func (mqttAsTest) AfterSend(_ func(interface{}) bool) error {
return nil
}
func (mqttAsTest) BeforeSend(_ func(interface{}) bool) error {
return nil
}
func TestConformance(t *testing.T) {
asTests := []drivertest.AsTest{mqttAsTest{}}
drivertest.RunConformanceTests(t, newHarness, asTests)
}
func BenchmarkBench(b *testing.B) {
hs, err := newSubPub(context.TODO())
if err != nil {
b.Fatal(err)
}
sub, err := OpenSubscription(hs.Subscriber, "__1", &SubscriptionOptions{10 * time.Millisecond})
if err != nil {
b.Fatal(err)
}
pub, err := OpenTopic(hs.Publisher, "__1", nil)
if err != nil {
b.Fatal(err)
}
drivertest.RunBenchmarks(b, pub, sub)
}
func isMQTTListens() bool {
_, err := net.Dial("tcp", localTestHost+":"+strconv.Itoa(testPort))
return err == nil
}