45 Star 552 Fork 169

联犀/物联网iot模块

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
assert.go 718 Bytes
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2023-02-27 22:17 +08:00 . doc:修改test文件夹为tests避免冲突
package mqttTest
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"time"
)
type AssertInfo struct {
SubTopic string
PubTopic string
Req []byte
}
func Assert(mc mqtt.Client, info AssertInfo, assertFunc func(resp []byte) error) error {
subChan := make(chan []byte)
go mc.Subscribe(info.SubTopic, 1, func(client mqtt.Client, message mqtt.Message) {
subChan <- message.Payload()
}).WaitTimeout(3 * time.Second)
time.Sleep(time.Second)
err := mc.Publish(info.PubTopic, 1, false, info.Req).Error()
if err != nil {
return err
}
for {
select {
case respC := <-subChan:
return assertFunc(respC)
case <-time.After(5 * time.Second):
return fmt.Errorf("subscribe time out")
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/unitedrhino/things.git
git@gitee.com:unitedrhino/things.git
unitedrhino
things
物联网iot模块
v1.5.73

搜索帮助