4
go.mod
4
go.mod
@@ -7,6 +7,7 @@ require (
|
|||||||
github.com/golang-jwt/jwt/v5 v5.0.0
|
github.com/golang-jwt/jwt/v5 v5.0.0
|
||||||
github.com/gorilla/websocket v1.5.0
|
github.com/gorilla/websocket v1.5.0
|
||||||
github.com/panjf2000/ants/v2 v2.11.3
|
github.com/panjf2000/ants/v2 v2.11.3
|
||||||
|
github.com/stretchr/testify v1.10.0
|
||||||
golang.org/x/crypto v0.17.0
|
golang.org/x/crypto v0.17.0
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
gorm.io/driver/postgres v1.5.9
|
gorm.io/driver/postgres v1.5.9
|
||||||
@@ -16,6 +17,7 @@ require (
|
|||||||
require (
|
require (
|
||||||
github.com/bytedance/sonic v1.9.1 // indirect
|
github.com/bytedance/sonic v1.9.1 // indirect
|
||||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
@@ -36,7 +38,9 @@ require (
|
|||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
||||||
|
github.com/stretchr/objx v0.5.2 // indirect
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||||
golang.org/x/arch v0.3.0 // indirect
|
golang.org/x/arch v0.3.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -73,6 +73,8 @@ github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7
|
|||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||||
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
|||||||
164
internal/tests/mocks/mock_repository.go
Normal file
164
internal/tests/mocks/mock_repository.go
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
package mocks
|
||||||
|
|
||||||
|
// Package mocks 模拟测试包
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockDeviceRepo 模拟设备仓库,实现DeviceRepo接口
|
||||||
|
type MockDeviceRepo struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create 模拟创建设备方法
|
||||||
|
func (m *MockDeviceRepo) Create(device *model.Device) error {
|
||||||
|
args := m.Called(device)
|
||||||
|
return args.Error(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByID 模拟根据ID查找设备方法
|
||||||
|
func (m *MockDeviceRepo) FindByID(id uint) (*model.Device, error) {
|
||||||
|
args := m.Called(id)
|
||||||
|
// 返回第一个参数作为设备,第二个参数作为错误
|
||||||
|
device, ok := args.Get(0).(*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return device, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByIDString 模拟根据ID字符串查找设备方法
|
||||||
|
func (m *MockDeviceRepo) FindByIDString(id string) (*model.Device, error) {
|
||||||
|
args := m.Called(id)
|
||||||
|
// 返回第一个参数作为设备,第二个参数作为错误
|
||||||
|
device, ok := args.Get(0).(*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return device, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByParentID 模拟根据上级设备ID查找设备方法
|
||||||
|
func (m *MockDeviceRepo) FindByParentID(parentID uint) ([]*model.Device, error) {
|
||||||
|
args := m.Called(parentID)
|
||||||
|
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||||
|
devices, ok := args.Get(0).([]*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return devices, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByType 模拟根据设备类型查找设备方法
|
||||||
|
func (m *MockDeviceRepo) FindByType(deviceType model.DeviceType) ([]*model.Device, error) {
|
||||||
|
args := m.Called(deviceType)
|
||||||
|
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||||
|
devices, ok := args.Get(0).([]*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return devices, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update 模拟更新设备信息方法
|
||||||
|
func (m *MockDeviceRepo) Update(device *model.Device) error {
|
||||||
|
args := m.Called(device)
|
||||||
|
return args.Error(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete 模拟删除设备方法
|
||||||
|
func (m *MockDeviceRepo) Delete(id uint) error {
|
||||||
|
args := m.Called(id)
|
||||||
|
return args.Error(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAll 模拟获取所有设备列表方法
|
||||||
|
func (m *MockDeviceRepo) ListAll() ([]model.Device, error) {
|
||||||
|
args := m.Called()
|
||||||
|
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||||
|
devices, ok := args.Get(0).([]model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return devices, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindRelayDevices 模拟获取所有中继设备方法
|
||||||
|
func (m *MockDeviceRepo) FindRelayDevices() ([]*model.Device, error) {
|
||||||
|
args := m.Called()
|
||||||
|
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||||
|
devices, ok := args.Get(0).([]*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return devices, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByDeviceID 模拟根据设备ID查找设备方法(额外方法)
|
||||||
|
func (m *MockDeviceRepo) FindByDeviceID(deviceID string) (*model.Device, error) {
|
||||||
|
args := m.Called(deviceID)
|
||||||
|
// 返回第一个参数作为设备,第二个参数作为错误
|
||||||
|
device, ok := args.Get(0).(*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return device, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindControllers 模拟查找控制器方法(额外方法)
|
||||||
|
func (m *MockDeviceRepo) FindControllers() ([]*model.Device, error) {
|
||||||
|
args := m.Called()
|
||||||
|
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||||
|
devices, ok := args.Get(0).([]*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return devices, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindRelays 模拟查找中继设备方法(额外方法)
|
||||||
|
func (m *MockDeviceRepo) FindRelays() ([]*model.Device, error) {
|
||||||
|
args := m.Called()
|
||||||
|
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||||
|
devices, ok := args.Get(0).([]*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return devices, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindDevicesByType 模拟根据类型查找设备方法(额外方法)
|
||||||
|
func (m *MockDeviceRepo) FindDevicesByType(deviceType string) ([]*model.Device, error) {
|
||||||
|
args := m.Called(deviceType)
|
||||||
|
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||||
|
devices, ok := args.Get(0).([]*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return devices, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindRelayDevices 模拟根据中继ID查找设备方法(额外方法)
|
||||||
|
func (m *MockDeviceRepo) FindRelayDevicesByID(relayID uint) ([]*model.Device, error) {
|
||||||
|
args := m.Called(relayID)
|
||||||
|
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||||
|
devices, ok := args.Get(0).([]*model.Device)
|
||||||
|
if !ok {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return devices, args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateDeviceStatus 模拟更新设备状态方法(额外方法)
|
||||||
|
func (m *MockDeviceRepo) UpdateDeviceStatus(id uint, active bool) error {
|
||||||
|
args := m.Called(id, active)
|
||||||
|
return args.Error(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDeviceStatus 模拟获取设备状态方法(额外方法)
|
||||||
|
func (m *MockDeviceRepo) GetDeviceStatus(id uint) (bool, error) {
|
||||||
|
args := m.Called(id)
|
||||||
|
return args.Bool(0), args.Error(1)
|
||||||
|
}
|
||||||
@@ -46,6 +46,9 @@ type Hub struct {
|
|||||||
|
|
||||||
// deviceRepo 设备仓库
|
// deviceRepo 设备仓库
|
||||||
deviceRepo repository.DeviceRepo
|
deviceRepo repository.DeviceRepo
|
||||||
|
|
||||||
|
// 关闭消息
|
||||||
|
close chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client WebSocket客户端结构
|
// Client WebSocket客户端结构
|
||||||
@@ -78,6 +81,7 @@ func NewHub(deviceRepo repository.DeviceRepo) *Hub {
|
|||||||
deviceClients: make(map[string]*Client),
|
deviceClients: make(map[string]*Client),
|
||||||
logger: logs.NewLogger(),
|
logger: logs.NewLogger(),
|
||||||
deviceRepo: deviceRepo,
|
deviceRepo: deviceRepo,
|
||||||
|
close: make(chan struct{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,10 +105,20 @@ func (h *Hub) Run() {
|
|||||||
h.unregisterClient(client)
|
h.unregisterClient(client)
|
||||||
case message := <-h.broadcast:
|
case message := <-h.broadcast:
|
||||||
h.broadcastMessage(message)
|
h.broadcastMessage(message)
|
||||||
|
case <-h.close:
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Hub) Close() {
|
||||||
|
// 关闭时清理所有资源
|
||||||
|
for client := range h.clients {
|
||||||
|
h.unregisterClient(client)
|
||||||
|
}
|
||||||
|
close(h.close)
|
||||||
|
}
|
||||||
|
|
||||||
// registerClient 注册客户端
|
// registerClient 注册客户端
|
||||||
func (h *Hub) registerClient(client *Client) {
|
func (h *Hub) registerClient(client *Client) {
|
||||||
h.mutex.Lock()
|
h.mutex.Lock()
|
||||||
|
|||||||
@@ -75,6 +75,10 @@ func (s *Server) Start() {
|
|||||||
go s.hub.Run()
|
go s.hub.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) Stop() {
|
||||||
|
s.hub.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// readPump 从WebSocket连接读取消息
|
// readPump 从WebSocket连接读取消息
|
||||||
func (c *Client) readPump() {
|
func (c *Client) readPump() {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
14
vendor/modules.txt
vendored
14
vendor/modules.txt
vendored
@@ -24,6 +24,9 @@ github.com/bytedance/sonic/utf8
|
|||||||
# github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311
|
# github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311
|
||||||
## explicit; go 1.15
|
## explicit; go 1.15
|
||||||
github.com/chenzhuoyu/base64x
|
github.com/chenzhuoyu/base64x
|
||||||
|
# github.com/davecgh/go-spew v1.1.1
|
||||||
|
## explicit
|
||||||
|
github.com/davecgh/go-spew/spew
|
||||||
# github.com/gabriel-vasile/mimetype v1.4.2
|
# github.com/gabriel-vasile/mimetype v1.4.2
|
||||||
## explicit; go 1.20
|
## explicit; go 1.20
|
||||||
github.com/gabriel-vasile/mimetype
|
github.com/gabriel-vasile/mimetype
|
||||||
@@ -129,8 +132,19 @@ github.com/pelletier/go-toml/v2/internal/characters
|
|||||||
github.com/pelletier/go-toml/v2/internal/danger
|
github.com/pelletier/go-toml/v2/internal/danger
|
||||||
github.com/pelletier/go-toml/v2/internal/tracker
|
github.com/pelletier/go-toml/v2/internal/tracker
|
||||||
github.com/pelletier/go-toml/v2/unstable
|
github.com/pelletier/go-toml/v2/unstable
|
||||||
|
# github.com/pmezard/go-difflib v1.0.0
|
||||||
|
## explicit
|
||||||
|
github.com/pmezard/go-difflib/difflib
|
||||||
# github.com/rogpeppe/go-internal v1.14.1
|
# github.com/rogpeppe/go-internal v1.14.1
|
||||||
## explicit; go 1.23
|
## explicit; go 1.23
|
||||||
|
# github.com/stretchr/objx v0.5.2
|
||||||
|
## explicit; go 1.20
|
||||||
|
github.com/stretchr/objx
|
||||||
|
# github.com/stretchr/testify v1.10.0
|
||||||
|
## explicit; go 1.17
|
||||||
|
github.com/stretchr/testify/assert
|
||||||
|
github.com/stretchr/testify/assert/yaml
|
||||||
|
github.com/stretchr/testify/mock
|
||||||
# github.com/twitchyliquid64/golang-asm v0.15.1
|
# github.com/twitchyliquid64/golang-asm v0.15.1
|
||||||
## explicit; go 1.13
|
## explicit; go 1.13
|
||||||
github.com/twitchyliquid64/golang-asm/asm/arch
|
github.com/twitchyliquid64/golang-asm/asm/arch
|
||||||
|
|||||||
Reference in New Issue
Block a user