@@ -43,16 +43,6 @@ func (ms *messageStore) get(topic string) ([]byte, bool) {
4343 return v , ok
4444}
4545
46- func (ms * messageStore ) topics () []string {
47- ms .mu .Lock ()
48- defer ms .mu .Unlock ()
49- topics := make ([]string , 0 , len (ms .messages ))
50- for t := range ms .messages {
51- topics = append (topics , t )
52- }
53- return topics
54- }
55-
5646// startTestBroker starts an embedded mochi-mqtt broker on a random port.
5747// Returns the broker, its TCP address, and a message store that captures all published messages.
5848func startTestBroker (t * testing.T ) (* mqtt.Server , string , * messageStore ) {
@@ -79,7 +69,8 @@ func TestMQTTRESTSensorParity(t *testing.T) {
7969 _ , addr , store := startTestBroker (t )
8070
8171 // Set up mock system with known values
82- sys := newTestSystem (t ) // SOC=85, PV=600+440, Load=500
72+ sys := newTestSystem (t ) // SOC=85, PV=600+440, Load=500
73+ unitInfos := sys .Units () // capture before hub starts to avoid race
8374 hub := NewHub (sys , 50 * time .Millisecond , 50 * time .Millisecond )
8475
8576 ctx , cancel := context .WithCancel (context .Background ())
@@ -93,7 +84,6 @@ func TestMQTTRESTSensorParity(t *testing.T) {
9384 ClientID : "test-publisher" ,
9485 TopicPrefix : "homeassistant" ,
9586 }
96- unitInfos := sys .Units ()
9787 pub , err := NewMQTTPublisher (mqttCfg , hub , unitInfos )
9888 require .NoError (t , err )
9989 go pub .Run (ctx )
@@ -178,6 +168,7 @@ func TestMQTTRESTControlParity(t *testing.T) {
178168 _ , addr , store := startTestBroker (t )
179169
180170 sys := newTestSystem (t )
171+ unitInfos := sys .Units ()
181172 hub := NewHub (sys , 50 * time .Millisecond , 50 * time .Millisecond )
182173
183174 ctx , cancel := context .WithCancel (context .Background ())
@@ -190,7 +181,7 @@ func TestMQTTRESTControlParity(t *testing.T) {
190181 ClientID : "test-publisher" ,
191182 TopicPrefix : "homeassistant" ,
192183 }
193- pub , err := NewMQTTPublisher (mqttCfg , hub , sys . Units () )
184+ pub , err := NewMQTTPublisher (mqttCfg , hub , unitInfos )
194185 require .NoError (t , err )
195186 go pub .Run (ctx )
196187 time .Sleep (300 * time .Millisecond )
@@ -328,6 +319,7 @@ func TestMQTTControlRoundTrip(t *testing.T) {
328319 broker , addr , store := startTestBroker (t )
329320
330321 sys := newTestSystem (t )
322+ unitInfos := sys .Units ()
331323 hub := NewHub (sys , 50 * time .Millisecond , 50 * time .Millisecond )
332324
333325 ctx , cancel := context .WithCancel (context .Background ())
@@ -340,7 +332,7 @@ func TestMQTTControlRoundTrip(t *testing.T) {
340332 ClientID : "test-publisher" ,
341333 TopicPrefix : "homeassistant" ,
342334 }
343- pub , err := NewMQTTPublisher (mqttCfg , hub , sys . Units () )
335+ pub , err := NewMQTTPublisher (mqttCfg , hub , unitInfos )
344336 require .NoError (t , err )
345337 go pub .Run (ctx )
346338 time .Sleep (300 * time .Millisecond )
0 commit comments