Skip to content

Commit

Permalink
fix mqtt test occasionally fail (#428)
Browse files Browse the repository at this point in the history
* trun on log for mqtt_test and add more information for test failed

* fix mqtt test fail bug
  • Loading branch information
suchen-sci committed Dec 23, 2021
1 parent f627a47 commit 8ae49a6
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/object/mqttproxy/mqtt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func (t *testMQ) get() *packets.PublishPacket {
}

func init() {
logger.InitNop()
// logger.InitMock()
logger.InitMock()
pipeline.Register(&pipeline.MockMQTTFilter{})
}

Expand Down Expand Up @@ -1853,6 +1852,18 @@ func TestHTTPGetAllSession(t *testing.T) {
clients = append(clients, client)
}

// we use goroutine to store session, make sure all sessions have been stored before we go forward.
for i := 0; i < 20; i++ {
sessions, _ := broker.sessMgr.store.getPrefix(sessionStoreKey(""), true)
if len(sessions) == clientNum {
break
}
time.Sleep(50 * time.Millisecond)
if i == 19 && len(sessions) != clientNum {
t.Fatalf("not all sessions have been stored %v", sessions)
}
}

// start server
srv := newServer(":8888")
srv.addHandlerFunc("/session/query", broker.httpGetAllSessionHandler)
Expand Down Expand Up @@ -1895,6 +1906,10 @@ func TestHTTPGetAllSession(t *testing.T) {
json.NewDecoder(resp.Body).Decode(sessions)
if len(sessions.Sessions) != test.ansLen {
t.Errorf("get wrong session number wanted %v, got %v", test.ansLen, len(sessions.Sessions))
sessions, _ := broker.sessMgr.store.getPrefix(sessionStoreKey(""), true)
broker.Lock()
t.Errorf("broker clients %v, sessions %v", broker.clients, sessions)
broker.Unlock()
}
}
resp.Body.Close()
Expand Down

0 comments on commit 8ae49a6

Please sign in to comment.