Skip to content

Commit 45f9b18

Browse files
committed
New Test for CleanSession
1 parent a84b623 commit 45f9b18

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

fvt_client_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,76 @@ func Test_Will(t *testing.T) {
220220
wsub.Disconnect(250)
221221
}
222222

223+
func Test_CleanSession(t *testing.T) {
224+
clsnc := make(chan string, 1)
225+
226+
sops := NewClientOptions().AddBroker(FVTTCP)
227+
sops.SetClientID("clsn-sender")
228+
sops.SetConnectionLostHandler(func(client Client, err error) {
229+
fmt.Println("OnConnectionLost!")
230+
})
231+
sops.SetAutoReconnect(false)
232+
c := NewClient(sops).(*client)
233+
234+
wops := NewClientOptions()
235+
wops.AddBroker(FVTTCP)
236+
wops.SetClientID("clsn-tester")
237+
wops.SetCleanSession(false)
238+
wops.SetDefaultPublishHandler(func(client Client, msg Message) {
239+
fmt.Printf("TOPIC: %s\n", msg.Topic())
240+
fmt.Printf("MSG: %s\n", msg.Payload())
241+
clsnc <- string(msg.Payload())
242+
})
243+
wops.SetAutoReconnect(false)
244+
wsub := NewClient(wops)
245+
246+
if wToken := wsub.Connect(); wToken.Wait() && wToken.Error() != nil {
247+
t.Fatalf("Error on Client.Connect(): %v", wToken.Error())
248+
}
249+
250+
if wsubToken := wsub.Subscribe("clean", 1, nil); wsubToken.Wait() && wsubToken.Error() != nil {
251+
t.Fatalf("Error on Client.Subscribe(): %v", wsubToken.Error())
252+
}
253+
254+
wsub.Disconnect(250)
255+
time.Sleep(2 * time.Second)
256+
257+
if token := c.Connect(); token.Wait() && token.Error() != nil {
258+
t.Fatalf("Error on Client.Connect(): %v", token.Error())
259+
}
260+
261+
if pToken := c.Publish("clean", 1, false, "clean!"); pToken.Wait() && pToken.Error() != nil {
262+
t.Fatalf("Error on Client.Publish(): %v", pToken.Error())
263+
}
264+
265+
c.Disconnect(250)
266+
267+
wsub = NewClient(wops)
268+
if wToken := wsub.Connect(); wToken.Wait() && wToken.Error() != nil {
269+
t.Fatalf("Error on Client.Connect(): %v", wToken.Error())
270+
}
271+
272+
select {
273+
case msg := <-clsnc:
274+
if msg != "clean!" {
275+
t.Fatalf("will message did not have correct payload")
276+
}
277+
case <-time.NewTicker(5 * time.Second).C:
278+
t.Fatalf("failed to receive publish")
279+
}
280+
281+
wsub.Disconnect(250)
282+
283+
wops.SetCleanSession(true)
284+
285+
wsub = NewClient(wops)
286+
if wToken := wsub.Connect(); wToken.Wait() && wToken.Error() != nil {
287+
t.Fatalf("Error on Client.Connect(): %v", wToken.Error())
288+
}
289+
290+
wsub.Disconnect(250)
291+
}
292+
223293
func Test_Binary_Will(t *testing.T) {
224294
willmsgc := make(chan []byte, 1)
225295
will := []byte{

0 commit comments

Comments
 (0)