Files
iotlcm/upstream-chan/internal/logic/upstreamapi/stale_loop_test.go

54 lines
1.8 KiB
Go

package upstreamapi
import (
"context"
"testing"
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model/entity"
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/service"
)
type staleLoopConfigSource struct {
service.IChannelConfig
snapshot *model.SimChannelConfigItemCache
latest *model.SimChannelConfigItemCache
}
func (s *staleLoopConfigSource) GetAllChannel(context.Context) ([]*model.SimChannelConfigItemCache, error) {
return []*model.SimChannelConfigItemCache{s.snapshot}, nil
}
func (s *staleLoopConfigSource) GetChannel(string) (*model.SimChannelConfigItemCache, bool) {
return s.latest, true
}
func TestLoopDoesNotReviveChannelFromStaleNormalSnapshot(t *testing.T) {
normal, abnormal := 0, 1
snapshot := &model.SimChannelConfigItemCache{
Channel: &entity.TSimChannels{ChanKey: "channel", TempKey: "template"},
Template: &entity.TSimChannelTemplate{TempKey: "template", TempModel: "direct"},
IsNormal: &normal,
Version: "old-normal",
}
latest := &model.SimChannelConfigItemCache{
Channel: &entity.TSimChannels{ChanKey: "channel", TempKey: "template"},
IsNormal: &abnormal,
Version: "new-abnormal",
}
source := &staleLoopConfigSource{snapshot: snapshot, latest: latest}
oldConfig := service.ChannelConfig()
service.RegisterChannelConfig(source)
t.Cleanup(func() { service.RegisterChannelConfig(oldConfig) })
api := NewUpstreamAPI()
api.RegisterChan("channel", "old-normal", &model.SimCardChannelCache{Instance: &requestScopedTestChannel{}, Version: "old-normal"})
api.RegisterTemp("template", &requestScopedTestTemplate{})
if err := api.loop(context.Background()); err != nil {
t.Fatal(err)
}
if _, ok := api.GetChan("channel"); ok {
t.Fatal("periodic loop revived or retained an abnormal channel from a stale snapshot")
}
}